21xrx.com
2024-06-02 22:19:51 Sunday
登录
文章检索 我的文章 写文章
使用C++和Dlib提取人脸特征点并保存为CSV文件
2023-07-05 07:54:28 深夜i     --     --
C++ Dlib 人脸特征点 提取 保存 CSV文件

对于许多计算机视觉应用程序来说,识别和定位人脸是一个基本步骤。而对于识别人脸的任务来说,关键是能够准确地提取人脸特征点。这就是为什么许多开发者都喜欢使用C++和Dlib来实现这一目标的原因。本文将介绍如何使用C++和Dlib提取人脸特征点并保存为CSV文件。

首先,我们需要理解什么是人脸特征点。通俗来说,它们是人脸上的一些具有代表性的特征,如眼睛、鼻子、嘴巴等,这些特征点的组合可唯一地标识一个人的面部特征。在Dlib中,提取人脸特征点的算法使用了人脸部分的特征点模型,并对其进行微调以适应具体的面部特征。

接下来,我们需要准备一些C++代码。当然,在编写此代码之前,我们需要安装和配置Dlib。具体的过程可以查阅Dlib官方文档。在此之后,便可参考以下C++代码:


#include <iostream>

#include <dlib/opencv.h>

#include <opencv2/opencv.hpp>

#include <dlib/image_processing.h>

#include <dlib/image_processing/frontal_face_detector.h>

#include <dlib/image_processing/render_face_detections.h>

#include <fstream>

#include <sstream>

#include <vector>

#include <string>

using namespace std;

using namespace dlib;

using namespace cv;

string windPath = "/mnt/c/Users/Administrator/Desktop/opencv/";

int main()

{

  try

  {

    frontal_face_detector detector = get_frontal_face_detector();

    // Load the shape_predictor

    shape_predictor sp;

    deserialize(windPath + "Dlib/pythonExamples/shape_predictor_68_face_landmarks.dat") >> sp;

    std::vector<string> persoane;

    persoane.push_back("1.jpg");

    for (string persoana : persoane)

    {

      string FileName = persoana.substr(0,persoana.size()-4)+".csv";

      fstream fout(windPath + "Dlib/pythonExamples/outcsv/"+FileName, fstream::out);

      if (fout.fail())

        cout<<"error"<<endl;

      

      //cout << "process " << persoana << endl;

      cv::Mat fot = cv::imread(persoana);

      cv_image<bgr_pixel> cimg(fot);

      // Detect faces

      std::vector<rectangle> faces = detector(cimg);

      cout << "Number of faces detected: " << faces.size() << endl;

      // Find the pose of each face

      std::vector<full_object_detection> shapes;

      for (unsigned long i = 0; i < faces.size(); ++i)

      {

        shapes.push_back(sp(cimg, faces[i]));

      }

      //Display landmarks on face

      image_window win;

      win.clear_overlay();

      win.set_image(cimg);

      win.add_overlay(render_face_detections(shapes));

      for (int i = 0; i < shapes.size(); i++) {

        for (int j = 0; j < shapes[i].num_parts(); j++) {

          fout << shapes[i].part(j).x() << "," << shapes[i].part(j).y() << ";";

        }

        fout << endl;

      }

      fout.close();

    }

  }

  catch (exception& e)

  {

    cout << endl << e.what() << endl;

  }

}

这段代码将读取“1.jpg”的图像文件并将其转换为Dlib接受的图像格式。然后,我们将使用frontal_face_detector检测人脸,并使用shape_predictor标记人脸特征点。最后,使用C++代码将保存这些特征点为CSV文件。可以看到,我们在代码中指定了特征点模型的路径,这是前面提到的Dlib必须配置的部分。

值得一提的是,本代码将会输出特征点的x和y坐标到CSV文件。此外,还可以使用image_window来显示特征点的位置。

在运行代码之后,我们将会在指定目录中看到名为“1.csv”的文件,其输出将类似于这样:

201.15,151.038;206.06,160.346;213.634,168.731;222.238,176.193;233.479,181.427;

如此简单,就实现了提取人脸特征点并将特征点保存为CSV文件的过程。

综上所述,本文介绍了如何使用C++和Dlib提取人脸特征点并将其保存为CSV文件。无论是在人脸识别、活体检测还是表情分析等领域,这些特征点都具有十分重要的作用。希望本文能给想要了解该领域的读者提供一些帮助!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章