0

I am trying to thread a speech_recognition function to run in the background continuously and the checkingAudio function to see what text was spoken and take actions accordingly, I tried to thread the 2 functions to run in parallel but the speech recon function is getting called over and over again, I have never worked with threading and followed a tutorial on youtube to thread my functions, I get that I could have made a very stupid mistake so I request the person who answers the question to be a little elaborate in their answer and my mistake. Thank you.

Edit
So I deleted a while loop in my listening function which was causing this error making the whole program redundant, but now I am getting TypeError: checkingAudio() missing 1 required positional argument: 'self' which I as explained here requires me to instantiate a class but I did that and the same error still.

class listen(threading.Thread):

    def __init__(self):

        self.playmusicobject = playmusic()
        self.r  = sr.Recognizer()

        self.listening()

def listening(self):

    self.objectspeak = speak()
    self.apiobject = googleAPI()
    print("say something")
    time.sleep(2.0)
    with sr.Microphone() as source:
        # self.objectspeak.speaking("say something")
        self.audio = self.r.listen(source)


    def checkingAudio(self):
        time.sleep(0.5)

        try:
            a = str(self.r.recognize_google(self.audio))
            a = str(self.r.recognize_google(self.audio))
            print(a)

            if a in greetings:
                self.objectspeak.speaking("I am good how are you?")

            if a in music:
                print("playing music")
                self.playmusicobject.play()
            if a in stop:
                print("stopping")
                self.playmusicobject.b()

            if a in api:
                self.apiobject.distance()

            else:
                print("error")

        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")

        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))


class speak:
    THIS IS A PYTTS class




class googleAPI:
    GOOGLE DISTANCE API function calculates distance between 2 places

class playmusic:

    def play(self):
        self.objectspeak = speak()
        playsound.playsound('C:\\Users\legion\Downloads\Music\merimeri.mp3')

    def b(self):
        self.objectspeak.speaking("music stopped")

while 1:
    a = listen
    t1 = threading.Thread(target=listen())
    t2 = threading.Thread(target= a.checkingAudio())
    t1.join()
    t2.join() 
  • 1
    In response to your edit, you didn't instantiate `listen`. You forgot parens; you wanted `a = listen()`. But of course, now you're doing the work you meant to thread outside of a thread, and still failing to do any work in the threads (because you have parens you don't want when defining their target). Read my answer, it covers all this. – ShadowRanger Aug 10 '17 at 18:37

1 Answers1

2

You didn't actually use any threading, you called the functions in your main thread, rather than making them the target to be called by the thread. Even if you had, you never called start to begin executing the threads. You need to fix a few things:

First, make sure you only perform the initialization, but not the ongoing work, in __init__; you need to finish creating the object first to even have one available for checkingAudio to use.

Second, change your thread creation to:

while 1:
    listener = listen()  # Make the object
    t1 = threading.Thread(target=listener.listening) # Note: No parens or we invoke in main thread
    t2 = threading.Thread(target=listener.checkingAudio) # Note: No parens
    t1.start()  # Actually launch threads
    t2.start()
    t1.join()
    t2.join() 
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • thank you for the answer and the explanation as well. I am grateful @ShadowRanger –  Aug 10 '17 at 18:40