【问题标题】:Speech_Recognition engine timeout (python 3.7.4)Speech_Recognition 引擎超时(python 3.7.4)
【发布时间】:2020-10-24 22:38:36
【问题描述】:

我正在尝试在一个学校项目中用 python 实现语音识别和响应。无法弄清楚我的代码有什么问题:

代码:(导入speech_recognition as sr

def takeCommand():
    # It takes microphones recognition from the user and returns String output
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 0.8
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio,language='en-in')
        print(f"User said:{query}\n")

    except Exception as e:
        # print(e)
        print("Say that again please")
        return "None"
    return query

print('Listening...') 语句后停止响应。

回溯(在键盘中断时):

Listening...
Traceback (most recent call last):
  File "c:/Users/Yash Khandelwal/Desktop/Python_Files/Jarvis/Jarvis_Project.py", line 51, in <module>
    takeCommand()
  File "c:/Users/Yash Khandelwal/Desktop/Python_Files/Jarvis/Jarvis_Project.py", line 36, in takeCommand
    audio = r.listen(source)
  File "C:\Users\New User\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 652, in listen    
    buffer = source.stream.read(source.CHUNK)
  File "C:\Users\New User\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition\__init__.py", line 161, in read      
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "C:\Users\New User\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
KeyboardInterrupt

这很容易解决吗?

【问题讨论】:

    标签: python python-3.x speech-recognition


    【解决方案1】:

    删除这一行:

    r.pause_threshold = 0.8
    

    试试这个:

    r.listen(source,timeout=2)
    

    为我工作!

    【讨论】:

      【解决方案2】:

      尝试删除暂停阈值并使用:

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

      【讨论】:

        【解决方案3】:

        你可以试试 query = r.recognize_google(audio,language='En-In')

        查询 = r.recognize_google(音频)

        它对我有用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-16
          • 1970-01-01
          • 2020-09-03
          • 2019-10-18
          相关资源
          最近更新 更多