Skip to content

Tutorials

Face landmarks detection – Opencv with Python

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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 .

33 comments

          1. I do it but it don’t work
            RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
            [ WARN:0] global D:\Build\OpenCV\opencv-4.1.2\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback

  1. Please help, my computer is giving the following error:
    RuntimeError Traceback (most recent call last)
    in
    6
    7 detector = dlib.get_frontal_face_detector()
    —-> 8 predictor = dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat”)
    9
    10 while True:

    RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat

  2. I did not understand how dat file works. Is it originally a .py file or totally different? How can I build my own
    shape_predictor_68_face_landmarks.dat file to fully understand how it works?
    Thank You

  3. Hi thank you for your good work. i am getting below error. would you advise ? i am new to this dilb

    Traceback (most recent call last):
    File “C:/Users/User/Desktop/py3 face detection/Database.py”, line 30, in
    cv2.imshow(“Frame”, frame)
    cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’

  4. dlib was successfully installed. However, when I used dlib.get_frontal_face_detector(), I got an attribute error as follows:
    AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
    I searched on StackOverflow and github, but didn’t find a satisfactory solution.
    Any kind of help would be appreciated. Thank you!

  5. Hi
    I don’t speak english sorry
    mon probleme ce situe sur “_” du “_, frame = cap.read()” AttributeError:’NoneType’
    someone help me please!
    Thanks you

Join the discussion