来自recogn_google的UnknownValueError结果

时间:2017-09-01 17:49:31

标签: python-2.7 speech-recognition

我无法理解为什么在进行语音识别时会出现此错误,它会运行一段时间然后弹出此错误。

追踪(最近一次通话):   File" C:/Python27/new4.py" ;,第46行,in     mainfunction(源)   文件" C:/Python27/new4.py",第27行,在main函数中     user = r.recognize_google(audio)   在recogn_google中的文件" C:\ Python27 \ lib \ site-packages \ speech_recognition__init __。py",第780行     如果不是isinstance(actual_result,dict)或len(actual_result.get(" alternative",[]))== 0:raise UnknownValueError() UnknownValueError

def mainfunction(source):
    audio = r.listen(source)
    user = r.recognize_google(audio)

    # recognize speech using Google Speech Recognition
    try:
        # for testing purposes, we're just using the default API key
        # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        # instead of `r.recognize_google(audio)`
        print("Google Speech Recognition thinks you said " + user)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))



if __name__ == "__main__":
    r = sr.Recognizer()
    with sr.Microphone() as source:
        while 1:
            mainfunction(source)

1 个答案:

答案 0 :(得分:-1)

将“user = r.recognize_google(audio)”行移至try块