python - 在 tkinter 上运行 voice_recognition 会导致卡住

标签 python python-3.x tkinter speech-recognition

我正在编写一个程序,你可以与它交谈,它会像 Siri 一样使用react。我使用 Google 的语音识别和 espeak 来听和说。然后对话被打印到文本框。

当程序被要求使用语音识别来收听时,它会卡住并再次单击 GUI,然后它说“没有响应”,是我运行错误还是无法在 tkinter< 中运行语音识别

为什么会结冰?

这是我迄今为止编写的完整代码:

import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr

def cbc(tex):

    return lambda : callback(tex)

def callback(tex):
    button = "Listen" 

    tex.insert(tk.END, button)
    tex.see(tk.END)# Scroll if necessary

def listen(tex):

        say('espeak '+"Say,,your,,command,,after,,the,,beep", shell=True)
        winsound.Beep(1000,500)

        ltext = 'listening...'
        tex.insert(tk.END, ltext)
        tex.see(tk.END)

        r = sr.Recognizer()

        with sr.Microphone() as source:
            damand = r.listen(source)

        damandtxt = (recognizer_google(damand))
        tex.insert(tk.END, damandtxt)
        tex.see(tk.END)



top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)


tk.Button(bop, text='Listen', command=lambda: listen(tex)).pack()
tk.Button(bop, text='Exit', command=top.destroy).pack()

top.mainloop()

最佳答案

运行脚本时,您的计算机一次只能执行一件事。因此,例如,如果您希望它监听,计算机在执行当前命令时将无法运行任何其他命令。解决这个问题的方法是使用多个线程,这样你的计算机就可以同时做两件事。查看Python 的线程模块。说实话,我对这方面也不太了解,但这是我在自己的 GUI 中实现的。

import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr

import threading

def cbc(tex):

    return lambda : callback(tex)

def callback(tex):
    button = "Listen" 

    tex.insert(tk.END, button)
    tex.see(tk.END)# Scroll if necessary

def listen(tex):
    def callback(tex):
        say('espeak '+"Say,,your,,command,,after,,the,,beep", shell=True)
        winsound.Beep(1000,500)

        ltext = 'listening...'
        tex.insert(tk.END, ltext)
        tex.see(tk.END)

        r = sr.Recognizer()

        with sr.Microphone() as source:
            damand = r.listen(source)

        damandtxt = (recognizer_google(damand))
        tex.insert(tk.END, damandtxt)
        tex.see(tk.END)

    a_thread = threading.Thread(target = callback(tex))
    a_thread.start()

top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)


tk.Button(bop, text='Listen', command=lambda: listen(tex)).pack()
tk.Button(bop, text='Exit', command=top.destroy).pack()

top.mainloop()

基本思想是创建一个线程对象,然后给它一个要运行的函数。然后,当您想要运行该线程时,请调用该线程的 start() 方法。绝对read因为当同时运行多个线程时,事情可能会变得棘手。

关于python - 在 tkinter 上运行 voice_recognition 会导致卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340393/

相关文章:

mysql - 什么是 mysql 缓冲游标 w.r.t python mysql 连接器

python - 如何让 Tkinter textarea 接受在 Python 3.X 上删除外部文件?

python - 如果 x 相同,如何不打印?

如果创建,Python 子进程 stderr/stdout 字段为 None

python - 有什么方法可以将模拟方法重置为其原始状态? - Python 模拟 - 模拟 1.0b1

python - 如何从 len()、str.format() 和零宽度空间获得合理的结果?

python - 使用 Python 正则表达式对整个文本中随机放置的整数求和

python - process_request() 恰好需要 2 个参数(给定 1 个) - Django Basic HTTP Auth Decorator

python - 如何在机器学习训练集中结合文本和数字特征?

python-3.x - Ubuntu Tkinter 安装不包括 PyImagingPhoto