Written By:
12 minute read
OpenCVFace DetectionPythonHaar Cascade

Face Detection using Haar Cascades

Posted in Machine Learning   LAST UPDATED: AUGUST 2, 2020

In this tutorial we will learn how to implement face detection using Haar cascade pre-trained model in OpenCV using Python. The code that we have used in this tutorial is available in the Github repository - https://github.com/shekharpandey89/face_detection_using_haar_cascade

with the following directory structure:

face detection using Haar cascades

Download the code from the GitHub link and unzip it. You will get three files with two directories and one python script. In which cascades directory has haarcascade_frontalface_deafult.xml pre-trained model and images folder has our test.png image.

In this blog, we are going to see how face detection is done in the image because I already have written one blog to detect face features using Dlib's-68 points.

Face detection is a hot topic in computer vision used in the camera to make them detect faces and this also has a wide variety of use cases like in home security systems for surveillance, touchless entry for employees in office, monitoring people at airports, etc.


To understand the concept of face detection, we have to understand the following:

  1. Understand the Viola-Jones algorithm.
  2. Understand the OpenCV built-in function to detect a face on the image.

The Viola-Jones algorithm (also known as Haar cascades) is the most common algorithm in the computer vision field used for face detection on the image. The Viola-Jones algo is used not only to detect faces on images but also we can train the model to detect different objects like cars, buildings, kitchen utensils, fruits, etc.

Understanding Face Detector Method:

The image to be used is divided into different kinds of sub-windows and multiple Haar-like features to compute it at different scales and positions for each sub-window. The main features are selected using the Adaboost algorithm. Then each sub-window is checked for the presence or absence of face using a cascade of classifiers.

Example Haar features:

using haar cascades for face detection

The detection algorithm uses a cascade of classifiers which use Haar-like features. Thus, it is also called the Haar Cascades based detector.

features = sum(pixels in the black area) - sum(pixels in the white area)

face detection using haar cascades

In the figure shown above, when we calculate the second image features, it will give more features because the bridge is lighter than the nearby area. But if the same features, we keep on the head of the face, we will get fewer features. In the third image, we also get more features as it can detect the eye region since the eye region is darker as compared to the region below it.

It should be noted that only a single feature is not capable of detecting faces with high accuracy. But, when many such features vote for the presence or absence of a face, the detection becomes very accurate and robust.

These features have actual real importance in the context of face detection:

  1. Eye regions tend to be darker than cheek regions.

  2. The nose region has more bright pixels than the eye region.

Therefore from the above given five rectangles along with the corresponding difference of sums, we are able to get the features which can classify the face. To detect which features belong to face from the available number of features we use the AdaBoost algorithm to select which ones correspond to facial regions of an image.

As we can imagine, using the above rectangle fixed sliding window on the image across every (x, y) coordinate of an image, followed by computing all those features using Haar to classify the face features. This whole process can be computationally expensive.

To overcome this, Viola and Jones introduced cascades concept. At each stop along the sliding window path, the window must pass a series of tests where each subsequent test is more computationally expensive than the previous one. If any of the test fails, the window is automatically discarded.

The Haar cascade approach is good as they are very fast like Haar features and also they choose the best features of the face using the AdaBoost algorithm.

Most probably we want a solution which can detect faces on images regardless of the image size and scale and using Haar cascade we are getting the same. And finally the Viola-Jones algorithm is able to detect objects in real-time.

Face Detection using Haar Cascade in Python:

Below is the code to detect a face on the image.

# python detect_faces.py --face cascades/haarcascade_frontalface_default.xml --image images/test.png

# import the necessary packages
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--face", required=True, help="Path to where the face cascade model resides")
ap.add_argument("-i", "--image", required=True, help="Path to where the image file resides")
args = vars(ap.parse_args())

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# load the face detector and detect faces in the image
faceDetector = cv2.CascadeClassifier(args["face"])

# Use the below code, if you are using OpenCV version 2.4 (uncomment it)
# faceRegions = faceDetector.detectMultiScale(gray, scaleFactor=1.06, minNeighbors=6,
# 		minSize=(32,32), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

# Use the below code, if you are using OpenCv 3.0+
faceRegions = faceDetector.detectMultiScale(grayImage, scaleFactor=1.06, minNeighbors=6,
		minSize=(32, 32), flags=cv2.CASCADE_SCALE_IMAGE)

print("Total {} face(s) found on the image".format(len(faceRegions)))

# Now we loop over the faces and draw a rectangle 
# around each of the image face which we found.
for (x, y, w, h) in faceRegions:
	cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# show the detected faces on screen
cv2.imshow("Detected Faces", image)

In the above code, on lines 9-12 we are parsing two arguments. The first argument is the --face which is the path of our trained face detector model provided by the OpenCV. This pre-trained face detector is just a .xml file which represents the serialized face detector. The second argument is the --image which is our image path on which we want to detect faces.

On code line 15 and 16, we simply load our image and convert it to greyscale(color of the image has no affect on face detection).

On code line 19, we load our pre-trained OpenCV face detector model using the cv2.CascadeClassifier.

On code line 22 to 30, we are simply getting all the faces on the image with a list of bounding boxes or start and end coordinates (x, y). Now we can see what is the meaning of each parameter here.

  • scaleFactor: This factor is used for image scaling in the pyramid. The value is used to scale pyramid to detect faces at multiple scales in the image. The value 1.06 shows that we reduce image size at each level of 5% in the pyramid.

  • minNeighbors: How many neighbours each window should have for the area in the window to be considered a face. This parameter controls how many rectangles (neighbours) need to be detected for the window to be labelled a face.

  • minSize: This will take value in pixels and as here mention (32, 32) height and width size of the window. So any bounding box smaller than the size of this window will be ignored.

Code line 35, loops over all the detected face and creates a rectangle box.

Code line 39, finally we show our image on the screen.

Run the code:

python detect_faces.py --face cascades/haarcascade_frontalface_default.xml --image images/test.png


face detection using haar cascades

face detection using haar cascades

The above code run on the OpenCV 3.0+ version. But you can also run the above code in the OpenCV version 2.4 which is already mention in the code. For that, you have to uncomment the below line:

faceRegions = faceDetector.detectMultiScale(gray, scaleFactor=1.06, minNeighbors=5,
                  minSize=(32, 32), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

and comment the following:

faceRegions = faceDetector.detectMultiScale(grayImage, scaleFactor=1.06, minNeighbors=5,
                  minSize=(32, 32), flags=cv2.CASCADE_SCALE_IMAGE)


Here in this tutorial, we learned how the Viola-Jones/Haar cascade algorithm work to detect faces on the image. We learn how we can use the OpenCV pre-trained model for our purpose. The Haar cascade is quite fast but also it has two shortcomings which are given below:

  1. Parameter tuning: We need to tune parameter detectMultiScale if we wish to detect many images. But the main issue of this is that, when we apply bulk images for face detection, we are not able to inspect all images for face detection.

  2. Highly prone to false positives: meaning that faces are detected when there really aren't any faces there! Again, this problem can be fixed by tuning the parameters of detectMultiScale on a case-by-case basis.