Tkinker文本和语音识别

发布于 2025-01-27 01:51:09 字数 1649 浏览 3 评论 0原文

我在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?

screenshot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文