Face Detection and Landmarks using dlib and OpenCV

Vignesh Suresh
4 min readJan 23, 2021

--

The world will be taken over by Artificial Intelligence very soon . Face Applications include identification of faces from videos or digital images. It is a good example of Computer Vision. For Identification with better accuracy and confidential value, the faces need to be detected properly. Face Detection Technology is used in applications to detect faces from digital images and videos. Also, just detecting the face will not help. We need more information about the face, i.e. whether a person smiles, laughs, or dimples seen while smiling etc. In short, facial expressions too give us information. In order to get more information about the face, we take the help of Facial Landmarks.

What are Facial Landmarks?

Facial landmarks are used for localizing and representing salient regions or facial parts of the person’s face, such as:

Eyebrows

Eyes

Jaws

Nose

Mouth etc.

Facial landmarks is a technique which can be applied to applications like face alignment, head pose estimation, face swapping, blink detection, drowsiness detection, etc. In this context of facial landmarks, our vital aim is to detect facial structures on the person’s face using a method called shape prediction.

Facial Landmarks Detection has 2 steps:

To detect the key facial structures on the person’s face.

It involves localizing the face in the image.

We can do Face detection in a number of ways. We can use OpenCV’s built-in Haar Cascade XML files or even TensorFlow or using Keras. Over here especially , We need to apply a HOG (Histogram of Gradients) and Linear SVM (Support Vector Machines) object detector specifically for the task of face detection. We can also do it using Deep Learning-based algorithms which are built for face localization. Also, The algorithm will be used for the detection of the faces in the image. We can obtain face bounding box through some method for which we use the (x, y) coordinates of the face in the image respectively.

We do have a variety of facial landmark detectors, but every method will essentially be trying to localize and also labelling the following facial regions will be done .

Nose

Jaws

Left eye

Right eye

Left eyebrow

Mouth

Right eyebrow

The facial landmark detector which is pre-trained inside the dlib library of python for detecting landmarks, is used to estimate the location of 68 points or (x, y) coordinates which map to the facial structures. These indexes of 68 coordinates or points can be easily visualized on the image below:

Face Landmark representation

The Locations of the Facial Parts are as follows:

The left eye is accessed with points [42, 47].

The mouth is accessed through points [48, 67].

The left eyebrow is accessed through points [22, 26].

The nose is accessed using points [27, 34].

The right eyebrow is accessed through points [17, 21].

The right eye is accessed using points [36, 41].

And the jaw is accessed via points [0, 16].

Following are the steps for Implementation of Face Landmarks Detection

Install Python 3. Also Spyder terminal, Jupyter Notebook or Pycharm Editor recommended.

Install libraries imutils, argparse, numpy, dlib and cv2-contrib-python and cv2-python using pip(Windows) and sudo apt for Linux.

Download and Install OpenCV 3 or above.

Download the dlib shape predictor. It is a file with .dat extension.

Implementation of Facial Detection with Facial Landmarks using Python

Execution of the Program

Open the Command prompt. Go to the path where this program is saved. Also save the image for landmark detection of faces in the same path or you can save the image in another folder but that folder should be saved in the same path.

Syntax of command:-

python filename.py shape-predictor shape_predictor_68_face_landmarks.dat image image_folder_name/image_name.file extension (This is if the image is saved in a separate folder )

Syntax for the command:-

python filename.py shape-predictor shape_predictor_68_face_landmarks.dat image image.file extension. (This is if the image is saved in a same path where program is saved)

Insert the command for the execution.

Here, the image is saved in the same path but separate folder.

python facial_landmarks.py ––shape-predictor shape_predictor_68_face_landmarks.dat image images/gehlert.jpg

Command Prompt Execution

Output:-

Pixabay Image.

As seen in the Output, the Landmarks are shown in red color dots and the Face Detection is in Cyan color box drawn around the face. (Note:- The above steps for execution works for Windows and Linux.)

References:-

Hope you enjoyed.

You can follow me on:-

LinkedIn

Tableau Public

Twitter

Thanks

Vignesh Suresh

--

--

Vignesh Suresh
Vignesh Suresh

Written by Vignesh Suresh

A Tech Lover, Tableau Enthusiast

No responses yet