语音识别 python 已停止收听

speech recognition python stopped in listen

我是 运行 Python 2.7 中安装了 pyAudio 的以下代码。我使用 this 教程。

import speech_recognition as sr

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))

python 在第 "audio = r.listen(source)"

行停止

我不是 100% 确定,但我想我前阵子遇到过这个问题,这可能与麦克风源有关。您可以通过...

修复它

将 Microphone() 的所有实例更改为 Microphone(device_index=MICROPHONE_INDEX),其中 MICROPHONE_INDEX 是麦克风的硬件特定索引

要弄清楚 MICROPHONE_INDEX 的值应该是多少,运行 下面的代码:

import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

这将打印出如下内容:

Microphone with name "HDA Intel HDMI: 0 (hw:0,3)" found for `Microphone(device_index=0)`
Microphone with name "HDA Intel HDMI: 1 (hw:0,7)" found for `Microphone(device_index=1)`
Microphone with name "HDA Intel HDMI: 2 (hw:0,8)" found for `Microphone(device_index=2)`
Microphone with name "Blue Snowball: USB Audio (hw:1,0)" found for `Microphone(device_index=3)`
Microphone with name "hdmi" found for `Microphone(device_index=4)`
Microphone with name "pulse" found for `Microphone(device_index=5)`
Microphone with name "default" found for `Microphone(device_index=6)`

现在,例如,要使用 Snowball 麦克风,您可以将 Microphone() 更改为 Microphone(device_index=3)。

希望对您有所帮助:)