【发布时间】: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