python pyaudio绑定有时不执行

时间:2014-11-05 11:11:42

标签: python loops speech-recognition portaudio pyaudio

我已经下载并安装了pyaudio和portaudio。

对于代码1,它始终完美运行而不会产生任何错误。

我在下面发布的代码2 有时成功。但有时会显示错误

>>> Please build and install the PortAudio Python bindings first.

我刚刚直接安装了pyaudio和portaudio。我应该做一些额外的事情,这样一直运行我的代码将成功执行而不会出现上述错误。 我的麦克风安装正确,没有问题,我也经过多次专门测试。

请帮我纠正我的代码2!

代码1:http://pastebin.com/UvX0UNY9

代码2:

import threading
import time
import speech_recognition as sr
import pyaudio
import _portaudio
from Tkinter import *
import tkMessageBox
import Tkinter as tki
import tkFileDialog as th1
sr.Microphone()
class SpeechRecognizer(threading.Thread):
     def __init__(self):
         super(SpeechRecognizer, self).__init__()
         self.setDaemon(True)
         self.recognized_text = "initial"

     def run(self):
        r=sr.Recognizer()
        with sr.Microphone() as source:
          while True:
            audio = r.listen(source) 
            try:
                self.recognized_text=(r.recognize(audio))

            except LookupError:                            
                self.recognized_text=("Could not understand audio")
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)
         self.txt1.insert(0.0, recognizer.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)
root = tki.Tk()
app = App(root)
root.mainloop()

0 个答案:

没有答案