Face landmarks detection – Opencv with Python

In this lesson, we will see how to identify our face in real-time through Face landmarks detection with OpenCV and Python. We will also be able to define the position of 68 points in our face.

Before going into detail on how to detect the facial landmarks using the Dlib library with Opencv and Python , I recommend following the tutorial on how to install dlib on windows how to install dlib on windows.

Face landmarks detection some code

The first important step for our Face landmarks detection project with OpenCV and Python is to import the necessary libraries for use.

Let’s move on to calling the shape_predictor_68_face_landmarks.dat file which will be used by our script to identify the points in our face. The video stream is still missing, which in my case comes from the webcam.

import cv2
import numpy as np
import dlib

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

As we surely already know, OpenCV parses the video stream Frame by Frame so we use a While loop to apply the processing.

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = detector(gray)

The images must first be transformed with COLOR_BGR2GRAY and then we get the object that contains the points of our face.

Face landmarks detection points extraction

Now only the code remains for extracting the coordinates and drawing the points found. It might seem complicated but thanks to the dlib library all this can be done with a few lines of code.

 for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()
        #cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)

        landmarks = predictor(gray, face)

        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            cv2.circle(frame, (x, y), 4, (255, 0, 0), -1)

I hope the explanation was useful to you and surely if you download the ready-to-use code you will be able to understand the concept even better. In any case, I refer you to the official OpenCV document: Face landmarks detection Opencv .


Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE