Face detection using the Haar Cascade Classifier

Nken Allassan
3 min readFeb 15, 2021

A step-back in Level 3 at the National Advance school of Engineering Douala, where i built my first facial recognition system using the concept of Principal component Analysis(PCA) more precisely the viola-jones Algorithm. Today i’m introducing one of the most common “Hello world” project in computer vision: Face detection

family smiling faces

What is computer Vision?

Computer vision is a field of study which deals with how computer see and understand digital images and videos.

Computer vision involves seeing or sensing a visual stimulus, make sense of what it has seen and also extract complex information that could be used for other machine learning activities.

Application of Computer Vision?

Ofcourse as mentioned on this post one of the application of computer vision is face detection, in a global way object detection. Another application is more specific and commonly used in industries as facial recognition systems (will work on it for the next post 😎). In domain of Automobile industries i’m sure you’ve already heard about self driving cars,🤔 which also uses the notion of computer vision (no magic here! 😜).

The commonly used open source library for computer vision is opencv which contains several hundreds of computer vision algorithm.

Getting back to Haar Cascade Classifiers!

Haar Cascade classifier is an effective object detection approach which was proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001.

This is basically a machine learning based approach where a cascade function is trained from a lot of images both positive and negative. Based on the training it is then used to detect the objects in the other images.

The algorithm is based on simple small filters to detect edges (features)on object(images ) we then concatenate the features in the form of Cascade hence the name Cascade Classifiers.

link to picture

As seen on the picture below the feature extraction are done in stages with each stage made up of set of filters (haar-filters).

The informations are then stored in large individual .xml files with a lot of feature sets and each xml corresponds to a very specific type of use case (face detection, eye-glasses detection etc…)

Coding time…

Without talking too much, let’s start coding 🙂....

the code

The image above actually presents a bunch of code for our task (face detection).

So in our case we are working with the“haarcascade_frontalface_default.xml” file(line 14). But before that we need to load frames of images captured by our camera the frame compile together form the video (remember videos are just a compilation of many images(line 10)). Each image is converted into greyscale for it to better fit with the classifier (line 20). The classifier then do the job by detecting all faces found in the image (line 22), draw rectangles on it (line 24–25) and release the entire images in the form of a video (line 27). As simple as that, without entring that much into the mathematical aspect of this concept (Remember it is a hello not goodbye world 😂). The code can be accessed through my github link.

Many post and advanced concept of computer vision coming soon. stay tuned

--

--