我正在为一个学校项目开发一个TTS/STT系统,它一直进行得很好,直到当代码经过"Google语音识别不承认您所说的话“时,我才开始得到这个特定的错误。我不知道为什么我会犯这个错误。我正在使用的python库只是python、语音识别和py音频库。我的密码在下面。
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?"])我能做些什么来纠正这个错误?谢谢!
发布于 2022-04-17 06:41:38
正如注释中提到的,当您得到错误时,函数voice()将0返回到main()中的变量文本。该函数用于if 'hello' in text:。但是您不能在0上进行一个in测试,因为它是一个整数。
https://stackoverflow.com/questions/71899439
复制相似问题