我正在使用语音识别库创建一个类似于Siri的程序。我希望将来可以使用Arduino代码控制房间里的物品。这是我的问题:
我已经完成了基本的语音识别代码,但为了让程序理解某些命令,我需要通过一个非常长的if-elif-elif-elif-else命令列表运行语音,这可能会很慢。由于大部分时间它将以else结束,因为命令将不会被识别,所以我需要比长串的if-elif-else语句更快的替代方法。我还在使用tts引擎与您交流。
以下是我的代码:
我已经完成了基本的语音识别代码,但为了让程序理解某些命令,我需要通过一个非常长的if-elif-elif-elif-else命令列表运行语音,这可能会很慢。由于大部分时间它将以else结束,因为命令将不会被识别,所以我需要比长串的if-elif-else语句更快的替代方法。我还在使用tts引擎与您交流。
以下是我的代码:
import pyttsx
import time
engine = pyttsx.init()
voices = engine.getProperty("voices")
spch = "There is nothing for me to say"
userSaid = "NULL"
engine.setProperty("rate", 130)
engine.setProperty("voice", voices[0].id)
def speak():
engine.say(spch)
engine.runAndWait()
def command():
**IF STATEMENT HERE**
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("CaSPAR is calibrated")
audio = r.listen(source)
try:
userSaid = r.recognize_google(audio)
except sr.UnknownValueError:
spch = "Sorry, I did'nt hear that properly"
except sr.RequestError as e:
spch = "I cannot reach the speech recognition service"
speak()
print "Done"