I am trying speech recognition with SpeechRecognition package in python and facing problem when trying to work with microphone.
I tested Microphone of my earphones , it is working fine and is being detected by my computer but my script is throwing error as if there is no mic connected. When I run the following script after installing pyAudio
$python -m speech_recognition
I get following error:
Traceback (most recent call last):
File "/home/harshita/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/harshita/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/harshita/anaconda3/lib/python3.6/site-packages/speech_recognition/__main__.py", line 4, in <module>
m = sr.Microphone()
File "/home/harshita/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 86, in __init__
device_info = audio.get_device_info_by_index(device_index) if device_index is not None else audio.get_default_input_device_info()
File "/home/harshita/anaconda3/lib/python3.6/site-packages/pyaudio.py", line 949, in get_default_input_device_info
device_index = pa.get_default_input_device()
OSError: No Default Input Device Available
And also:
import speech_recognition as sr
sr.Microphone.list_microphone_names()
output: [ ]
Where is it that I am going wrong?
Also why is it showing 'OSError'?, I saw other related queries but all of them had it as IOError.
At first(as for Linux users), check the following link and update your Pyaudio & Portaudio with the given repository, since there is a bug in Anaconda's Pyaudio and Portaudio library.
Now if it worked, but the terminal is stuck on "Speak anything..", then it means that the library is detecting too many noises, and you can filter them out by adding the following line after the with
statement.
r.adjust_for_ambient_noise(source)
For example:
with sr.Microphone(device_index=2) as source:
r.adjust_for_ambient_noise(source)
print("Speak Anything :")
audio = r.listen(source)
Please note the r
here is the instance of speech_recognition.Microphone().
Also, I recommend you start passing the index of the the device you want to use like I did in the above example (like this device_index=2
), and you could try using an index with range of 0 to 4 (could be more or less, depends on how many inputs you have).
Use the below command. I solved this issue following this
conda install nwani::portaudio nwani::pyaudio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With