Search code examples
pythonaudiospeech-recognitionmicrophonealsa

SpeechRecognition not working (microphone not detected)


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


Solution

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