我正在尝试创建一个能与自己对话的Python脚本。(示例:https://vimeo.com/172440766)我已经能够实现文本到语音和语音识别一个接一个地进行,但还没有找到同时进行的方法。
有没有办法并行执行这两个任务?欢迎任何建议。
回答:
如果你需要同时进行某些操作,请查看threading库:https://docs.python.org/3/library/threading.html
一个想法是为语音识别创建一个独立于主控制流程的线程,因为我认为这将大部分时间处于活动状态。作为开始,你可以这样做:
import threadingclass SpeechRecognition(threading.Thread): def __init__(self, parent=None): super().__init__() self.parent = parent def run(self): (你的语音识别函数/代码在这里)
并启动语音识别:
process = SpeechRecognition()process.start()