类型错误函数缺少一个参数 'self'
type error function missing one argument 'self'
我正在尝试在后台将 speech_recognition 函数连续线程化到 运行 并使用 checkingAudio 函数查看说出的文本并采取相应的操作,我尝试将这 2 个函数线程化到运行 并行但语音侦察功能被一遍又一遍地调用,我从未使用过线程并按照 youtube 上的教程对我的函数进行线程化,我知道我可能犯了一个非常愚蠢的错误所以我请回答问题的人详细说明他们的回答和我的错误。谢谢。
Edit
所以我在我的监听函数中删除了一个 while 循环,它导致了这个错误,使整个程序变得多余,但现在我得到了 TypeError: checkingAudio () 缺少 1 个必需的位置参数:'self' 我
as explained here 要求我实例化一个 class 但我这样做了,但仍然出现同样的错误。
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()
您实际上并没有使用任何线程,您在主线程中调用函数,而不是让它们成为线程调用的目标。即使您有,您也从未调用 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()
我正在尝试在后台将 speech_recognition 函数连续线程化到 运行 并使用 checkingAudio 函数查看说出的文本并采取相应的操作,我尝试将这 2 个函数线程化到运行 并行但语音侦察功能被一遍又一遍地调用,我从未使用过线程并按照 youtube 上的教程对我的函数进行线程化,我知道我可能犯了一个非常愚蠢的错误所以我请回答问题的人详细说明他们的回答和我的错误。谢谢。
Edit
所以我在我的监听函数中删除了一个 while 循环,它导致了这个错误,使整个程序变得多余,但现在我得到了 TypeError: checkingAudio () 缺少 1 个必需的位置参数:'self' 我
as explained here 要求我实例化一个 class 但我这样做了,但仍然出现同样的错误。
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()
您实际上并没有使用任何线程,您在主线程中调用函数,而不是让它们成为线程调用的目标。即使您有,您也从未调用 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()