python pyaudio绑定有时不执行

2024-05-18 18:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经下载并安装了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()

Tags: 代码textimportselftxttruedefas

热门问题