我希望我的 python 聊天机器人在我说“Echo”时开始收听。我怎么做?下面是一段聊天机器人。
import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source)
try:
x=r.recognize_google(audio)
print(x)
except sr.UnknownValueError:
pass
except sr.RequestError as e:
pass
while running==True:
r = sr.Recognizer()
with sr.Microphone() as source:
while 1:
Speech()
最佳答案
经过打击和试验,我做对了。但是我觉得很慢。如果你有更好的攻略欢迎评论。
import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source)
try:
x=r.recognize_google(audio)
if x=="hello":
print("Speak up")
audio = r.listen(source)
print(r.recognize_google(audio))
except sr.UnknownValueError:
pass
except sr.RequestError as e:
pass
while running==True:
Speech()
关于python - 如何让聊天机器人开始监听调用它的名字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249113/