1
votes

I wanted to do an little program with Speech Recognition.

Here is my code (classic one):

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
    print("SAY SOMETHING")
    audio = r.listen(source,timeout=3, phrase_time_limit=3)
    print("TIME OVER")
try:
    print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except Exception:
    print("ERROR")

But when I tried to start the program I have this error :

ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channeljack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Traceback (most recent call last):
File "record.py", line 6, in <module>
with sr.Microphone() as source:
File "/usr/lib/python2.7/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 "/usr/lib64/python2.7/site-packages/pyaudio.py", line 949, in
get_default_input_device_info
device_index = pa.get_default_input_device()
IOError: No Default Input Device Available

When I do arecord -l I have this :

**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC298 Analog [ALC298 Analog]
Subdevices: 0/1
Subdevice #0: subdevice #0

Ps : The microphone works well with any software like Skype or Google

5
From the module documentation: pypi.org/project/SpeechRecognition "As the error says, the program doesn’t know which microphone to use. To proceed, either use Microphone(device_index=MICROPHONE_INDEX, ...) instead of Microphone(...), or set a default microphone in your OS. You can obtain possible values of MICROPHONE_INDEX using the code in the troubleshooting entry right above this one." - tgikal
I already tried this with index 0 to 5 ... - Someone
And how can i set a default microphone on linux ? By only command lines ... i am on fedora 29 (but the thing that's strange is that i can use the microphone on all other apps) - Someone
Have you tried listing the devices to see what's available? for index, name in enumerate(sr.Microphone.list_microphone_names()): print("Microphone with name \"{1}\" found for Microphone(device_index={0})".format(index, name)) - tgikal
I have 13 device_index i tried all but no one works ... here is the error (same for all) : with sr.Microphone(device_index = 13) as source: File "/usr/local/lib/python3.7/site-packages/speech_recognition/__init__.py", line 141, in enter input=True, # stream is an input stream File "/usr/local/lib64/python3.7/site-packages/pyaudio.py", line 750, in open stream = Stream(self, *args, **kwargs) File "/usr/local/lib64/python3.7/site-packages/pyaudio.py", line 441, in init self._stream = pa.open(**arguments) OSError: [Errno -9998] Invalid number of channels - Someone

5 Answers

0
votes

Try: Changing your default microphone device.

0
votes

I have a workinng code snippet in one of my project try this it will work possibly:

import speech_recognition as sr

rObject = sr.Recognizer() 
audio = '' 
with sr.Microphone() as source: 
    print("Speak...")   
    audio = rObject.listen(source, phrase_time_limit = 0) 
    print("Stop.")
    try: 
        text = rObject.recognize_google(audio, language ='fr-FR') 
        print("You : "+ text)  
    except: 
        speak("Could not understand your audio...PLease try again !") 

Run this snippet and i think there is some issue of package named PyAudio...is not installed properly and if there is no pyaudio it will not work (specially in python 2.7 it will not work without pyaudio).

0
votes

what about adding a Speech_regognition Timeout error in the exception block

with sr.Microphone() as source:
    print("SAY SOMETHING")
    audio = r.listen(source,timeout=3, phrase_time_limit=3)
    print("TIME OVER")
try:
    print("TEXTE : "+r.recognize_google(audio, language="fr-FR"))
except sr.WaitTimeoutError:
    print("ERROR")
0
votes
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source, 1)
    print("Listening....")
    speak("listening")
    audio = r.listen(source)


    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"You said: {query}\n ")

    except sr.UnknownValueError:
        speak("Could not hear that, Try saying again")

    except sr.RequestError:
        speak("Make Sure that you have a good Internet connection")
    return query

You Have to import pyttsx3 by pip install pyttsx3 and also import speech_recognition as sr by pip install SpeechRecognition then your code may work.

-1
votes

Try Installing PyAudio into your code to cut off the error. Even try connecting to the network or make your microphone working.