Face Detection using MTCNN

In this post I will show how to use MTCNN to extract faces and features from pictures.

Saransh Rajput
3 min readSep 1, 2020

What are MTCNN????

MTCNN or Multi-Task Cascaded Convolutional Neural Networks is a neural network which detects faces and facial landmarks on images. It was published in 2016 by Zhang et al.

MTCNN is one of the most popular and most accurate face detection tools today. It consists of 3 neural networks connected in a cascade.

It is an implementation of the MTCNN face detector for Keras in Python3.4+. It is written from scratch, using as a reference the implementation of MTCNN from David Sandberg (FaceNet’s MTCNN) in Facenet. It is based on the paper Zhang, K et al. (2016) [ZHANG2016].

To install MTCNN

pip install mtcnn

Now lets get to the real task

Code

Result

Here we have got the box around the face.

Detecting different faces at once.

Libraries:

matplotlib: For plotting of images and boxes.

mtcnn: It is an implementation of the MTCNN face detector for Keras in Python3.4+.

Workflow of the code

— Image Location

— Reading the image

— Creating an object of mtcnn

— Feeding the image through the detector to get the resulting box

— Plotting the box on the image

Inside the draw_facebox() function:

A lot of it is self-explanatory, but it basically returns coordinates, or the pixel values of a rectangle where the MTCNN algorithm detected faces. The “box” value above returns the location of the whole face, followed by a “confidence” level.

If you want to do more advanced extractions or algorithms, you will have access to other facial landmarks, called “keypoints” as well. Namely the MTCNN model located the eyes, mouth and nose as well!

Plotting the face box along with the keypoints of eyes, nose and mouth

Here we are just retracting the key value points from result and placing those points on the image.

Result

Here we have got the box around the face along with the keypoints like eyes, mouth and nose.

Github: https://github.com/saranshrajput/

References:

--

--