0

每次我对着麦克风说话时,第一个 except 块都会运行,请帮助!

'''

import speech_recognition as sr  

# get audio from the microphone                                                                       
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak:")                                                                                   
    audio = r.listen(source)   

try:
    print("You said " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Could not understand audio")
except sr.RequestError as e:
    print("Could not request results; {0}".format(e))

'''

4

2 回答 2

0

这应该会有所帮助

我在 try 块的打印语句中 添加了','而不是'+'

    import speech_recognition as sr  

    # get audio from the microphone                                                                       
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Speak:")                                                                                   
        audio = r.listen(source)   

    try:
        print("You said " ,r.recognize_google(audio))
    except sr.UnknownValueError:
        print("Could not understand audio")
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))
于 2021-01-30T13:50:12.773 回答
0

我认为您RequestError是 Google API 达到其极限的结果。谷歌说:

超过 1 分钟的音频必须使用 uri 字段来引用 Google Cloud Storage 中的音频文件。 有关文档,请参见此处

所以你需要在这里创建一个帐户并使用给定的 API 密钥。然后将音频上传到云端,然后将该链接用作程序中的参数。

这是谷歌提供的唯一解决方案。希望能帮助到你 :)

于 2020-06-08T04:04:57.323 回答