Face Landmark Detection using Python

Original Source Here

Face Landmark Detection with Dlib

Dlib is a library for applying machine learning and computer vision solutions. This library is based on the C++ language, but we can use a language like Python for using the library. One of the solutions that we can apply by using this library is face landmark detection. Now let’s get into the implementation.

Install the library

Installing a library can become a problem. If we don’t have a good guide, installing the library can take several days. Dlib is one of them. Because it uses C++ as the primary language, we have to install C++ tools for installing the library.

There are several steps that we should do to install it. Here are the steps:

  • First, install the CMake. You can download the software here. If you are using Windows, please locate the CMake file path first. Then, set the path to the executable path on the environment variable.
  • Then, install Visual Studio with the C++ dependencies to it. You can download the software here. For the dependencies, you can look at this screenshot below:
The screenshot is captured by the author
  • After you install the Visual Studio, the next step is to install the Python. To make your installation simpler, I recommend you for installing Anaconda. You can download it here. For the Python version, I recommend you for using the 3.6.6 version to avoid any errors.
  • Lastly, install the CMake, dlib, and OpenCV library by using pip. Here is the command for doing that:
pip install cmake
pip install dlib
pip install opencv-contrib-python

Import the libraries

After we’ve installed the libraries, the next step is to import them into our code. We will import OpenCV for retrieving inputs from the webcam, NumPy for numerical computation, and Dlib for detecting keypoints from a face.

Here is the code for doing that:

Initialize the objects

Now let’s initialize several variables. There are three must need variables that we will initialize:

  • A detector for detecting one or more faces. We set the dlib.get_frontal_face_detector function inside the variable.
  • A predictor for detecting keypoints from faces. We set the dlib.shape_predictor function inside the variable. This function needs a pretrained model location as the parameter, which you can download here.
  • The cv2.VideoCapture object for capturing images from the webcam. Also, we set a parameter with value 0 for capturing images from a webcam.

Let’s write this code for initializing variables:

Face landmark detection mechanism

As you can see from above, we initialize the face landmark detector by using the pretrained model. The model is based on ensemble regression trees because the model will predict continuous numbers. You can read the details about the model here.

That model is trained on the iBUG-300 W dataset, where it contains images and their corresponding 68 face landmark points. In general, those landmark points belong to the nose, the eyes, the mouth, and the edge of a face. You can download the dataset here.

Here is the visualization of the face landmark locations below:

The image is created by Brandon Amos from CMU that creates OpenFace

Implement the face landmark detection

Now you know how the face landmark detection algorithm works. Now let’s implement the algorithm. For implementing that, you can see the code below along with explanations on each line of code:

By combining all the code as one, now let’s try the code! If the code doesn’t have any errors, the webcam will display the result along with the keypoints. In my case, here is the result:

The image is captured by the author.


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: