Face Identification using Haar cascade classifier

Learn how to develop a face identification system using haar cascade classifier

VAIBHAV HARIRAMANI
Oct 7 · 8 min read
Image for post
Image for post

A facial identification system is a technology capable of identifying a face of a person from a digital image or a video frame from a video source.

Cascade classifier, or namely cascade of boosted classifiers working with haar-like features, is a special case of ensemble learning, called boosting. It typically relies on Adaboost classifiers (and other models such as Real Adaboost, Gentle Adaboost or Logitboost).

Cascade classifiers are trained on a few hundred sample images of image that contain the object we want to detect, and other images that do not contain those images.

How can we detect if a face is there or not ?

  • Haar Feature Selection, features derived from Haar wavelets
  • Create integral image
  • Adaboost Training
  • Cascading Classifiers
Image for post
Image for post

Haar Cascade classifier is based on the Haar Wavelet technique to analyze pixels in the image into squares by function. This uses “integral image” concepts to compute the “features” detected.

Haar cascade classifier is based on the Viola-Jones detection algorithm which is trained in given some input faces and non-faces and training a classifier that identifies a face.

I.1.a. Haar Feature Selection

There are some common features that we find on most common human faces :

  • a bright nose bridge region compared to the eyes
  • some specific location of eyes, mouth, nose…

The characteristics are called Haar Features. The feature extraction process will look like this :

Image for post
Image for post
Haar Features
Image for post
Image for post

Haar features are similar to these convolution kernels which are used to detect the presence of that feature in the given image.

Detect edges using convolution kernels:

Given an input image and convolution kernel, we place kernel to a corner and do convolution multiplication shifting the kernels.

Image for post
Image for post
Image for post
Image for post

This method is used to detect different types of edges using different kernels.

A Haar-Feature is just like a kernel in CNN, except that in a CNN, the values of the kernel are determined by training, while a Haar-Feature is manually determined.

Image for post
Image for post

Here are some Haar-Features. The first two are “edge features”, used to detect edges. The third is a “line feature”, while the fourth is a “four rectangle feature”, most likely used to detect a slanted line.

When haar features are applied to image of a girl.

Each feature results in a single value which is calculated by subtracting the sum of pixels under a white rectangle from the sum of pixels under the black rectangle.

Image for post
Image for post

Every haar feature has some sort of resemblance to identify a part of the face.

Viola-Jones uses 24*24 as base window size and calculates the above features all over the image shifting by 1 PX.

If we consider all possible parameters of the haar features like position, scale, and type we end up calculating about 160,000+ features. So we need to evaluate a huge set of features for every 24*24 PX.

So to avoid this we have an idea to avoid redundant features and pick only those features which are very useful for us. This can be done using AdaBoost.

I.1.b. The integral image

Suppose we want to determine the rectangle features at a given pixel with coordinates (x,y). Then, the integral image of the pixel in the sum of the pixels above and to the left of the given pixel.

Image for post
Image for post

where ii(x,y) is the integral image and i(x,y) is the original image.

When you compute the whole integral image, there is a form a recurrence which requires only one pass over the original image. Indeed, we can define the following pair of recurrences :

Image for post
Image for post

where s(x,y) is the cumulative row sum and and s(x−1)=0, ii(−1,y)=0.

How can that be useful ? Well, consider a region D for which we would like to estimate the sum of the pixels. We have defined 3 other regions : A, B and C.

  • The value of the integral image at point 1 is the sum of the pixels in rectangle A.
  • The value at point 2 is A + B
  • The value at point 3 is A + C
  • The value at point 4 is A + B + C + D.

Therefore, the sum of pixels in region D can simply be computed as : 4+1−(2+3).

And over a single pass, we have computed the value inside a rectangle using only 4 array references.

Image for post
Image for post

I.1c. Learning the classification function with Adaboost

AdaBoost is used to remove redundant features and choose only relevant features.

