语音识别python停在听

时间:2017-12-31 14:02:23

标签: python speech-recognition

我在安装了pyAudio的Python 2.7中运行以下代码。我使用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)”

1 个答案:

答案 0 :(得分:1)

我不是100%肯定,但我认为我有一段时间没有这个问题,这可能与麦克风源有关。你可以通过......修复它。

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

要弄清楚MICROPHONE_INDEX的值应该是什么,请运行以下代码:

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

这将打印出如下内容:

<form method="GET/POST">

</form>

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

我希望这有助于:)