Thursday, December 5, 2019

Complete Guide for Face Detection Methods | Working | dlib | HaarCascade

In the last few years, facial recognition has kept an important idea and it is being appreciated all over the world. In many places, it has been used to solve many real-world problems to make life easier. With the marvelous increase in video and image databases, there is an incredible need for automatic understanding and examination of information by the intelligent system as manually it is getting to be plainly distant. Human beings have not tremendous ability to identify different faces than machines. So, an automatic face detection system places an important role in face recognition, facial expression recognition, head-pose estimation, human-computer interaction, etc. 


face detection


The method of face detection in pictures is complicated because of variability present across human faces such as pose, expression, position and orientation, skin color, the presence of glasses or facial hair, differences in camera gain, lighting conditions, and image resolution.

Object detection is one of the computer technologies, which connected to image processing and computer vision and it interacts with detecting instances of an object such as human faces, building, tree, car, etc. The primary aim of the face detection algorithm is to determine whether there is any face in an image or not.

I have often used dlib for face detection and facial landmark detection. The frontal face detector in dlib works really well. It's simple and works great.

The detector is based on the histogram of oriented gradients (HOG) and linear SVM. While the HOG+SVM based face detector has been around for a while and has collected a good number of users, I am not sure how many of us marked the CNN based face detector available in dlib.

The CNN(Convolution Neural Network) based detector is capable of detecting faces almost in all angles. Unfortunately, it is not suitable for real-time videos. It is meant to be executed on a GPU. To get the same speed as an HOG based detector you might need to run on a powerful Nvidia GPU.

Now, I am going to show you how you can use the CNN based face detector form dlib on images and compare the results with HOG based detector with ready to use Python code.

Let's Start

CNN Face Detection with Dlib




Now Let's import the necessary packages. You can install these packages by typing the below command in the terminal.

pip install opencv-python dlib argparse time

You can get the model weights file by typing the command below in the terminal.

wget http://arunponnusamy.com/files/mmod_human_face_detector.dat

You can run the code by typing 

python cnn-facedetection-dlib.py -i <path-to-image-input> -w <path-to-weights-file>

If you find an issue while importing dlib in python 3.6 copy these 2 files from my drive and paste in the site-packages of your environment. The link to my drive is here.


HOG Face Detection with Dlib




You have already installed all the necessary packages required for this.

You can run this code by typing 

python HOG-facedetection-dlib.py -i <path to image input>

For the HOG based one, we don't need to provide any file to initialize. It is pre-built inside dlib. Just calling the method should be enough.

For the CNN based one, we need to provide the weights file to initialize with.

Real-time HOG Face Detector with Dlib




You can run this code by typing 
python realtime-hogfacedetector-dlib.py

Then you will find output like this:


face detection with dlib


Real-time face detection with haar-cascade

Haar Cascade is based on the Haar wavelet technique to analyze pixels in the image into squares by function. This uses machine learning techniques to get a high degree of accuracy from what is called "training data". This uses "integral image" concepts to compute the "features" detected. Haar cascade uses the Adaboost learning algorithm which selects a small no. of important features from a large set to give an efficient result of classifiers.

The code snippet to run this is:




Use this link to get the haarcascade_frontalface_default.xml.

If you run the above code you will see that your face is detected.

If you like this post, don't forget to follow us for the new interesting and latest post.