user11251362
user11251362

Reputation: 101

Speech to text code stuck on "say something":

I am running speech to text code on python 3 by importing speech_recognition and my program stuck on "say something" and showing

sudo jack_control start //terminal commmand

--- start

sudo python speech.py //terminal command

terminal output:

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
speak say anything

python3 code:

import speech_recognition as sr 
r = sr.Recognizer()
with sr.Microphone() as source:
    print('speak say anything')
    audio = r.listen(source)
    text = r.recognize_google(audio)
    print("you said:{}".format(text))

I am not able to find the problem whether it is in jack or in code.

Upvotes: 6

Views: 2235

Answers (1)

glhr
glhr

Reputation: 4537

As per official documentation, the listen() method waits until the audio energy exceeds a certain level (indicating that someone is speaking), and records until silence is detected. If your microphone is picking up too much ambient noise, then listen() never returns because it keeps waiting for silence.

To remedy this, you can use r.adjust_for_ambient_noise(source):

with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source)
    print('speak say anything')
    audio = r.listen(source) 
    print("done listening")

You can also specify timeout and phrase_time_limit parameters in listen() to make it stop and return after a certain number of seconds even if no speech or silence have been detected.

with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source)
    print('speak say anything')
    try:
    # wait for speech for a maximum of 3 seconds
    # listen to speech for a maximum of 3 seconds
        audio = r.listen(source, timeout=3, phrase_time_limit=3)
    except Exception as e:
        # a timeouterror exception will be thrown if the timeout is reached
        print(e) 
    print("done listening")

In the initialization, it might also be a good idea to check for working microphones and explicitly set the device_index:

for device_index in Microphone.list_working_microphones():
    m = Microphone(device_index=device_index)
    break
else:
    print("No working microphones found!")

Upvotes: 6

Related Questions