Face Detection using Haar Cascades


Here we're going to discuss an interesting and a classical application of Computer Vision: Object Detection. We'll do face and eye detection with OpenCV using Haar Cascades. In order to do object recognition/detection with cascade files, you first need cascade files. For the extremely popular tasks, these already exist. Detecting things like faces, eyes, smiles, cars, and license plates for example are all pretty popular and Haar Cascades of these are readily available.

You can find such popular cascade files from https://github.com/Itseez/opencv/tree/master/data/haarcascades

You can also use Google to find various Haar Cascades of things you may want to detect. We will use a Face cascade and Eye cascade. You can find a few more at the root directory of Haar cascades. Note the license for using/distributing these Haar Cascades.

Now we are doing face and eye detection, and thus we need face and eye haar cascade files.

You can download these files from:

Just right click on raw and save the link in the same directory of your python file.

First we need to load the required XML classifiers. Then load our input image (or video) in grayscale mode.

In [ ]:
import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('ij.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Now we find the faces in the image. If faces are found, it returns the positions of detected faces as Rect(x,y,w,h). The function detectMultiScale() basically finds faces in the images. You can find the full description on how the cascade classifier works, here: http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html

Once we get these locations, we can create a ROI for the face and apply eye detection on this ROI (since eyes are always on the face! ). Most eye detection uses the surrounding skin, eye lids, eye lashes, and eye brows to also make the detection. Thus, our next step is to break down the faces first, before getting to the eyes:

In [ ]:
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

# break down the faces first, before getting to the eyes:

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]

    # Here, we're finding faces, their sizes, drawing rectangles, and noting the ROI.
    

    eyes = eye_cascade.detectMultiScale(roi_gray)

    # If we find those, we'll go ahead and make some more rectangles.
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    
cv2.imshow('img',img)

The Complete Code:

In [ ]:
import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')


img = cv2.imread('ij.jpg')
#ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    # break down the faces first, before getting to the eyes:

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]

        # Here, we're finding faces, their sizes, drawing rectangles, and noting the ROI.
        # Next, we poke around for some eyes:

    eyes = eye_cascade.detectMultiScale(roi_gray)
        # If we find those, we'll go ahead and make some more rectangles.
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    
cv2.imshow('img',img)

The resulting images with face and eyes detected would look like:


Pretty cool right? :D

However you might have noticed sometimes some eyes in your images might have been left out. Also sometimes mouths might also be detected pretty often as eyes. Facial hair and other things can often fool this basic face detection, and even advanced. Skin color can also cause trouble, as we often try to simplify images as much as possible, thus losing a lot of color values.

This was about face and eye recognition. If you want to take a step futher you can actually train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Its full details are given here: Cascade Classifier Training.