Search code examples
pythonpython-3.xspeech-recognitionspeech-to-text

My pc is listening and does not advance, does not show me an error or anything


I am trying to test the Python Speech Recognition library for speech recognition and go to text, however when I run the program the cursor stays blinking and does not advance, no errors come out, I have to press Ctrl + C to interrupt it

This is my code, is a only example

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

But it does nothing and doesn't even give me an error or return the exception message stays like this

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:
|

when i interrupt the program i get this:

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

Solution

  • I was doing some tests with the same library a long time ago, and I tried two computers, in the newer one I had problems because the sound card automatically filtered the ambient sound and only recognized the last words. While in the other it seemed that it did nothing or took a long time to process the recognition. In the end I did it by adding this line:

    r.adjust_for_ambient_noise(source)
    

    According to the documentation in SpeechRecognition 3.8.1:

    is probably set to a value that is too high to start off with, and then being adjusted lower automatically by dynamic energy threshold adjustment. Before it is at a good level, the energy threshold is so high that speech is just considered ambient noise.

    The solution is to decrease this threshold, or call recognizer_instance.adjust_for_ambient_noise beforehand, which will set the threshold to a good value automatically.

    Here's an example: speech_recognition/examples/calibrate_energy_threshold.py