I am using Python speech recognition and when it hears sounds that are not understandable (like a dog barking) it raises an error and the program has to be restarted. Is there any way to fix this?
Restarting the program would not be an option for me as I need to run all the time.
import speech_recognition as sr
import pyttsx3
import Wikipedia
import pywhatkit
listener = sr.Recognizer()
player = pyttsx3.init()
def listen():
with sr.Microphone() as input_device:
print("100% booted waiting for commands.")
voice_content = listener.listen(input_device)
text_command = listener.recognize_google(voice_content)
text_command = text_command.lower()
print(text_command)
return text_command;
def talk(text):
player.say(text)
player.runAndWait()
def run_voice_bot():
command = listen()
if "what is" in command:
command = command.replace("what is", "")
info = wikipedia.summary(command, 5)
talk(info)
print(info)
elif "who is" in command:
command = command.replace("who is", "")
info = wikipedia.summary(command, 5)
talk(info)
print(info)
elif "play" in command:
command = command.replace("play", "")
pywhatkit.playonyt(command)
talk("ok here you go.")
else:
talk("Sorry, I am unable to find what you looking for")
print("Sorry, I am unable to find what you looking for")
run_voice_bot()
The error is caused by an unexpected format returned by Google's api, described by this issue in their repo.
Some things you can try are:
listener.recognize(voice_content)
instead of recognize_googlelistener.recognize_google(voice_content, key=<your api key>)
Alternatively, you can use an exception to elegantly handle errors:
try:
text_command = listener.recognize_google(audio, language='en')
except Exception as e:
return "None"
It's also good practice to specify the expected language being used. For example, if the expected language is English, use listener.recognize_google(voice_content, key=<your api key>, language='en')