Using OpenCV to detect face key points with C++

This post is a follow-up on my first post about building a face detector with OpenCV in C++. In this post we will build on the existing code and detect face key points. The result will look like this.

Woman smiling with detected face keypoints overlay

Since we will work with a relatively new version of OpenCV (4.2.0), you might want to go back to the previous post to read more on how to install the necessary packages.

The code is on my github.

Let’s get going!

Detecting face key points

After detecting faces in the last post, we now want to detect face key points. We use the cv::face::FacemarkLBF model to find key points in the face rectangles we identified in the last tutorial.

Adding the key point detection model file

As for the face detection model, we have to add a model file for the LBF model. I put the model file in the assets folder of this posts git repo, so you can go there and download it.

For passing the location of this model file to our code, we will use the same CMake target_compile_definitions trick we used in the first post. So make sure you have the model file in the right place and add the following lines to your CMakeLists.txt.

## Introduce preprocessor variables to keep paths of asset files 

A class for the key point detector

We start by adding a class for the key point detector. This way we have the code for initializing and calling the model in one place.


In the include folder, we create a file KeyPointDetector.h. This will be the header file for the key point detector.

The KeyPointDetector will have two public methods. The first is a constructor. We will use the constructor to initialize the underlying LBF model.

The second method detect_key_points detects face key points within a given rectangle in an image. As the key points for each face are of type std::vector<cv::Point2f>, this function will return a vector of std::vector<cv::Point2f>.

The header file for the key point detector will look like this.

#include <opencv4/opencv2/face.hpp>
class KeyPointDetector { 
     /// Constructor
     explicit KeyPointDetector();
      /// Detect face key points within a rectangle inside an image     
      /// \param face_rectangles Rectangles that contain faces
     /// \param image Image in which we want to detect key points
     /// \return List of face keypoints for each face rectangle
     detect_key_points(const std::vector<cv::Rect> &face_rectangles,
                       const cv::Mat &image) const;
     cv::Ptr<cv::face::Facemark> facemark_;

#code #image-processing #opencv #cpp #machine-learning #cplusplus

What is GEEK

Buddha Community

Using OpenCV to detect face key points with C++
Chando Dhar

Chando Dhar


Deep Learning Project : Real Time Object Detection in Python & Opencv

Real Time Object Detection in Python And OpenCV

Github Link:

Blog Link:

I’m on Instagram as @knowledge_doctor.

Follow Me On Instagram :

Like My Facebook Page:

#python project #object detection #python opencv #opencv object detection #object detection in python #python opencv for object detection

Pass method as parameter using C# | Delegates in C# | C# Bangla Tutorial | Advanced C#

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #pass method as parameter using c#

Ari  Bogisich

Ari Bogisich


Using isdigit() in C/C++

In this article, we’ll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let’s look at how to use this function, using some simple examples.

#c programming #c++ #c #c#

Ari  Bogisich

Ari Bogisich


A Guide to using the strdup() function in C/C++

In this article, we’ll take a look at using the strdup() function in C/C++.

The strdup() function is very useful if you want to duplicate the contents of a string onto another string.

Let’s see how we can utilize this function, using some simple examples.

#c programming #c++ #c #c#

Tamale  Moses

Tamale Moses


How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++