Image for post
Image for post
For example, feature detecting a vertical edge is useful detecting a nose but irrelevant detecting a lip.

Given a set of labeled training images (positive or negative), Adaboost is used to :

  • select a small set of features
  • and train the classifier

AdaBoost is used to get the best features among all these 160,000+ features. These features are also called as weak classifiers. After these features are found a weighted combination of all these features in use in evaluating and deciding any given window has a face or not.

I.1.d. Cascading Classifier

The key idea is to reject sub-windows that do not contain faces while identifying regions that do. Since the task is to identify properly the face, we want to minimize the false negative rate, i.e the sub-windows that contain a face and have not been identified as such.

A series of classifiers are applied to every sub-window. These classifiers are simple decision trees :

  • if the first classifier is positive, we move on to the second
  • if the second classifier is positive, we move on to the third

Any negative result at some point leads to a rejection of the sub-window as potentially containing a face. The initial classifier eliminates most negative examples at a low computational cost, and the following classifiers eliminate additional negative examples but require more computational effort.

Image for post
Image for post

The classifiers are trained using Adaboost and adjusting the threshold to minimize the false rate. When training such model, the variables are the following :

  • the number of classifier stages
  • the number of features in each stage
  • the threshold of each stage

The job of each stage is used to determine whether a given sub window is definitely not a face or may be a face. A given sub window is immediately discarded as not a face if it fails in any of the stage.

Luckily in OpenCV, this whole model is already pre-trained for face detection.

The viola-Jones face detection algorithm is trained and weights are stored in the disk. All we do is take the features from the file and apply to our image, if the face is present in the image we get the face location.

Image for post
Image for post
cat face detected using viola-Jones face detection algorithm in openCV.

You can find the face detection implementation by OpenCV “haarcascade_frontalface_default.xml” file here.

Further Use

To See the implementation of Haar cascade classifier and face recognition using OpenCV in python Checkout this blog here.

Results

Drawbacks of Haar Cascade Classifiers

Image for post
Image for post
Figure : Example of falsely detecting a face in an image. This is a common problem when using Haar cascade classifiers.

In order to detect faces/humans/objects/whatever in OpenCV (and remove the false positives), you’ll spend a lot of time tuning the cv2.detectMultiScale parameters. And again, there is no guarantee that the exact same parameters will work from image-to-image. This makes batch-processing large datasets for face detection a tedious task since you’ll be very concerned with either (1) falsely detecting faces or (2) missing faces entirely, simply due to poor parameter choices on a per image basis.

To troubleshoot this issue we have Histogram of Oriented Gradients approach about which we will discuss in new blog untill then stay tuned..

Here is The original paper was published in 2001 by Viola & Jones.

This article was originally published on my personal blog : https://vaibhavhariramani.blogspot.com/2020/04/a-full-guide-to-face-detection.html

The Github repository of this article (and all the others from my blog) can be found here :

vaibhavhariaramani / FaceDetection

This repo contains exercises, code, tutorials and articles of my personal blog — geekyBawa/Computer_Vision

Do Checkout My other Blogs

Thank You for reading

Please give 👏🏻 Claps if you like the blog.

GEEKY BAWA

just a silly Geek who love to seek out new technologies and experience cool stuff.

VAIBHAV HARIRAMANI

Written by

Hi there! I am Vaibhav Hariramani a Travel & Tech blogger who love to seek out new technologies and experience cool stuff.

GEEKY BAWA

Hi there! I am VAIBHAV HARIRAMANI (AKA GEEKY BAWA) Tech blogger who love to seek out new technologies and experience cool stuff.

VAIBHAV HARIRAMANI

Written by

Hi there! I am Vaibhav Hariramani a Travel & Tech blogger who love to seek out new technologies and experience cool stuff.

GEEKY BAWA

Hi there! I am VAIBHAV HARIRAMANI (AKA GEEKY BAWA) Tech blogger who love to seek out new technologies and experience cool stuff.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store