我的电脑正在侦听并且没有前进,没有显示错误或其他内容

问题描述 投票:1回答:1

我正在尝试测试Python语音识别库以进行语音识别并转到文本,但是,当我运行该程序时,光标保持闪烁并且不前进,没有出现错误,我必须按Ctrl + C来中断它

这是我的代码,仅是一个示例

import speech_recognition as sr

   r = sr.Recognizer() 
   mic = sr.Microphone(device_index=0)
   with mic as source:
      print('Di algo: ')
      audio = r.listen(source)

      try:
         text = r.recognize_google(audio, language="es-EC")
         print('Tu dijiste:', text)
      except:
         print('No puedo escucharte')

但是它什么也不做,甚至没有给我一个错误或返回异常消息保持这样

PS C:\Users\dell\Documents\Cuarentena\Pruebas de reconocimiento de voz> & C:/Users/dell/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/dell/Documents/Cuarentena/Pruebas de reconocimiento de voz/prueba1.py"
Di algo:
|

当我中断程序时,我得到了这个:

File "c:/Users/kathy/Documents/Cuarentena/Pruebas de reconocimiento de voz/prueba1.py", line 9, in <module>
    audio = r.listen(source)
  File "C:\Users\kathy\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 652, in listen
    buffer = source.stream.read(source.CHUNK)
  File "C:\Users\kathy\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 161, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "C:\Users\kathy\AppData\Local\Programs\Python\Python38\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 speech-to-text
1个回答
0
投票

很久以前,我在同一个库中进行了一些测试,我尝试了两台计算机,在较新的计算机中,我遇到了问题,因为声卡会自动过滤环境声音,并且只能识别最后的单词。在另一种情况下,似乎什么也没做或花费很长时间来处理识别。最后,我通过添加以下行来做到这一点:

r.adjust_for_ambient_noise(source)

根据SpeechRecognition 3.8.1中的文档:

可能设置得太高而无法开始使用,然后通过动态能量阈值调整自动将其调整为较低的值。在其处于良好水平之前,能量阈值是如此之高,以至于语音仅被视为环境噪声。

解决方案是降低此阈值,或预先调用Recognir_instance.adjust_for_ambient_noise,这将自动将阈值设置为一个好的值。

下面是一个例子:speech_recognition/examples/calibrate_energy_threshold.py

© www.soinside.com 2019 - 2024. All rights reserved.