在引发UnknownValueError之后,谷歌语音识别始终终止程序

时间:2016-07-26 19:06:00

标签: speech-recognition speech google-speech-api

引发UnknownValueError

后程序崩溃

我正在尝试继续通过应用程序但是这个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

0 个答案:

没有答案