0

下面的代码显示了面部并使用声音产生输出。问题是我无法停止声音,我希望它只说一次,而不是为每一帧拍摄 PS 我尝试使用计时器但它不起作用。

    import cv2
    import pyttsx3
    cap = cv2.VideoCapture(0)
    voiceEngine = pyttsx3.init()
    while(True):
        # Capture frame-by-frame
        success, frame = cap.read()
    
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if success:
            voiceEngine.say("hello there")
            voiceEngine.runAndWait()
  
            cv2.imshow('frame',gray)
            if cv2.waitKey(1) & 0xFF == 27:
                break
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
4

1 回答 1

0

这就是标志的用途。

said = False
while True:
...
        if success and not said:
            voiceEngine.say("hello there")
            voiceEngine.runAndWait()
            said = True
于 2021-03-14T20:50:26.407 回答