我正在尝试继续通过应用程序但是这个UnknownValueError在引发时会终止/结束整个程序。如果没有终止我的程序,我将如何使用sr.UnknownValueError?我实际上让它在某一点工作,但它似乎在没有触摸或编辑代码的情况下停止工作。我将不胜感激任何帮助。感谢。
这是我的代码:
while speakCommand == True:
# GOOGLE LISTEN
print ("Please speak a command.")
engine = pyttsx.init()
engine.say("Please speak a command.")
engine.runAndWait()
# r.record(source, duration = 60s, offset = None)
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source, timeout=None)
VoiceCommand1Up = r.recognize_google(audio)
VoiceCommand1Low = (VoiceCommand1Up.lower())
try:
print("You said, " + r.recognize_google(audio))
# engine = pyttsx.init()
# engine.say("We think you said, " + VoiceCommand1Low)
# engine.runAndWait()
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
engine = pyttsx.init()
engine.say("Google Speech Recognition could not understand audio")
engine.runAndWait()
standby = False
speakCommand = True
break
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
engine = pyttsx.init()
engine.say("Could not request results from Google Speech Recognition service; {0}".format(e))
engine.runAndWait()
standby = False
speakCommand = True
break