【问题标题】:type error function missing one argument 'self'类型错误函数缺少一个参数“自我”
【发布时间】:2017-08-11 02:34:28
【问题描述】:

我正在尝试线程化一个 Speech_recognition 函数以在后台连续运行,以及检查音频函数以查看说出的文本并采取相应的操作,我尝试线程化这 2 个函数以并行运行,但语音侦察功能越来越一遍又一遍地打电话,我从来没有使用过线程,并按照 youtube 上的教程来线程我的函数,我知道我可能犯了一个非常愚蠢的错误,所以我要求回答问题的人在他们的答案和我的错误。谢谢。

编辑
所以我在我的监听函数中删除了一个while循环,这导致了这个错误,使整个程序变得多余,但现在我得到了 TypeError: checksAudio() missing 1 required positional argument :我的“自我” 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() 

【问题讨论】:

  • 响应您的编辑,您没有实例化 listen。你忘记了括号;你想要a = listen()。但是,当然,现在您正在执行您打算在线程之外进行线程化的工作,并且仍然无法在线程中执行任何工作(因为您在定义目标时使用了不需要的括号)。阅读我的答案,它涵盖了所有这些。

标签: python multithreading speech-recognition python-multithreading


【解决方案1】:

您实际上并没有使用任何线程,而是在主线程中调用了函数,而不是使它们成为线程调用的目标。即使你有,你也从来没有调用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() 

【讨论】:

  • 感谢您的回答和解释。我很感激@ShadowRanger
猜你喜欢
  • 1970-01-01
  • 2021-01-14
  • 2018-03-28
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多