Python Speech_Recognition hung up on mic listening

1.5k views Asked by At

I'd appreciate the help if someone knows whats happening here. So i am trying to recognise speech inputted via a microphone (with device index 1), and my program is getting hung up on the listening. Ive adjusted for ambient noise and i am still not making any progress.

Here is my code :

import pyaudio
import speech_recognition as sr

r = sr.Recognizer()
mic = sr.Microphone(1)
with mic as source:
     r.adjust_for_ambient_noise(source)
     print("Please Speak :")
     audio = r.listen(source)
     print("Stop Talking")

     try:
         text = r.recognize_google(audio)
         print("You said : " + r. recognize_google(text))
     except:
         print("Sorry, I could not recognize what you said")

And this is all i get when the program is run:

Please Speak :

then nothing else. I ran this in the python console and never got the console prompt back.

r = sr.Recognizer()
with mic as source:
 r.adjust_for_ambient_noise(source)
 print("Please Speak :")
 audio = r.listen(source)

Please Speak :

Im not sure what I can do here or whats going wrong. I am running:

    Mac OS Mojave 10.14.6
    Python 3.8
    Pycharm IDLE

Now I have made some changes:

import speech_recognition as sr

r = sr.Recognizer()
print(sr.Microphone.list_microphone_names())
mic = sr.Microphone(device_index=1)
with mic as source:
    r.adjust_for_ambient_noise(source, duration=5)
    print("Please Speak :")
    audio = r.listen(source, timeout=5)
    print("Stop Talking")

try:
    text = r.recognize_google(audio)
    print("You said : " + text)
except:
    print("Sorry, I could not recognize what you said.")

And this is my error:

eTraceback (most recent call last):
File "/Users/cameronclarke/PycharmProjects/SpeechRecog/Speech 
Recog.py", line 13, in <module>
audio = r.listen(source, timeout=5)
File 
Users/cameronclarke/opt/anaconda3/envs/SpeechRecog/lib/python3.7/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.

Why is this happening? Cheers! All help is appreciated :)

1

There are 1 answers

0
Bingchen Li On

The problem is the OS does not allow your IDE to access microphone. If you run your python code in OS native 'Terminal' there would be a prompt to ask you if allow terminal to access microphone, then allow it, the code will work.