Python linking python fails sometimes

I have downloaded and installed pyaudio and portaudio.

For code 1, it works fine all the time without error.

Code 2 I posted below works sometimes successfully . But sometimes the error appears :

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

I just installed pyaudio and portaudio directly. Do I have to do something extra so that all the time it runs my code will run successfully without the above error. My microphones were set correctly, no problems, which I specifically tested several times.

Please help me fix my code 2!

Code 1: http://pastebin.com/UvX0UNY9

Code 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()

      

+3


source to share





All Articles