1

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

Cam Clarke
  • 11
  • 3
  • 1
    You can either ask it to stop listening https://github.com/Uberi/speech_recognition/blob/master/examples/background_listening.py. – Deepak Patankar Dec 28 '19 at 14:15
  • I found a similar question here https://stackoverflow.com/questions/32005310/speech-recognition-python-code-not-working, whether the solution listed here helps ? – Deepak Patankar Dec 28 '19 at 14:16
  • Hey there, I've edited my original post! @DeepakPatankar Thanks for the help – Cam Clarke Jan 08 '20 at 18:27

1 Answers1

1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135