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?
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.
SpeechRecognition Installation SpeechRecognition package is compatible with various versions of python language like 2.6, 2.7, and 3.3+.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With