我正在尝试线程语音识别函数在后台连续运行,checkingAudio函数查看所说的文本,并采取相应的措施,我试图线程这两个函数并行运行,但语音识别函数被反复调用,我从来没有使用过线程技术,并且在youtube上通过一个教程来处理我的函数,我知道我可能犯了一个非常愚蠢的错误,所以我要求回答这个问题的人在他们的回答和我的错误时要稍微详细一点。谢谢您。在
编辑
所以我在监听函数中删除了一个while循环,这个循环导致了这个错误,使得整个程序变得多余,但是现在我得到了TypeError:checkingAudio()缺少一个必需的位置参数:“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()
实际上,您没有使用任何线程,而是在主线程中调用函数,而不是将它们作为线程调用的目标。即使有,也从来没有调用
start
来开始执行线程。你需要解决一些问题:首先,确保在
__init__
中只执行初始化,而不是正在进行的工作;您需要先完成对象的创建,以使checkingAudio
可以使用。在其次,将线程创建更改为:
相关问题 更多 >
编程相关推荐