4

I am trying a python script for speech recognition, i have installed the required pyaudio and SpeechRecognition modules in my enivronment.

The program was running fine till yesterday, but now it is stuck in "say something". Below is my code.

import speech_recognition as sr
print "say something1"
r = sr.Recognizer()
print "say something2"
with sr.Microphone() as source:                # use the default microphone as the audio source
    print "say something3"
    audio = r.listen(source,timeout=3)                   # listen for the first phrase and extract it into audio data

print "say something"
try:
    print("You said " + r.recognize(audio))    # recognize speech using Google Speech Recognition
except LookupError:                            # speech is unintelligible
    print("Could not understand audio")

Console o/p :-

say something1
say something2
ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
say something3

Not duplicate of/ Have refferd to :- speech recognition python code not working

Community
  • 1
  • 1
Sleez
  • 107
  • 1
  • 10

4 Answers4

2

May be this can solve your problem.

print "say something2"
with sr.Microphone() as source:                # use the default microphone as the audio source
    r.adjust_for_ambient_noise(source)  # here
    print "say something3"
    audio = r.listen(source,timeout=3)   

look at this. https://github.com/Uberi/speech_recognition/issues/191

Last solution work for me . hope same work for your

Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
2

I also had this problem and changing

audio = r.listen(source,timeout=3)

to

audio = r.listen(source,timeout=3, phrase_time_limit=3)

solved it for me.

Yovboy
  • 85
  • 7
1

I have had the same problem with me, i ended up installing jack2d and pulseaudio and what not.

That was the problem, I then uninstalled the jack2d by running

sudo apt-get remove --auto-remove jack

Then restarted the system, and then ran

jack_control  stop

Then this will give the voice input to pulse-aduio.

When you run the program, the console should print

ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack 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

The last lines state that Jack has stopped and the voice input is being redirected to pulse.

what actually happening here is, the audio resoruce(mic) is being channeled only to either jack or pulse, so I uninstalled jack

Now my program works just fine

penta
  • 2,536
  • 4
  • 25
  • 50
1

When I tried your code as it is I got following attribute error.

AttributeError: 'Recognizer' object has no attribute 'recognize'

Upon going through documentation it looks like the Recognizer class don't have a method recognize. you will need to use one of the several recognize_* methods that recognize class offers.
It seems you want to use recognize_google,so when I change your code from

print("You said " + r.recognize(audio))

to

print("You said " + r.recognize_google(audio))

The code is working for me.

I said "hello", which was recognized below.

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
say something1
say something2
say something3
say something
You said hello

Hope this helps.

Anil_M
  • 10,893
  • 6
  • 47
  • 74