2014-11-03 41 views
1

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

问题:它只是听一次然后停止running.I需要listen it till,i close the 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() 
+0

您需要线程来解决这个问题,并且由于Tkinter不知道线程,所以需要后续函数来轮询mainloop中的更改/识别。 – deets 2014-11-03 17:04:24

+0

@deets你能帮我一个小例子,涉及我的查询吗? – 2014-11-03 17:24:23

+0

@sarkite:如果你已经让PyAudio使用speech_recognition工作,就像你在这里完成的那样,你能不能关闭或者更好地回答你关于它的其他问题。 http://stackoverflow.com/questions/26666216/pyaudio-build-and-install-python-portaudio-bindings-first – tom10 2014-11-03 21:10:57

回答

3

您需要使用的线程来运行一个适当的并发语音识别,并使用更新文本控件内容后的方法,因为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() 
+0

我执行了上面的执行过程,并随着我的语音识别进行了随机编辑,因为这里是http://pastebin.com/7Czqq1Pr但是,我只能说一次,然后它不识别和打印,并且与我以前的编码一样。请帮助我做出一些改变,以便我能够说出来,直到我关闭了我的Tkinter。并且我没有识别出'清晰' – 2014-11-04 06:59:07

+0

您必须将您的语音识别代码* Thread-subclass的run-method,不只是一遍又一遍地分配一次计算的值。 – deets 2014-11-04 09:46:53

+0

我已经完全发布了一个关于此问题的新问题。 http://stackoverflow.com/questions/26731763/tkinter-to-display-and-clear-text-from-speech-recognition你能帮我解决这个问题吗? – 2014-11-04 10:08:31