This website is made possible by displaying online advertisements to our visitors.
Please consider supporting us by disabling your ad blocker.

Sponsored

Smile Detection with Deep Learning Course

How to Detect Face landmarks with Dlib, Python, and OpenCV

July 4 2022 Yacine Rouizi
Image Processing Computer Vision Face Detection Dlib
How to Detect Face landmarks with Dlib, Python, and OpenCV

In this tutorial, we will see how to detect 68 facial landmarks in an image using Python, OpenCV, and the Dlib library.

The Dlib library is a C++ toolkit containing machine learning algorithms and tools for making practical applications.

What are Facial Landmarks?

Face landmarks are points on a face that represent facial features such as eyes, nose, mouth, etc. These facial landmarks can be used for different applications such as head pose estimation, face swapping, face alignment, and more.

Sponsored

Dlib's 68 Face Features

Dlib provides a pre-trained facial landmark detector that can detect 68 points on a face. 

The image below shows the location of these 68 points:

Facial landmarks 68 markup

As you can see in the image above, each facial feature is mapped with a set of points. If we want, for example, to locate a month in the face, we can use the points from 49 to 68.

Since we are going to use a pre-trained face landmark detector, we need to download the file shape_predictor_68_face_landmarks.dat for that.

Sponsored

Facial Landmark Detection

If you don't have Dlib on your system, you can install it using pip:

pip install dlib

Now create a new Python file and let's detect facial landmarks:

import cv2
import dlib

# load the face detector and shape predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

The first thing we need to do is to import the required packages and load dlib's face detector and facial landmark predictor.

Next, let's load our image:

image = cv2.imread("images/3.jpg")
image = cv2.resize(image, (600, 500))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect the faces
rects = detector(gray)

After loading our image, we resize it, convert it to grayscale, and pass our grayscale image to the dlib's face detector.

rects contains the bounding boxes of the detected faces (the (x, y) coordinates of each bounding box).

We can loop over the rects object, draw a rectangle around each detected face, and apply our facial landmark detector to the face ROI:

# go through the face bounding boxes
for rect in rects:
    # extract the coordinates of the bounding box
    x1 = rect.left()
    y1 = rect.top()
    x2 = rect.right()
    y2 = rect.bottom()

    cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 3)

    # apply the shape predictor to the face ROI
    shape = predictor(gray, rect)

We loop over the face bounding boxes, extract the coordinates of each bounding box, and draw a rectangle around each detected face.

We then apply our facial landmarks detector to the face region.

We can now draw the facial landmarks:

    for n in range(0, 68):
        x = shape.part(n).x
        y = shape.part(n).y
        cv2.circle(image, (x, y), 4, (255, 0, 0), -1)
        
cv2.imshow("Image", image)
cv2.waitKey(0)

We loop over the 68 facial landmarks and draw them on the image.

The image below shows the detected face and facial landmarks:

Face landmark detection

Sponsored

Summary

In this post, you learned about facial landmarks, what they are used for, and how to detect them using the Dlib library.

If you want to learn more about computer vision and image processing then check out my course Computer Vision and Image Processing with OpenCV and Python.

The final code used in this tutorial is available on GitHub in my repository.

If you have any questions or want to say something, please leave a comment in the section below.

Previous Article
Smile Detection with Python, OpenCV, and Deep Learning

Smile Detection with Python, OpenCV, and Deep Learning

Next Article
Bubble Sheet Multiple Choice Test with OpenCV and Python

Bubble Sheet Multiple Choice Test with OpenCV and Python

Subscribe

Join the mailing list to be notified about new posts and updates.

Leave a comment

(Your email address will not be published)