Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter中的python语音识别_Python_Loops_Tkinter_Speech Recognition - Fatal编程技术网

Tkinter中的python语音识别

Tkinter中的python语音识别,python,loops,tkinter,speech-recognition,Python,Loops,Tkinter,Speech Recognition,我有一个代码,它将显示从语音识别到文本框的文本 问题:它只需监听一次,然后停止运行。我需要监听,直到关闭Tkinter 如果我说清除,那么它必须清除文本框上的内容。 我的问题是,我不能直接将内容告诉Tkinter。它会在Shell输出后侦听 请帮我解决我的问题 编码: from Tkinter import * import pyaudio import tkMessageBox import Tkinter as tki import tkFileDialog as th1 import sp

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

问题:它只需监听一次,然后停止运行。我需要
监听,直到关闭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方法更新文本窗口小部件的内容,因为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不知道线程,你需要after函数来轮询主循环中的更改/识别。@deets你能帮我举一个小例子,涉及我的查询吗?@sarkite:如果你有PyAudio来处理语音识别,就像你在这里做的那样,请你结束,或者更好地回答你关于它的另一个问题。在一个完全无关的话题上。。你能告诉我你是怎么让语音识别工作的吗?我一直得到
没有名为speech_recognition的模块
我执行了上述执行,并将我的speech recognition作为sgown进行了随机编辑,但我只能说一次,然后它无法识别和打印,与我以前的编码工作相同。请帮我做一些更改,以便我能够说话,在我关闭Tkinter之前,我无法识别
clear
您必须将语音识别代码放入Thread子类的run方法中,而不仅仅是一遍又一遍地分配一次计算值。我已经发布了一个新问题,关于这个问题,完全是。你能帮我一下吗?我不能,因为这些问题与实际的语音识别有关,我没有这方面的知识。祝你好运