Someone
Someone

Reputation: 41

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

Upvotes: 1

Views: 8392

Answers (5)

A2SR Creations
A2SR Creations

Reputation: 46

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.

Upvotes: 2

A2SR Creations
A2SR Creations

Reputation: 46

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

Upvotes: -1

Omega Joctan
Omega Joctan

Reputation: 21

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

Upvotes: 0

Zeeking786
Zeeking786

Reputation: 195

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

Upvotes: 0

Naves
Naves

Reputation: 1

Try: Changing your default microphone device.

Upvotes: 0

Related Questions