【问题标题】:python speech recognition in TkinterTkinter中的python语音识别
【发布时间】:2014-12-30 09:28:56
【问题描述】:

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

问题:它只听一次然后停止运行。我需要listen it till,i close the Tkinter

如果我说 clear 那么它必须清除文本框中的内容。 我的问题是,我不能直接将内容告诉 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()

【问题讨论】:

  • 你需要线程来解决这个问题,因为 Tkinter 不知道线程,你需要后函数来轮询你的主循环中的更改/识别。
  • @deets 你能帮我举个小例子吗,涉及我的查询?
  • @sarkite:如果您已经让 PyAudio 与 Speech_recognition 一起工作,就像您在这里所做的那样,请您关闭或更好地回答您关于它的其他问题。 stackoverflow.com/questions/26666216/…
  • 关于一个完全不相关的话题.. 你能告诉我你是如何让 Speech_recognition 工作的吗?我不断收到no module named speech_recognition

标签: python loops tkinter speech-recognition


【解决方案1】:

您需要使用线程来运行正确的并发语音识别,并使用 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()

【讨论】:

  • 我执行了上述执行并随机编辑了我的语音识别作为 sgown here pastebin.com/7Czqq1Pr 但是,我只能说一次,然后它无法识别和打印,并且与我之前的编码一样工作。请帮我做一些改变,这样我才能说话,直到我关闭我的 Tkinter。我不认识clear
  • 您必须将您的语音识别代码放入线程子类的运行方法中,而不是一遍又一遍地分配一次计算的值。
  • 我已经发布了一个关于这个问题的新问题,完全。 stackoverflow.com/questions/26731763/…你能帮我解决这个问题吗?
  • 我不能,因为这些问题与实际的语音识别有关——我不知道,也没有事先知道的知识。祝你好运!
猜你喜欢
  • 2019-06-19
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 2021-11-21
相关资源
最近更新 更多