Modern-Day Face Detection with the OpenCV Library


Pedro Miranda

Pedro Miranda
Fullstack Developer

INSCALE Article - Modern-Day Face Detection with the OpenCV Library

Face detection is a specific area of computer vision and image processing. It is not a new technology but only in the recent years has it been commercially acceptable due to the significant advances in processing speed of microchips and GPUs. Face detection can be used in many ways such as tagging friends in images, focus in a face or even unlock our smartphones.

But facial detection is not 100% flawless, there is a lot of differences present across human faces such as pose, expression, position and orientation and concerning the picture itself there is the lighting and image resolution.

Real-time face detection
The first real-time face detector was introduced with the Viola-Jones algorithm in 2001 and it segments the photo in multiple subsections and tries to find haar-like features inside each subarea. Performance can be improved by using cascade and adaptative boosting. Although this algorithm reveals a very high accuracy detecting faces, it still contains a lot of false positives and fortunately today there are better alternatives such as Support Vector Machines, Naïve Bayes Classifiers, Deep Neural Networks (DNN), etc. We explored the DNN for facial recognition using the OpenCV library.

OpenCV’s DNN Module
OpenCV is an open source computer vision and machine learning library made in C++, but you can easily find wrappers in Python, Java or C# and in this case we chose the Python wrapper. It contains out-of-the-box Haar cascades, but since version 3.3 there is pre-trained deep learning face detector. The DNN face detector is based on the Single Shot Detector (SSD) framework using a ResNet-10 like base Network.

In practice
In order to use DNN face detector in OpenCV, you first need to download the Caffe files from the OpenCV repository, the deploy.prototxt file defines the network architecture and res10_300x300_ssd_iter_140000.caffemodel has the weights of the layers.

The first step is to load the neural network:

import numpy as np

import cv2

network = cv2.dnn.readNetFromCaffe(“deploy.prototxt”, “res10_300x300_ssd_iter_140000.caffemodel”)

Now, we can read the image file and extract the object that we are going to use as input in our network. It is very important to use 300×300 since the network is expecting this size:

img = cv2.imread(“path to image here”)

(height, width) = img.shape[:2]

blob = cv2.dnn.blobFromImage(cv2.resize(img, (300,300)), 1.0, (300,300) (104.0, 177.0, 123.0))


Finally, we run the input through our network, check if there are any detections and for each detection the correspondent confidence is extracted; if the confidence of the detections meets the confidence threshold (in this case we set the threshold to 0.5) the detection is accepted and the (x, y) coordinates are extracted from the box object and draw a box around the detected faces and show it.

detections = net.forward()

for i in range(0, detections.shape[2]):

confidence = detections[0, 0, i, 2]

if confidence > 0.5:

box = detections[0, 0, i, 3:7] * np.array([width, height, width, height])

(startX, startY, endX, endY) = box.asType(“int”)

Cv2.rectangle(img, (startX, startY), (endX, endY)), (255, 0, 0), 1)

Cv2.imshow(“Detection window”, img)

Let’s run our code!

face detection - Article 4 Image1 - 1

In conclusion, the DNN face detector from OpenCV is highly accurate and easy to use.

Real-time algorithm can be used in almost every scenario despite of the conditions of the source, the lighting and image resolution, face pose, position, and facial expressions.

OpenCV is a state-of-the-art library with the latest computer vision and machine learning algorithms and it is backed by a strong an active community.