如何解决recogniser.listensource不会停止监听
我正在制作一个python脚本,该脚本会听您的声音,然后根据您所说的内容执行功能。
while True:
try:
with sr.Microphone() as source1:
SpeakText("one")
r.adjust_for_ambient_noise(source1)
print("Set minimum energy threshold to {}".format(r.energy_threshold))
SpeakText("two")
audio1 = r.listen(source)
SpeakText("three")
MyText = r.recognize_google(audio1)
print(MyText)
SpeakText("four")
MyText = MyText.lower()
if MyText == "oink" or "ink" or "link":
playsound("oink_boop.mp3")
detect_command()
except sr.UnkNownValueError:
print("No Speech or No Internet")
现在我只是为了调试而放入SpeakText部分(使用pyttsx3),但是它说“两个”然后卡住了,以听我在audio1 = r.listen(source)上的讲话
似乎并没有停止听我的声音,我什至尝试使麦克风静音,但这也不起作用。
解决方法
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
audio = r.listen(source)
try:
query = r.recognize_google(audio,language='en-en')
your_query_Fuction(query)
except Exception:
print("No Speech or No Internet")
your_Mic_Fuction()
您还应该使用:
if 'oink' in MyText or ...
编辑: 使用这个。
def your_Mic_Fuction():
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
audio = r.listen(source)
try:
query = r.recognize_google(audio,language='en-en')
your_query_Fuction(query)
except Exception:
print("No Speech or No Internet")
your_Mic_Fuction()
def your_query_Fuction(query):
if "oink" in query or "ink" in query or "link" in query:
playsound("oink_boop.mp3")
your_Mic_Fuction()
,
我已经尝试过了:
with sr.Microphone() as source1:
r.adjust_for_ambient_noise(source1)
r.pause_threshold = 1
audio1 = r.listen(source1)
try:
MyText = r.recognize_google(audio1,language='en-en')
MyText = MyText.lower()
if "oink" in MyText or "ink" in MyText or "link" in MyText:
playsound("oink_boop.mp3")
detect_command()
except sr.UnknownValueError:
print("No Speech or No Internet")
它仍然不起作用 我做错了什么吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。