1

引发 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
4

0 回答 0