TowardsMachineLearning

Face Detection using OpenCV

Introduction:-

In the previous OpenCV articles, we have discussed the various functions in OpenCV that are very much useful in building a computer vision model. In this article we will be discussing the Application of some of those function, we will build a face detector classifier using OpenCV.

So if you don’t have much knowledge of OpenCV, then I will suggest you to go through the previous articles i.e. Introduction to OpenCV and Important Functions of OpenCV . These articles will help you to get the good understanding on OpenCV library & its really awesome features.

So let’s start with the basic understanding of the face detection,

Face detection is a technique that identifies or locates human faces in images. Face detection is performed by the classifier. The classifier returns the probability whether the face is present or not. The classifier need to be trained on thousands of images with and without faces in order to work accurately.

In this article we are not going to train any classifier but instead we are using the pre-trained classifier available in OpenCV.

OpenCV has two pre-trained classifier which are:

  • Haar classifier
  • Local binary pattern( LBP classifier)

In this article, however, we will only discuss the Haar Classifier.

Haar Classifier-

Harr classifier is more sensitive to noise as compare to LBP classifier or other deep learning algorithms but still, OpenCV’s Haar cascades have its own place because:

  • They are lightweight
  • They are super fast, even on resource-constrained devices
  • The Haar cascade model size is tiny (930 KB)

Haar feature-based cascade classifiers-

Haar feature-based cascade classifier is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001. It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

Haar-like features are digital image features used in object recognition. Some of the haar features are:

Source : https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html

The main satges in algorithms are described briefly:

Haar features’ extraction-

 We extract the Haar-features from the data we fed into the classifier. Haar- features are just like our convolutional kernel. Each feature is a single value obtained by subtracting sum of pixels under the white rectangle from sum of pixels under the black rectangle

‘Integral Image’ concept- 

An Integral image is where each pixel represents the cumulative sum of a corresponding input pixel with all pixels above and left of the input pixel. This algorithm enables rapid calculation of summations over image sub-regions. Any rectangular subset of such sub-region can be evaluated in constant time.

This concept is adopted to reduce the calculations for a given pixel to an operation involving just four pixels. Nice, isn’t it? It makes things super-fast. For more understanding of this concept check the Research paper.

Adaboost- 

A machine learning algorithm called Adaboost is used to only select those features that help to improve the classifier accuracy.

Cascading Classifiers-

In an image, most of the image is non-face region. So it is a better idea to have a simple method to check if a window is not a face region. If it is not, discard it in a single shot, and don’t process it again. Instead, focus on regions where there can be a face. This way, we spend more time checking possible face regions.

For this they introduced the concept of Cascade of Classifiers. Instead of applying all 6000 features on a window, the features are grouped into different stages of classifiers and applied one-by-one. if a window fails the first stage, discard it. If it passes, apply the second stage of features and continue the process. The window which passes all stages is a face region.

Image:- Cascade Structure of Haar Classifier
Source : https://www.researchgate.net/figure/Cascade-structure-for-Haar-classifiers_fig9_277929875

The Paul- Viola algorithm can be visualized by the following gif-

Face Detection using Python and OpenCV :-

Now we have an intuition behind the working of the Haar feature-based cascade classifiers .So, let us start the implementation part-

The image will be displayed like below-

Converting the image to Gray scale-

We will convert the image into a grayscale since for the face detection we should not bother about the skin tone, but rather on the haar-features, this gray scale conversion will help us to reduce the computational calculation .So, we will apply the algorithm on gray scale image.

Load the classifier for face detection-

OpenCV comes with a lot of pre-trained classifiers. For instance, there are classifiers for smile, eyes, face, etc. These come in the form of xml files and are located in the opencv/data/haarcascades  git-hub repository.

Download the xml files and place them in the data folder in the same working directory as the jupyter notebook.

In our implementation we are using haarcascade_frontalface_default.xml. Since, we are interested in front face of human. You can play with other cascades also.

We shall be using the detectMultiscale module of the classifier. This function will return a rectangle with coordinates(x,y,w,h) around the detected face. This function has three important parameters which have to be tuned according to the data.

Let’s understand each parameter used in the algorithm-

scaleFactor-

How much the image size is reduced at each image scale. This value is used to create the scale pyramid. In a group photo, there may be some faces which are near the camera than others. Naturally, such faces would appear more prominent than the ones behind. This factor compensates for that. A value of 1.1 indicates that we are reducing the size of the image by 10% at each level in the pyramid.

 

minNeighbors-

The cascade classifier will detect multiple windows around a face. This parameter controls how many rectangles (neighbors) need to be detected for the window to be labeled a face.

minSize-

A tuple of width and height (in pixels) indicating the window’s minimum size. Bounding boxes smaller than this size are ignored. It is a good idea to start with (30, 30) and fine-tune from there.

Image:- Input Image1
Image:- Input Image2

 

 

 

 

 

 

 

 

 

 

In the Input Image2 we can observe that there are few misclassifications in this case, but still this algo. has its own advantages which I specified earlier.

Now, What about eyes and smile other things? We have many cascades available in the repository of OpenCV. Implement yourself and check the result. You can also try real time face detection using your webcam so just explore the computer vision realm.

Congrats, we detected faces in the different input images successfully !!

References :-

 

 

 

 

 

Leave a Comment