Pyttsx3 与 Opencv 并行运行

问题描述

我正在尝试使用 pyttsx3 创建语音输出,而 Opencv 正在捕获和显示图像帧。但是 engine.runandwait() 使循环在预定时间内冻结。这导致图像帧和语音输出的差异。如何在语音输出中同步捕获和显示图像帧?下面是我用线程尝试过的伪代码,但它不起作用

import cv2
import numpy as np
import pyttsx3
from threading import Thread
import threading

def display(cap):
    ret,frame = cap.read()
    if ret == True:
        cv2.imshow('Frame',frame)
    if cv2.waitKey(1) == ord("q"):
        cv2.destroyAllWindows()

def textToSpeech(text):
    engine = pyttsx3.init()
    voices = engine.getProperty("voices")
    engine.setProperty("voice",voices[0].id)
    engine.setProperty("rate",220)
    engine.say(text)
    engine.runAndWait()
    del engine


def say(text,cap):
    t1 = threading.Thread(target=textToSpeech,args=(text,))
    t2 = threading.Thread(target=display,args = (cap,))

    t1.start()
    t2.start()



cap = cv2.VideoCapture(0)
if (cap.isOpened()== False): 
  print("Error opening video stream or file")

while(cap.isOpened()):
  say("welcome",cap)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)