pythonpython-3.xspeech-recognitionpyaudio

error handling speech_recognition WaitTimeOutError


when i tried to run this code

  import speech_recognition as sr  #importing sr
r = sr.Recognizer()

with sr.Microphone() as source:
    print("speak into mic")
    audio = r.listen(source,timeout=2)


try:
    print("Transcription:" + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Audio Unintelligible")

except sr.RequestError as e:
    print("cannot obtain results : {0}".format(e))

except sr.WaitTimeoutError as k :
    print("time out")    #error handler for time out error

when i run the above code it giving error like this

speak

 into mic
Traceback (most recent call last):
  File "C:/Users/punna/PycharmProjects/alex/alex.py", line 6, in <module>
    audio = r.listen(source,timeout=2)
  File "C:\Users\punna\Anaconda3\lib\site-packages\speech_recognition\__init__.py", line 544, in listen
    raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start

I wrote exception but it is again giving error

can anyone help me


Solution

  • It is because you kept the timeout for a limited amount of time. Try without the timeout or keep the timeout at least more than 5