Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#590·speech_recognition

sr.Recogniser.listen() does not work and stalls

Author: estella144Created Jan 16, 2022Updated Mar 10, 2025

Steps to reproduce

This code does not get past getting audio from the microphone: `import speech_recognition as sr

r = sr.Recognizer() m = sr.Microphone()

try: print("A moment of silence, please...") with m as source: r.adjust_for_ambient_noise(source) print("Set minimum energy threshold to {}".format(r.energy_threshold)) while True: print("Say something!") with m as source: audio = r.listen(source) print("Got it! Now to recognize it...") try: # recognize speech using Google Speech Recognition value = r.recognize_google(audio)

        # we need some special handling here to correctly print unicode characters to standard output
        if str is bytes:  # this version of Python uses bytes for strings (Python 2)
            print(u"You said {}".format(value).encode("utf-8"))
        else:  # this version of Python uses unicode for strings (Python 3+)
            print("You said {}".format(value))
    except sr.UnknownValueError:
        print("Oops! Didn't catch that")
    except sr.RequestError as e:
        print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e))

except KeyboardInterrupt: pass`

Expected behavior

Should advance to recognition: value = r.recognize_google(audio)

Actual behavior

with m as source: audio = r.listen(source), even after speaking, does not advance to the next line.

System information

My system is macOS Monterey version 12.0.1 (21A559).

My Python version is Python 3.10.1.

My Pip version is pip 21.3.1 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10).

My SpeechRecognition library version is 3.8.1.

My PyAudio library version is 0.2.11

My microphones are: ['MacBook Air Microphone', 'MacBook Air Speakers', 'ZoomAudioDevice'].

My working microphones are: Function does not work Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'Microphone' has no attribute 'list_working_microphones'

Source: Uberi/speech_recognition

View original on GitHubView discussion on GitHub