语音识别 python 卡在听力中

Speech recogition python stuck in listening

我正在使用 nextech 桌面麦克风进行输入,我知道它可以工作,因为我在设置中对其进行了测试。我正在使用 python 2.7。当我执行下面的代码时,脚本就像卡在 audio = r.listen(source) 处一样。当我执行键盘中断以终止脚本时,这是回溯:

Traceback (most recent call last):
  File "test_audio.py", line 12, in <module>
    listen()
  File "test_audio.py", line 6, in listen
    audio = r.listen(source)
  File "/usr/local/lib/python2.7/dist-packages/speech_recognition /__init__.py", line 559, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/usr/local/lib/python2.7/dist-packages/speech_recognition/__init__.py", line 161, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
KeyboardInterrupt

这是代码:

import speech_recognition as sr

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
    try:
        print(r.recognize_wit(audio, key="############################"))
    except sr.RequestError as e:
        return "There was an error with the speech recognititon software."

listen()

您可能对此感兴趣:

The recognizer tries to recognize speech even when I’m not speaking, or after I’m done speaking.

Try increasing the recognizer_instance.energy_threshold property. This is basically how sensitive the recognizer is to when recognition should start. Higher values mean that it will be less sensitive, which is useful if you are in a loud room.

来源:https://pypi.python.org/pypi/SpeechRecognition/

这是解决方案:

print("Speak something")
audio = r.listen(source)

try:
    said = r.recognize_google(audio)
    print(f"You said: {said}")
except Exception as e:
    print(f"I think you are at very noisy place,\nThis is the error in computer languge: {str(e)}")

在写有​​“audio = r.listen(source)”的行中,您只需添加另一个参数“phrase_time_limit=5”。它看起来像这样:

audio = r.listen(source,phrase_time_limit=5)