0
votes

I am relatively new to Python (and programming in general) and am trying to use the Speech Recognition library. However, when running a very simple program on Python 3.3 using Mac OSX.

import speech_recognition as sr
r = sr.Recognizer()

sr.adjust_for_ambient_noise(source, duration = 1)
with sr.Microphone() as source:                # use the default microphone as the audio source
    audio = r.listen(source)                   # listen for the first phrase and extract it into audio data

try:
    print("You said " + r.recognize(audio))    # recognize speech using Google Speech Recognition
except LookupError:                            # speech is unintelligible
    print("Could not understand audio") 

The code keeps saying "Could not understand audio" even when I speak into microphone at a loud volume and make sure all ambient noise is to a minimum. How do I get the microphone and speech recognition to work on Mac OSX?

Are there other libraries that are better for speech recognition on Python and that can be used in GUIs successfully?

Thanks.

1
Shouldn't sr.adjust_for_ambient_noise(source, duration = 1) be within the using .. as source: block? Is this actual code you have run? - Toby Speight

1 Answers

-2
votes

Here is a working example of some code using speech_recognition with Python.

import speech_recognition as sr
#obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)
    print(r.recognize_google(audio))