Tkinter 中的 python 语音识别

标签 python loops tkinter speech-recognition

我有一个代码,它将把语音识别中的文本显示到文本框中。

问题:它只听一次,然后停止运行。我需要听它,直到我关闭 Tkinter

如果我说清除,那么它必须清除文本框中的内容。 我的问题是,我无法直接将内容告诉 Tkinter。它在 Shell 输出后监听。

请帮我解决我的问题。

编码:

from Tkinter import *
import pyaudio
import tkMessageBox
import Tkinter as tki
import tkFileDialog as th1
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
    audio = r.listen(source) 
try:
    a=(r.recognize(audio))
    print a
except LookupError:                            
    a=("Could not understand audio")
    print a
class App(object):

    def __init__(self,root):
        self.root = root

    # create a Frame for the Text and Scrollbar
        txt_frm = tki.Frame(self.root, width=900, height=900)
        txt_frm.pack(fill="both", expand=True)
        # ensure a consistent GUI size
        txt_frm.grid_propagate(False)

    # create first Text label, widget and scrollbar
        self.lbl1 = tki.Label(txt_frm, text="Type")
        self.lbl1.grid(row=0,column=0,padx=2,pady=2)

        self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
        self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
        self.txt1.grid(row=25, column=7, sticky="nsew", padx=2, pady=2)
        self.txt1.insert(0.0,a)

    def clearBox(self):
        if a == "clear":
            self.txt1.delete('1.0', 'end')        
root = tki.Tk()
app = App(root)
root.mainloop()

最佳答案

您需要使用线程来运行正确的并发语音识别,并使用 after-method 更新文本小部件内容,因为 Tkinter 不支持不同方式的线程。

我这里没有语音识别功能,因此您需要填写空白并将 random.choice-call 替换为实际的语音识别功能。

 import threading
 import time
 import random

 from Tkinter import *
 import tkMessageBox
 import Tkinter as tki
 import tkFileDialog as th1

 class SpeechRecognizer(threading.Thread):

     ANSWERS = ["foo", "bar"]

     def __init__(self):
         super(SpeechRecognizer, self).__init__()
         self.setDaemon(True)
         self.recognized_text = "initial"

     def run(self):
         while True:
             time.sleep(1.0)
             self.recognized_text = random.choice(self.ANSWERS)


 recognizer = SpeechRecognizer()
 recognizer.start()

 class App(object):

     def __init__(self,root):
         self.root = root

     # create a Frame for the Text and Scrollbar
         txt_frm = tki.Frame(self.root, width=900, height=900)
         txt_frm.pack(fill="both", expand=True)
         # ensure a consistent GUI size
         txt_frm.grid_propagate(False)

     # create first Text label, widget and scrollbar
         self.lbl1 = tki.Label(txt_frm, text="Type")
         self.lbl1.grid(row=0,column=0,padx=2,pady=2)

         self.recognized_text = StringVar()
         self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55,
         )
         self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
         self.txt1.grid(row=25, column=7, sticky="nsew", padx=2, pady=2)
         root.after(100, self.update_recognized_text)

     def update_recognized_text(self):
         self.txt1.delete(0.0, END)
         self.txt1.insert(0.0, recognizer.recognized_text)
         root.after(100, self.update_recognized_text)

     def clearBox(self):
         if a == "clear":
             self.txt1.delete('1.0', 'end')

 root = tki.Tk()
 app = App(root)
 root.mainloop()

关于Tkinter 中的 python 语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719084/

相关文章:

python - 如何在opencv-python中将输出帧全部组合在一起?

python - POST 请求正文中删除了换行符? (谷歌应用引擎)

c++ - 如果 C++ 中有 for 循环,是否可以将方法声明为内联方法

python - 如何在Python脚本中途暂停并恢复它?

python - 如何在 matplotlib 中将 rc 参数 `usetex=True` 与其他字体一起使用

python - 试图用 scrapy 抓取网页的所有链接。但是我无法在页面上输出链接

Java TCP 客户端/服务器和循环问题

c# - 在一个时间跨度内循环

python - Tkinter - 如何更改默认笔记本边框颜色?

python - 更改 Tkinter 框架标题