Face Detection models and softwares

Source: Deep Learning on Medium

Go to the profile of Achraf KHAZRI


Face detection is a great tool that can be used in different fields such as security and human resources.

You can develop face detection algorithms, there is some different approch (we are going to talk about some of them) or you can just use commercial softwares like :

  • Amazon Rekognition API : this API provides face recognition, emotion detection and motion tracking
  • Microsoft Azure API : provides face recognition, gender detection and face similarity matching

If you dont want pay for this tools, you can use opensource libs like :

  • OpenFace
  • Dlib
  • Opencv

OpenCV Face Detection

OpenCV provides the Haar Feature-based Cascade Classifiers for face detection, this model was presented by Paul Viola and Michael Jones in 2001.

This method apply series of classifiers to every subwindow of input picture, the first one classifier eliminates a large number of non-faces examples with very little processing. The other classifiers eliminate additional negatives but require additional computation. After several stages of processing the number of sub-windows have been reduced radically.

As you can see in the picture below, this architecture uses filters to extract features from image, and those filters became more and more complexe in each stage from one to n,

Let’s try this detector.

import cv2 as cv
# Load Haar Cascade Classifier
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
# Read the image
img = cv.imread('test.jpg')
# Convert image to graysclae
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:


You find the models in OpenCV github repository, Link.

The result :

Dlib Face Detection

Dlib Face recognition uses SVM (Support vector machine) combined with HOG (Histogram of Oriented Gradient) to detect faces.

  • Support Vector Machine (SVM) : Machine learning model proposed by Vladimir N. Vapnik and Alexey Ya. Chervonenkis in 1963. SVM goal is to find an hyperplane that separates classes and have maximum margin between them.
Support Vector Machine

There is multiple types of SVM and that depends on the kernel type; Linear, RBF and Polynominial.

Implementation of the HOG descriptor algorithm is as follows:

  1. Divide the image into small connected regions called cells, and for each cell compute a histogram of gradient directions or edge orientations for the pixels within the cell.
  2. Discretize each cell into angular bins according to the gradient orientation.
  3. Each cell’s pixel contributes weighted gradient to its corresponding angular bin.
  4. Groups of adjacent cells are considered as spatial regions called blocks. The grouping of cells into a block is the basis for grouping and normalization of histograms.
  5. Normalized group of histograms represents the block histogram. The set of these block histograms represents the descriptor.

The following figure demonstrates the algorithm implementation scheme:

Histogram of Oriented Gradient

So to the detection, a window with dimension n*m (No matter what dimension, just put in mind that you have to normalize that dimension when clasifiying because it have to be the same dimension with the input of the SVM, or to get rid off all normalizing stuff, just use the same dimension of the SVM input wich is your training data dimension) slide on your picture , and for each window w apply hog to extract features and after that w apply SVM classifier to predict if it is a face or not. Like shown in the picture below.

Let’s try Dlib :

import face_recognition
import cv2
# Load the jpg file into a numpy array
image = cv2.imread("test.jpg")
# Find all the faces in the image
face_locations = face_recognition.face_locations(image)
# Get number of faces
number_of_faces = len(face_locations)
print("Number of face(s) in this image {}.".format(number_of_faces))
for face_location in face_locations:
# Get coord
x, y, z, w = face_location
 # Draw Face rectangle
# Show image

And the result look like this example :

Deep Learning approches for Face Detection

Deep learning represents a big evolution in the world of AI, and in computer vision we have new and powerful deep models for object detection such as Faster RCNN, Yolo and SSD.

You can use one of those models to build your face detection software, all what you have to do is making a small dataset that contains 100 or 200 pictures with the ground truth of the face (You can use Labelimg) and then retrain a pretrained model model and get your face detector.

For the Faster RCNN and SSD you can use Tensorflow Object Detection API, and get pretrained models from here.

And for the Yolo, you can use Darknet to train the pretrained model on your custom dataset.

Papers links :