2

I am trying to write a speech recognition code which takes voice from the microphone and process till a "Stop" is said. The code works for the first voice but then it gives an error. The code which I wrote is below:

import speech_recognition as sr
import webbrowser
r = sr.Recognizer()


with sr.Microphone() as source:                
    while True:
        audio = r.listen(source)
        print("You said " + r.recognize(audio)) 
        if r.recognize(audio)=="Facebook":
            webbrowser.open('https://www.facebook.com')
        if r.recognize(audio)=="Google":
            webbrowser.open('https://www.google.co.uk')
        if r.recognize(audio)=="Stop":
            break

The error which I am getting is :

You said Facebook
Traceback (most recent call last):
File "C:/Work/Scripts/SpeechRecognition/Speech.py", line 9, in <module>
print("You said " + r.recognize(audio)) # listen for the first phrase and extract it into audio data
File "C:\Users\roradhak.NDS-UK\AppData\Roaming\Python\Python27\site-packages\speech_recognition\__init__.py", line 324, in recognize
raise LookupError("Speech is unintelligible")

LookupError: Speech is unintelligible

Process finished with exit code 1
Roshan r
  • 522
  • 2
  • 11
  • 30
  • Peter.. I said Facebook and it opened the facebook page. I tried by waiting / No Waiting to say google but the error happens – Roshan r Jun 19 '15 at 10:20

2 Answers2

2

I think the problem might be that you call recognize many times on the same input and the first call has already consumed the audio. You can introduce one extra variable + add try/catch to handle the LookupError.

with sr.Microphone() as source:                
    while True:
        audio = r.listen(source)
        try:
            result = r.recognize(audio)
            print("You said " + result)
            words = result.lower()
            if words=="facebook":
                webbrowser.open('https://www.facebook.com')
            if words=="google":
                webbrowser.open('https://www.google.co.uk')
            if words=="stop":
                break
        except LookupError:
            print("Please, speak more clearly")
Ari24
  • 370
  • 5
  • 12
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
1

You need to catch the exception:

def recognize(audio):
    try:
        return r.recognize(audio)
    except LookupError, e:
        print e
        return ''

Then:

with sr.Microphone() as source:                
    while True:
        audio = r.listen(source)
        words = recognize(audio)
        print("You said " + words) 
        if words == "Facebook":
            webbrowser.open('https://www.facebook.com')
        elif words =="Google":
            webbrowser.open('https://www.google.co.uk')
        elif words == "Stop":
            break
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Yes, It worked for me. Thanks a lot. I have few more on the same but will raise a new question. – Roshan r Jun 19 '15 at 10:30
  • Peter.. I have a problem here. Saying "Stop" won't quit the program. – Roshan r Jun 19 '15 at 10:34
  • What does it print? Does it say `You said Stop` exactly, or something slightly different? e.g. case difference. – Peter Wood Jun 19 '15 at 10:52
  • Yes, it prints stop only. See the output below **C:\Python27\python.exe C:/Work/Scripts/SpeechRecognition/Speech2.py You said stop Speech is unintelligible You said Speech is unintelligible You said** – Roshan r Jun 19 '15 at 10:54
  • Peter... I corrected it. Changing from "Stop" to "stop" worked. – Roshan r Jun 19 '15 at 10:56