Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.
https://pypi.python.org/pypi/SpeechRecognition/
BSD 3-Clause "New" or "Revised" License
8.44k stars 2.4k forks source link

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

Open estella144 opened 2 years ago

estella144 commented 2 years ago

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'

taviroquai commented 2 years ago

Hello,

I'm having the same problem. Just running the example with microphone and it stalls and does nothing. But, running the example with wav file it runs without problems. So I guess its related to the microphone stream.

How long does the listen method listens to? It is a stream? When does it starts to convert to text? It is not clear from the example...

Any help please? Thank you.

rllysleepy commented 2 years ago

Hey you all! Same problem here. PyAudio's recording feature seems to breakdown after a few uses, in my case.

m1daz commented 2 years ago

Hey you all! Same problem here. PyAudio's recording feature seems to breakdown after a few uses, in my case.

Did you figure out any fix for that?

I am currently testing it as well and have the same issue, it can maybe figure out my hello once or twice and then proceeds to just not do anything.

rllysleepy commented 2 years ago

I paused the project quite a while ago, but IIRC my next steps were to use an alternate library to record audio, then write the data to a file and convert it to FLAC formatting, before using the SpeechRecognizer recognize function to get the text.

Alternatively, you could record audio and write to an array, outputting the data to a file whenever noise drops below x threshold, and send that for recognition. This process would basically replicate what pre-existing libraries have already done though. PyAudio seems to just be janky.

Ccode-lang commented 2 years ago

Hello, I had this problem too. Please check if you are using the default microphone. I was connected to my Oculos microphone by default for some reason 🤷

rllysleepy commented 2 years ago

That could work, but the issue is that it works for a few times before crashing. So the correct microphone should be being recognized.

firefly19 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。