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 
... 
set(KEY_POINT_DETECTION_MODEL 
  "${PROJECT_SOURCE_DIR}/assets/lbfmodel.yaml") 
... 
target_compile_definitions(${PROJECT_NAME} 
  PRIVATE KEY_POINT_DETECTION_MODEL="${KEY_POINT_DETECTION_MODEL}")

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.

KeyPointDetector.h

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.

#ifndef KEYPOINTDETECTOR_H 
#define KEYPOINTDETECTOR_H  
#include <opencv4/opencv2/face.hpp>
class KeyPointDetector { 
public:
     /// 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
     std::vector<std::vector<cv::Point2f>>
     detect_key_points(const std::vector<cv::Rect> &face_rectangles,
                       const cv::Mat &image) const;
  private:
     cv::Ptr<cv::face::Facemark> facemark_;
 };
#endif //KEYPOINTDETECTOR_H

#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

1619799996

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

Real Time Object Detection in Python And OpenCV

Github Link: https://github.com/Chando0185/Object_Detection

Blog Link: https://knowledgedoctor37.blogspot.com/#

I’m on Instagram as @knowledge_doctor.

Follow Me On Instagram :
https://www.instagram.com/invites/contact/?i=f9n3ongbu8ma&utm_content=jresydt

Like My Facebook Page:

https://www.facebook.com/Knowledge-Doctor-Programming-114082097010409/

#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#

https://youtu.be/GfcTSJf5Rc8

#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

1589816580

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

1590565090

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

1624240146

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++