I'm working on a TTS/STT system for a school project and it was going well until I started getting this specific error whenever the code ran through the "Google Speech Recognition Does not recognize what you said" bit. I am unsure of why I get this specific error. The python libraries that I am using are just the espeak,speech recognition, and pyaudio libraries. My code is below.
from subprocess import call
import speech_recognition as sr
import serial
r = sr.Recognizer()
import os,time
def listen1():
with sr.Microphone(device_index = 2) as source:
r.adjust_for_ambient_noise(source)
print("Please speak.");
audio = r.listen(source)
print("Heard.");
return audio
def voice(audio1):
try:
text1 = r.recognize_google(audio1)
##call('espeak ' +text, shell=True)
print("You said: " +text1);
return text1;
except sr.UnknownValueError:
call(["espeak", "-a 200 -v en+1 ", "Google Speech Recognition did not recognize what you said."])
print("Google Speech Recognition did not recognize what you said.")
return 0
except sr.RequestError as e:
print("Could not request results fromm Google")
return 0
def main(text):
audio1 = listen1()
text = voice(audio1)
if 'hello' in text:
call(["espeak", "-a 200 -v en+1" , "Hi, how are you"])
if 'good' in text:
call(["espeak", "-a 200 -v en+1" , "Good to hear, now what will you have me do?"])
if 'exit' in text:
call(["espeak", "-a 200 -v en+1" , "Thank you, exiting."])
exit()
text = {}
main(text)
if __name__ =='__main__':
while(1):
audio1=listen1()
text = voice(audio1)
if text == 'start':
text = {}
call(["espeak", "-a 200 -v en+1" ,"Hello, user, please say a commmand"])
main(text)
else:
call(["espeak", "-a 200 -v en+1 " ,"Did not recognize command, repeat please?"])
What can I do to fix this error? Thanks!
As mentioned in the comments, when you get the error, your function
voice()
returns 0 to the variable text inmain()
. That function doesif 'hello' in text:
. But you can't do an in test on 0 because it is an integer.