没有输出?(Python语音识别)

No output?(Python speech recognition)

我已经看过超过 5 个关于如何使用语音识别模块的教程,但是当我尝试它们时,我没有得到任何输出!

import speech_recognition as sr

root = sr.Recognizer()

with sr.Microphone() as source :
    print('Listening:')
    audio = root.listen(source)
text = root.recognize_google(audio)
print(text)

这是我的代码。 它打印 Listening: 但如果我与它交谈,则不会打印任何内容。 我确定我的麦克风没问题,因为我测试了 windows 默认语音识别并且没有错误。 我还安装了所有需要的库,所以我认为这也不是什么大问题。

我认为你应该给 listen() 函数一个持续时间,以便识别器可以解析你的说法。

The phrase_time_limit parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. - based on the official docs

import speech_recognition as sr  
                                                                
root = sr.Recognizer()                                                                                   
with sr.Microphone(device_index=0) as source:                                                                       
    print("Speak:")                                                                                   
    audio = root.listen(source, phrase_time_limit=5.0) # in seconds  
    # or you can try this ...
    # audio = root.record(source, duration = 5.0)

print("You said " + root.recognize_google(audio))