请选择 进入手机版 | 继续访问电脑版
查看: 1938|回复: 3

Dlib提取人脸特征点(68点,opencv画图)

[复制链接]

38

主题

84

帖子

243

积分

中级会员

Rank: 3Rank: 3

积分
243
发表于 2018-12-3 18:29:52 | 显示全部楼层 |阅读模式
惯例先放效果图吧:

2.JPG



接着就是简单粗暴的代码:
//@zmdsjtu@163.com
//2016-12-4
//http://blog.csdn.net/zmdsjtu/article/details/53454071
#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>

using namespace dlib;
using namespace std;

int main()
{
try
{
  cv::VideoCapture cap(0);
  if (!cap.isOpened())
  {
   cerr << "Unable to connect to camera" << endl;
   return 1;
  }

  //image_window win;

  // Load face detection and pose estimation models.
  frontal_face_detector detector = get_frontal_face_detector();
  shape_predictor pose_model;
  deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

  // Grab and process frames until the main window is closed by the user.
  while (cv::waitKey(30) != 27)
  {
   // Grab a frame
   cv::Mat temp;
   cap >> temp;

   cv_image<bgr_pixel> cimg(temp);
   // Detect faces
   std::vector<rectangle> faces = detector(cimg);
   // Find the pose of each face.
   std::vector<full_object_detection> shapes;
   for (unsigned long i = 0; i < faces.size(); ++i)
    shapes.push_back(pose_model(cimg, faces));

   if (!shapes.empty()) {
    for (int i = 0; i < 68; i++) {
     circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
     // shapes[0].part(i).x();//68个
    }
   }
   //Display it all on the screen
   imshow("Dlib特征点", temp);

  }
}
catch (serialization_error& e)
{
  cout << "You need dlib's default face landmarking model file to run this example." << endl;
  cout << "You can get it from the following URL: " << endl;
  cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
  cout << endl << e.what() << endl;
}
catch (exception& e)
{
  cout << e.what() << endl;
}
}
来看下上面那段代码,所有的需要的特征点都存储在Shapes里。仔细看看下面这行代码:
circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);

可以看到shpes[0]代表的是第一个人(可以同时检测到很多个人),part(i)代表的是第i个特征点,x()和y()是访问特征点坐标的途径。

每个特征点的编号如下:
在上述画图的基础上加了如下一行代码:
putText(temp, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0),1,4);

效果图:
1.JPG

对照着上图,比如说想获取鼻尖的坐标,那么横坐标就是shapes[0].part[30].x(),其余的类似。

在这个的基础上就可以做很多有意思的事情啦,2333

最后祝大家开发愉快:)
---------------------
作者:朱铭德
来源:CSDN
原文:https://blog.csdn.net/zmdsjtu/article/details/53454071
版权声明:本文为博主原创文章,转载请附上博文链接!

回复

使用道具 举报

665

主题

1234

帖子

6561

积分

xdtech

Rank: 5Rank: 5

积分
6561
发表于 2018-12-23 17:45:23 | 显示全部楼层
dlib越来越强大了
这效果
厉害
回复

使用道具 举报

166

主题

616

帖子

1万

积分

xdtech

Rank: 5Rank: 5

积分
10667
发表于 2019-1-25 11:57:19 | 显示全部楼层
68个点
少了一点
最好是128特征点
更准确一些
回复

使用道具 举报

166

主题

616

帖子

1万

积分

xdtech

Rank: 5Rank: 5

积分
10667
发表于 2019-1-25 11:57:39 | 显示全部楼层
现在dlib的使用越发广泛了
深度学习方法
也被整合进去了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表