Search code examples
pythonopencvface-detection

OpenCV When face not detected in live feed, exit


So I am trying to make a code where when face is detected from webcam it shows a green square around face. That part is done. What I want to make next is that when face is no longer detected by program that it break the loop and exit program. I tried ways through "if" or "else" or find something online but I was not going anywhere. Is there some way to do it? Here is my code:

import cv2
import os
import time

cascPath = os.path.dirname(
    cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray,
                                         scaleFactor=1.1,
                                         minNeighbors=5,
                                         minSize=(60, 60),
                                         flags=cv2.CASCADE_SCALE_IMAGE)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

Solution

  • How about adding this? Count the number of times no face is detected; break if passes a threshold:

    iter_with_no_faces=0 #put this outside the main loop
    ### put the follwing after updating `faces`
    if len(faces) ==0:
        iter_with_no_faces+=1
    ##  add break condition as this:
    if iter_with_no_faces >100:
        break
    

    you can iter_with_no_faces in the faces loop: iter_with_no_faces=0

    In sum, this might work with slight modification:

    import cv2
    import os
    import time
    
    cascPath = os.path.dirname(
        cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
    faceCascade = cv2.CascadeClassifier(cascPath)
    video_capture = cv2.VideoCapture(0)
    iter_with_no_faces=0 #put this outside the main loop
    while True:
        ret, frame = video_capture.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(gray,
                                             scaleFactor=1.1,
                                             minNeighbors=5,
                                             minSize=(60, 60),
                                             flags=cv2.CASCADE_SCALE_IMAGE)
        for (x,y,w,h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)
    
        if len(faces) ==0:
            iter_with_no_faces+=1
        else:
            iter_with_no_faces=0 # I assume you want to continue program when a face detected for a duration. you can omit else statement
        if iter_with_no_faces >100: #set this threshold to larger or smaller number
            break
        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    video_capture.release()
    cv2.destroyAllWindows()