-1

How do I leave the webcam open and do the face detect with haar cascade for only a few seconds?

I have a function and this function returns true if the face detect of a face has been carried out, but it must not do it immediately as soon as it detects it, rather it must do it only after the face has been detected for at least 3 seconds for example.

If I use the time module and do the wait, obviously this will simply slow down the execution of my program and consequently also that of the cv2.VideoCapture, seeing the jerky webcam.

Here is the code:

import cv2

def face_detect():
    video_capture = cv2.VideoCapture(0)
    while True:
        # Capture frame-by-frame
        ret, frames = video_capture.read()
        gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )
        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
            cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
            return True

if __name__ == '__main__': 
    detected = face_detect()
    if detected == True:
        print("The face is detected. OK")
    else:
        print("I'm sorry but I can't detect your face")
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
bellapetezio
  • 111
  • 1
  • 10
  • use a variable as an accumulator. If you detect a face increment it, if not set it to 0. Then if the variable reaches a certain threshold it means you had been detecting the face for n consecutive readings. If you know how much time each reading takes you can adjust the threshold so n consecutive readings means 3 seconds. This way you don't need delays and the videofeed wont get jerky. –  Dec 17 '21 at 15:02
  • You don't need to put
    in your text. To break the text to a new line, simply separate the text with a new line.
    – Ted Klein Bergman Dec 17 '21 at 15:04
  • @SembeiNorimaki I had already thought of a similar solution, but the problem that the code will run on different PCs and therefore the execution speed can vary – bellapetezio Dec 17 '21 at 15:05
  • Then use a Time library, save the timestamp when the first detection is made, then at each detection check if enough time has passed to consider the continuos detection a positive. –  Dec 17 '21 at 15:08

1 Answers1

1

Simply record the time when the face is detected and see only draw the faces when the face has been detected and the current time stamp is timeout seconds after the recorded time stamp.

import cv2
from time import time

def face_detect(timeout):
    video_capture = cv2.VideoCapture(0)
    start_time    = 0      # Temporary value.
    face_detected = False  # Used to see if we've detected the face.
    while True:
        # Capture frame-by-frame
        ret, frames = video_capture.read()
        gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )

        if not face_detected and faces:
            face_detected = True
            start_time    = time()
        elif not faces:
            face_detected = False  # Reset if we don't find the faces.
        elif face_detected and time() - start_time >= timeout:
            # Draw a rectangle around the faces
            for (x, y, w, h) in faces:
                cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
                return True

if __name__ == '__main__':
    detected = face_detect(timeout=3)
    if detected == True:
        print("The face is detected. OK")
    else:
        print("I'm sorry but I can't detect your face")
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50