<分区> 分区>
我正在使用语音识别库创建类似 Siri 的程序。我希望将来我可以使用带有 Arduino 的代码来控制我房间周围的东西。这是我的问题:
我已经制定了基本的语音识别代码,但为了让程序理解某些命令,我必须通过一长串 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"
