Skip to content

Real-time Face Detection

Date: 2021-05-31 07:30:23, by Pc Ng

#Face Detection #OpenCV

Initialize Camera⚓︎

We can use the VideoCapture function from OpenCV to capture a sequence of images (in other words, video) from a camera attached to our computer. Then, the faces in each image can be detected using the similar method described in the previous post.

First, let's import the OpenCV and initialize the VideoCapture.

import cv2 

# initialize the VideoCapture to capture the video from a camera.
camera = cv2.VideoCapture(0)

Note:

if you have more than one camera attached to your computer, you can set the value to 1 to select another camera.

Load the Cascade Classifier for Face and Eye Detection⚓︎

Similar, we need to load the haar cascade classifier for face detection and eye detection. The haar cascade classifier normally come with the OpenCV package, if you can't locate the files, check out here.

import os 

cv2_base_dir = os.path.dirname(os.path.abspath(cv2.__file__))


# path to haarcascade_frontalcatface.xml and haarcascade_eye.xml
face_xml = os.path.join(cv2_base_dir, 'data', 'haarcascade_frontalface_default.xml').replace('\\', '/')
eye_xml = os.path.join(cv2_base_dir, 'data', 'haarcascade_eye.xml').replace('\\', '/')

# load the files 
face_classifier = cv2.CascadeClassifier(face_xml)
eye_classifier = cv2.CascadeClassifier(eye_xml)

Read the Frame from camera⚓︎

Upon initialize the VideoCapture, we can read the frame from the camera continuously until we deinitialize the camera by calling release() function.

while(True):
    # read frames from the camera
    ret, image = camera.read()

    # check if there is a frame
    if ret == True:

        # perform face detection
        faces = face_classifier.detectMultiScale(image, 1.3)

        for face in faces:
            x, y, width, height = face 

            # draw the bounding box
            cv2.rectangle(
                img= image,
                pt1= (x, y),
                pt2=(x+width, y+height),
                color= (0, 255, 255),
                thickness= 2
            )

        # perform eye detection
        eyes = eye_classifier.detectMultiScale(image, 1.3)

        for eye in eyes:
            x, y, width, height = eye 

            # draw the bounding box 
            cv2.rectangle(
                img= image,
                pt1= (x, y),
                pt2=(x+width, y+height),
                color= (255, 0, 255),
                thickness= 2
            )

        # display the image with bounding boxes
        cv2.imshow('Face and Eye Detection', image)


    # if ESC key is triggered
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break


# deinitialize the camera process
camera.release()
cv2.destroyAllWindows()

Note:

the decimal value for ESC key is 27.



Run the Code⚓︎

when you run the above code, a window named "Face and Eye Detection" will pop up, and the camera will start to capture the image. If face and eyes are detected, bounding box will be drawn to indicate the detected face and eyes.

.