Search code examples
pythonopencvface-detectionhaar-classifier

Return True if the face is detected at least for 3 seconds


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

Solution

  • 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")