1

I am running the following code in Python 2.7 with pyAudio installed. I use this tutorial.

import speech_recognition as sr

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

try:
    print("You said " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Could not understand audio")
except sr.RequestError as e:
    print("Could not request results; {0}".format(e))

python stopped in row "audio = r.listen(source)"

babygame0ver
  • 447
  • 4
  • 16
  • in the tutorial they used the Shebang `#!/usr/bin/env python3` so they used python3. Maybe you have a inconsistent module for python2.7. Did you get any Error? Returned `r.listen(source)` an audio instance? – bierschi Dec 31 '17 at 14:30
  • Try running `python -m speech_recognition` and see if it works and recognizes your words. – Evya Dec 31 '17 at 17:27

1 Answers1

3

I am not 100% sure but I think I had this problem a while back and it could be something to do with the microphone source. You can fix it by...

change all instances of Microphone() to Microphone(device_index=MICROPHONE_INDEX), where MICROPHONE_INDEX is the hardware-specific index of the microphone

To figure out what the value of MICROPHONE_INDEX should be, run the following code:

import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

This will print out something like the following:

Microphone with name "HDA Intel HDMI: 0 (hw:0,3)" found for `Microphone(device_index=0)`
Microphone with name "HDA Intel HDMI: 1 (hw:0,7)" found for `Microphone(device_index=1)`
Microphone with name "HDA Intel HDMI: 2 (hw:0,8)" found for `Microphone(device_index=2)`
Microphone with name "Blue Snowball: USB Audio (hw:1,0)" found for `Microphone(device_index=3)`
Microphone with name "hdmi" found for `Microphone(device_index=4)`
Microphone with name "pulse" found for `Microphone(device_index=5)`
Microphone with name "default" found for `Microphone(device_index=6)`

Now, for example, to use the Snowball microphone, you would change Microphone() to Microphone(device_index=3).

I hope this helped :)

Jude Molloy
  • 193
  • 3
  • 18