JGStyle
JGStyle

Reputation: 127

Python speech recognition is stuck (Mac)

This is my code:

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
    print("Go ahead!")
    audio = r.listen(source)

    try:
        text = r.recognize_google(audio)
        print("Understood: %s" % text)
    except:
        print("Couldnt recognize your voice")

But if I start the program it says Go ahead! but nothing else happens... Is it because I might have to gran python the microphone somehow? Im on Mac It doesn't print an error message or say Couldnt recognize your voice

Upvotes: 1

Views: 1001

Answers (3)

usersina
usersina

Reputation: 1783

It was all about the threshold for me, my macbook's microphone is too sensitive for its own good and it wasn't "stuck" per say. It was simply listening endlessly since it thought I wasn't done talking.

You can verify this by opening the microphone interface on the side and lowering it all the way down as soon as you finish talking and will then proceed to the "recognizing" step, but that's far from an ideal solution.

What you can do though is increasing the threshold sensitivity which might work just fine for you.

r.energy_threshold = 5000

Even with this, my microhpone was still too sensitive, so I opted for a push to talk solution where I would hold a button, speak, then release the button.

Here's a nice freeware that you can use. That said, it does not work for M1 out of the box so you have to build it from source (this is as easy as running two commands or three, if you have an xcode error)

Upvotes: 0

JGStyle
JGStyle

Reputation: 127

The problem was using the integrated terminal. Using an external Terminal like iTerm or the default terminal app will solve the problem

Upvotes: 2

Julio S.
Julio S.

Reputation: 970

Firstly try using Python up to 3.6.

Then install the following:

xcode-select --install
brew remove portaudio
brew install portaudio
pip3 install pyaudio

Now it should work. Also, in order to debug your code, remove the block try/except. It will help you checking what's wrong.

Upvotes: 0

Related Questions