python - Speech recognizing in a loop -


i trying write speech recognition code takes voice microphone , process till "stop" said. code works first voice gives error. code wrote below:

import speech_recognition sr import webbrowser r = sr.recognizer()   sr.microphone() 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 getting :

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 first phrase , extract 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 unintelligible") 

lookuperror: speech unintelligible

process finished exit code 1 

you need catch exception:

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

then:

with sr.microphone() 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 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -