语音识别python卡在听

时间:2017-02-26 15:14:30

标签: python python-2.7 audio speech-recognition

我正在使用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()

2 个答案:

答案 0 :(得分:1)

这可能对你很有意思:

  

即使我不说话,识别器也会尝试识别语音,   或者在我说完之后。

     

尝试增加recognizer_instance.energy_threshold属性。这个   基本上是识别器在识别时的敏感程度   应该开始。值越高意味着它就越不敏感   如果你在一个响亮的房间里很有用。

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

答案 1 :(得分:0)

这是解决方案:

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)