Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Speech Recognition: 'module' object has no attribute 'microphone'

Running the following code on a macbook air 64 bit, testing the code on python 2.7 and python 3.4

import speech_recognition as sr
r = sr.Recognizer()
with sr.microphone() as source:
    audio = r.listen(source)

try:
    print("You said " + r.recognize(audio))
except LookupError:
    print("Could not understand audio")

When i try python 2.7, I keep getting the error of :

Traceback (most recent call last):
File "star.py", line 3, in <module>
with sr.microphone() as source:
AttributeError: 'module' object has no attribute 'microphone'

(star.py is the name of the file I am testing it on) When I try python 3.4, I keep getting the error of :

Traceback (most recent call last):
File "star.py", line 1, in <module>
import speech_recognition as sr
ImportError: No module named 'speech_recognition'

I have downloaded the speech_recognition and pyaudio libraries yet the cause of the error still eludes me. Please Help!

like image 647
Jacob Newbury Avatar asked Jan 17 '15 22:01

Jacob Newbury


2 Answers

Fix found -

pip install SpeechRecognition

pip install pyaudio

If you found error -

sudo apt-get install python-pyaudio

sudo apt-get install libjack-jackd2-dev portaudio19-dev

Then again -

pip install pyaudio
like image 139
Dadaso Zanzane Avatar answered Oct 20 '22 00:10

Dadaso Zanzane


For 2.7: The PyPI SpeechRecognition page mentions Microphone rather than microphone. Try capitalizing the name. If you had tried

>>> import speech_recognition as sr
>>> dir(sr)

to see what attributes the module does have, you would likely have discovered the mistake.

For 3.4, I suspect you downloaded and installed the sr module only for 2.7. At a command line (using the Windows version, adjust for another OS as needed)

.../python34/Scripts> pip install SpeechRecognition

should install for 3.4.

like image 33
Terry Jan Reedy Avatar answered Oct 20 '22 00:10

Terry Jan Reedy