Python Speech Recognition: 'module' object has no attribute 'microphone'
Asked Answered
B

6

8

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!

Batho answered 17/1, 2015 at 22:17 Comment(1)
try this for detailed installation guide : https://mcmap.net/q/412832/-cannot-install-pyaudio-gcc-errorTrishtrisha
O
5

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.

Organon answered 18/1, 2015 at 2:53 Comment(0)
C
15

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
Ciliata answered 9/10, 2015 at 7:37 Comment(0)
O
5

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.

Organon answered 18/1, 2015 at 2:53 Comment(0)
O
4

To fix this issue, try to open the terminal and execute:

brew install portaudio
pip install pyaudio

I hope this works for U ;)

Outcrop answered 7/12, 2015 at 11:22 Comment(0)
P
2

Mac comes with Python 2.7 pre-installed by Apple. and its default path is /usr/bin/python but if you install python 3 then it uses different path which is /applications/python 3 (depend on latest version 3.4 3.5 ..) so you have to install pyaudio and pip for python 3 separate

open the terminal and execute:

cd /Applications/Python\ 3.5 
pip3 install SpeechRecognition
pip3 install pyaudio
Polycotyledon answered 9/2, 2016 at 19:27 Comment(0)
O
1

You need to install packadge "pyaudio"

Opinion answered 13/7, 2015 at 17:4 Comment(0)
A
0

If you want to use audio input from microphones, PyAudio is also necessary. If not installed, the library will still work, but Microphone will not be defined.

Source

Afrit answered 3/9, 2015 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.