Skip to content

myRecognizer object freezes the script when using .listen method (sometimes) #798

@SebasSBM

Description

@SebasSBM

Steps to reproduce

--- (How do you make the issue happen? Does it happen every time you try it?)

Summarizing: I start my computer. I run this code on Terminal. And it works as expected. After some tests, I interrupt the script with Ctrl + C. Then I feel like run it again, it works again, interrupt it again with Ctrl + C. Eventually, after running it several times, when I execute the script again, it always freezes when the r.listen statement is reached, and not even Ctrl + C will interrupt the execution.

When I instantiate a Recognizer object, and then execute the method myRecognizer.listen(source, timeout=5, phrase_time_limit=5) using sr.Microphone() as source, well... sometimes I run it and ev3rything works fine. But some other times I run the script and it freezes when using myRecognizer.listen method... when this happens, not even using Ctrl + C will succeed interrupting the script. This happens despite using timeout parameters to make sure ambient noise doesn't keep it listening forever, and depite testing it in relatively silent places. Trying to catch any exceptions, or even trying multithreading didn't solve the issue. This happens no matter if I use either Google Recognition or Sphinx Recognition

--- (Make sure to go into as much detail as needed to reproduce the issue. Posting your code here can help us resolve the problem much faster!)

# -*- coding: utf-8 -*-
import pyttsx3
import speech_recognition as sr

GOOGLE_RECOGNITION = 0
SPHINX_RECOGNITION = 1

def speech_to_string(language='es-es', recog_mode=GOOGLE_RECOGNITION):
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source, timeout=5, phrase_time_limit=5)

        query = ""
        try:
            print("Recognizing...")
            if recog_mode == GOOGLE_RECOGNITION:
                query = r.recognize_google(audio, language=language)
            elif recog_mode == SPHINX_RECOGNITION:
                query = r.recognize_sphinx(audio, language=language)
            else: # Default
                query = r.recognize(audio, language=language)
            #print(f"User said: {query}\n")  #User query will be printed.
        except sr.WaitTimeoutError as e:
            print(e)
            print("WAIT TIMEOUT ERROR")
        except sr.UnknownValueError as e:
            print(e)
            print("UNKNOWN VALUE ERROR")
        except sr.RequestError as e:
            print(e)
            print("UNKNOWN VALUE ERROR")
        except Exception as e:
            print(e)
            print("SOME OTHER EXCEPTION...") 
        return query


### MAIN CODE
engine = pyttsx3.init()
while True:
    # XXX Should I run `speech_to_string` in a different thread to not block the main thread? TODO
    #     I might add a variant of this main code  using ThreadpoolExecutor for Multithreading
    #     ...if required. For now I keep the snippet simple. It would freeze the same way anyways
    my_voice_str = speech_to_string('es-es', GOOGLE_RECOGNITION)
    print(f"User said: {my_voice_str}\n")

Expected behaviour

I just expect it to execute without freezing... and if freezing is unevitable due to infinite loops or other issues with the internal algorithm, at least, I'd like to have a chance to capture that as some kind of Exception that I can deal with.

Actual behaviour

As I explained in "Steps to reproduce" (AKA "how to make the issue happen"), sometimes it behaves as expected when you run the script. But some other times, you run the script and the script freezes completely. And when this happens, not even Ctrl + C from the linux Terminal will interrupt the script successfully. When it comes to this, my only option is forcefully closing the Terminal window.

(If the library threw an exception, paste the full stack trace here)

NO, it did throw none Exception at all. BUT, when it runs, I usually see all this output in the Terminal, both the times that the code works, and the times when it freezes. This one is, to be accurate, extracted from one of the executions that lead to freeze the Terminal console... but I honestly think it is very similar to this when it works as well:

ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:1000:(snd_pcm_dmix_open) unable to open slave
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
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:1000:(snd_pcm_dmix_open) unable to open slave
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
Listening...

System information

(Delete all the statements that don't apply.)

My system is <Ubuntu 24.04 LTS x64>

My Python version is <3.12.3>

My Pip version is <24.3.1>.

My SpeechRecognition library version is <3.10.4>. // Confirmed this issue happens with <3.12.x> as well

My PyAudio library version is <0.2.14>

My microphones are: ['sof-essx8336: - (hw:0,5)', 'sof-essx8336: - (hw:0,6)', 'sof-essx8336: - (hw:0,7)', 'pipewire', 'default']
I installed PocketSphinx from --> pip3

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions