类型错误函数缺少一个参数' self'

时间:2017-08-10 18:05:16

标签: python multithreading speech-recognition python-multithreading

我正在尝试将一个speech_recognition函数连接到后台连续运行,并且checkAudio函数查看所说的文本并采取相应的操作,我试图将这两个函数并行运行但是语音侦听功能正在增加一遍又一遍地调用,我从来没有使用线程,并在youtube上按照教程来编写我的函数,我知道我可能犯了一个非常愚蠢的错误,所以我要求回答问题的人在他们的回答我的错误。谢谢。

编辑
所以我在我的监听功能中删除了一个while循环,这导致了这个错误,使整个程序变得冗余,但现在我得到了TypeError:checkingAudio()缺少1个必需的位置论证:' self'我 as explained here要求我实例化一个类但我做了那个并且仍然存在同样的错误。

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 个答案:

答案 0 :(得分:3)

你没有实际使用任何线程,你在主线程中调用了函数,而不是让它们成为线程调用的目标。即使你有,你也从未调用start来开始执行线程。你需要解决一些问题:

首先,确保您只在__init__中执行初始化,而不是正在进行的工作;你需要先完成创建对象,甚至可以让checkingAudio使用它。

其次,将您的线程创建更改为:

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