使用
pyttsx3
和 speech_recognition
运行 Python 脚本时遇到分段错误错误。这是我的代码的简化版本:
import time
import pyttsx3
import speech_recognition as sr
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def speke(text):
engine.say("")
engine.say(text)
engine.runAndWait()
def dot_s():
for i in range(4):
time.sleep(0.5)
print(".", end="", flush=True)
print("")
if __name__ == "__main__":
speke("Initialize Jarvis......")
while True:
try:
with sr.Microphone() as source:
print("\nListening", end="", flush=True)
dot_s()
audio = recognizer.listen(source, timeout=2, phrase_time_limit=2)
print("Recognizing", end="", flush=True)
dot_s()
Word = recognizer.recognize_google(audio)
if Word.lower() == "jarvis":
speke("ya")
with sr.Microphone() as source:
print("\nJarvis Activating", end="", flush=True)
dot_s()
audio = recognizer.listen(source)
print("Recognizing", end="", flush=True)
dot_s()
command = recognizer.recognize_google(audio)
processCommand(command)
except Exception as x:
print(f"Error : {x}")
运行此脚本会导致“分段错误(核心转储)”错误。我该如何解决这个问题?
问题解决:问题在第 9 行
engine.say("")