Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SpeechRecognition ignores timeout when listening, and hangs

I'm using the SpeechRecognition package to try to recognize speech. When I call recognizer.listen(mic, timeout=5.0), the timeout is completely ignored. Sometimes it returns after one second or less even if I haven't spoken into the microphone. Sometimes it waits 30 seconds or more before returning. Sometimes it doesn't return at all, or is just taking so long that it appears to be hanging. Above all, it never times out.

How do I make the speech recognizer respect my tiemout?

like image 892
dinosaur Avatar asked Sep 24 '15 04:09

dinosaur


People also ask

How does Python recognize voice?

Recognition of Spoken WordsPyaudio − It can be installed by using pip install Pyaudio command. SpeechRecognition − This package can be installed by using pip install SpeechRecognition. Google-Speech-API − It can be installed by using the command pip install google-api-python-client.

What is SR in Python?

SpeechRecognition Installation SpeechRecognition package is compatible with various versions of python language like 2.6, 2.7, and 3.3+.

How do you use text to speech in Python?

Translation of Speech to Text: First, we need to import the library and then initialize it using init() function. This function may take 2 arguments. After initialization, we will make the program speak the text using say() function. This method may also take 2 arguments.


1 Answers

Try setting recognizer.dynamic_energy_threshold = False.

To understand why this works, print out recognizer.energy_threshold to see what its value is for different iterations when recognizer.dynamic_energy_threshold is True.

I found that if the energy threshold is allowed to be dynamic, sometimes it sets itself very low. Then, even if you're not speaking, the recognizer hears continuous ambient noise and thinks you are speaking continuously. It won't return until it stops hearing "speech". It won't time out unless it hears utter silence for the duration of the timeout time.

I set the energy threshold to 400, and recognizer.dynamic_energy_threshold = False. This way, the recognizer interprets some amount of background noise (including heavy breathing) as utter silence, and times out as expected when I'm not speaking.

like image 123
dinosaur Avatar answered Oct 22 '22 15:10

dinosaur