1
votes

I am trying to use the Speech Recognition library on Python 3.7 (OS X/internal microphone)

Here's the code I am using so far:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

print("Google Speech Recognition thinks you said " + r.recognize_google(audio))

However, there is no output and no error message when I run the program. But when I hit the "stop" button, this error message turns up

Traceback (most recent call last): File "/Users/diandraelmira/PycharmProjects/untitled/venv/APP.py", line 6, in audio = r.listen(source) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/init.py", line 620, in listen buffer = source.stream.read(source.CHUNK) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/speech_recognition/init.py", line 161, in read return self.pyaudio_stream.read(size, exception_on_overflow=False) File "/Users/diandraelmira/PycharmProjects/untitled/venv/lib/python3.7/site-packages/pyaudio.py", line 608, in read return pa.read_stream(self._stream, num_frames, exception_on_overflow) KeyboardInterrupt

How can I fix this?

2

2 Answers

0
votes

Hi could you please try this and will look for the errors

try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))
0
votes

i think there is need to little bit changes in your code and it is :

import speech_recognition as sr

rObject = sr.Recognizer() 
audio = '' 
with sr.Microphone() as source: 
    print("Speak...")   
    audio = rObject.listen(source, phrase_time_limit = 0) 
    print("Stop.")
    try: 
        text = rObject.recognize_google(audio, language ='en-US') 
        print("You : "+ text)  
    except: 
        speak("Could not understand your audio...PLease try again !") 

Try this ! Hope error will be solved .