Tkinker文本和语音识别
我在TKINTE和语音识别方面有问题。我正在创建一个聊天机器人,但是当我单击麦克风按钮时,我无法同时进行语音识别和TKINTER文本一起使用。我尝试穿线,但无法使其顺利进行...
我还尝试在一段时间内将语音识别放置,以便用户每次尝试说话时都不需要单击麦克风按钮,因为我希望用户能够与机器人交谈,直到他/她到达下一章...我也无法做到……
问题是当我使用螺纹
和时,
>循环语音识别即使我不单击麦克风按钮,也会在我跳过本章时继续工作...任何人都可以帮我弄清楚吗?
def record_audio():
global r
global talk
def txtEvent(event):
if event.state == 12 and event.keysym == 'c':
return
else:
return "break"
txt.bind("<Key>", lambda e: txtEvent(e))
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
voice = r.listen(source)
text_punk = r.recognize_google(voice, language="en-US")
text_punk = text_punk.lower()
punk_screen = "You---> " + text_punk
txt.insert(END, "\n" + punk_screen)
if text_punk != '':
test=testpy.Testing()
res = test.response(text_punk)
txt.insert(END, "\n" + 'Bot---> ' + res)
talk(res)
except sr.UnknownValueError:
txt.insert(END, "\n" + "System could not recognize your voice")
except sr.RequestError:
txt.insert(END, "\n" + 'Sorry, the speech service is down')
mic_btt = Button(frame_button, image=mic_icon_rsd, bd=0, bg="#2d4d82",
activebackground="#2d4d82",command= record_audio)
mic_btt.place(relx=0.5, rely=0.1)
I am having a problem with tkinter and speech recognition. I am creating a chatbot however I couldn't make speech recognition and tkinter text work together at the same time when I click on the mic button. I tried threading but couldn't make it work smoothly...
I also tried to put speech recognition in a while loop so that users don't need to click on the mic button everytime they tried to speak because I wanted users to be able to talk to the bot until he/she gets to the next chapter... I couldn't make it either...
The problem is when I use threading
and while
loops speech recognition starts working even if I don't click on the mic button and also it keeps working when I skip the chapter... Can anyone please help me figure it out?
def record_audio():
global r
global talk
def txtEvent(event):
if event.state == 12 and event.keysym == 'c':
return
else:
return "break"
txt.bind("<Key>", lambda e: txtEvent(e))
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
voice = r.listen(source)
text_punk = r.recognize_google(voice, language="en-US")
text_punk = text_punk.lower()
punk_screen = "You---> " + text_punk
txt.insert(END, "\n" + punk_screen)
if text_punk != '':
test=testpy.Testing()
res = test.response(text_punk)
txt.insert(END, "\n" + 'Bot---> ' + res)
talk(res)
except sr.UnknownValueError:
txt.insert(END, "\n" + "System could not recognize your voice")
except sr.RequestError:
txt.insert(END, "\n" + 'Sorry, the speech service is down')
mic_btt = Button(frame_button, image=mic_icon_rsd, bd=0, bg="#2d4d82",
activebackground="#2d4d82",command= record_audio)
mic_btt.place(relx=0.5, rely=0.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论