Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

python - OSError: No Default Input Device Available

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...