0

The code below shows the face and produces an output using voices. The problem is I'm unable to stop the voices, I want it to say it only once not for each frame taken P.S I've tried using a timer but it didn't work.

    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()

1 Answers1

0

This is what flags are for.

said = False
while True:
...
        if success and not said:
            voiceEngine.say("hello there")
            voiceEngine.runAndWait()
            said = True
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Where is there any facial recognition in this code? – Tim Roberts Mar 15 '21 at 17:48
  • I deleted my first comment by mistake: But how in my case can I let it say my name whenever he sees me, in other words, if my face in the frame he says my name once if I'm off the frame and I come back to the frame he says my name again also once. – Morianoo Mar 15 '21 at 18:00
  • You use a toggle, like a flip flop. `if face-detected and not said:` / `said = True`, along with `if face-not-detected and said:` / `said = False`. – Tim Roberts Mar 15 '21 at 19:12