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
Post a Comment