Dlib提取人脸特征点

转自https://blog.csdn.net/zmdsjtu/article/details/53454071?ticket=ST-3153-47Plcph2ez6yRnbTZqXP-passport.csdn.net

主要在官网给的Demo基础之上用Opencv把特征点描绘出来了。


很早之前写过一篇配置Dlib环境的博客,现在来稍微梳理下提取特征点的使用方法。

上一篇配置环境博客地址:http://blog.csdn.net/zmdsjtu/article/details/52422847


惯例先放效果图吧:


动图如下:





接着就是简单粗暴的代码:

  1. //@zmdsjtu@163.com
  2. //2016-12-4
  3. //http://blog.csdn.net/zmdsjtu/article/details/53454071
  4. #include <dlib/opencv.h>
  5. #include <opencv2/opencv.hpp>
  6. #include <dlib/image_processing/frontal_face_detector.h>
  7. #include <dlib/image_processing/render_face_detections.h>
  8. #include <dlib/image_processing.h>
  9. #include <dlib/gui_widgets.h>
  10. using namespace dlib;
  11. using namespace std;
  12. int main()
  13. {
  14. try
  15. {
  16. cv:: VideoCapture cap(0);
  17. if (!cap.isOpened())
  18. {
  19. cerr << "Unable to connect to camera" << endl;
  20. return 1;
  21. }
  22. //image_window win;
  23. // Load face detection and pose estimation models.
  24. frontal_face_detector detector = get_frontal_face_detector();
  25. shape_predictor pose_model;
  26. deserialize( "shape_predictor_68_face_landmarks.dat") >> pose_model;
  27. // Grab and process frames until the main window is closed by the user.
  28. while (cv::waitKey( 30) != 27)
  29. {
  30. // Grab a frame
  31. cv::Mat temp;
  32. cap >> temp;
  33. cv_image<bgr_pixel> cimg(temp);
  34. // Detect faces
  35. std:: vector<rectangle> faces = detector(cimg);
  36. // Find the pose of each face.
  37. std:: vector<full_object_detection> shapes;
  38. for ( unsigned long i = 0; i < faces.size(); ++i)
  39. shapes.push_back(pose_model(cimg, faces[i]));
  40. if (!shapes.empty()) {
  41. for ( int i = 0; i < 68; i++) {
  42. circle(temp, cvPoint(shapes[ 0].part(i).x(), shapes[ 0].part(i).y()), 3, cv::Scalar( 0, 0, 255), -1);
  43. // shapes[0].part(i).x();//68个
  44. }
  45. }
  46. //Display it all on the screen
  47. imshow( "Dlib特征点", temp);
  48. }
  49. }
  50. catch (serialization_error& e)
  51. {
  52. cout << "You need dlib's default face landmarking model file to run this example." << endl;
  53. cout << "You can get it from the following URL: " << endl;
  54. cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
  55. cout << endl << e.what() << endl;
  56. }
  57. catch (exception& e)
  58. {
  59. cout << e.what() << endl;
  60. }
  61. }

来看下上面那段代码,所有的需要的特征点都存储在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);


效果图:


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


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


最后祝大家开发愉快:)

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
提取人脸特征点,可以使用OpenCV和dlib库。 使用OpenCV: 1. 加载图像并将其转换为灰度图像。 ``` import cv2 image = cv2.imread('image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ``` 2. 加载人脸检测器。 ``` face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') ``` 3. 使用人脸检测器检测图像中的人脸。 ``` faces = face_cascade.detectMultiScale(gray, 1.3, 5) ``` 4. 对于每个检测到的人脸,使用OpenCV的特征点检测器提取特征点。 ``` import numpy as np for (x, y, w, h) in faces: roi_gray = gray[y:y+h, x:x+w] eyes_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') eyes = eyes_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: eye_center = (x + ex + ew // 2, y + ey + eh // 2) cv2.circle(image, eye_center, 2, (255, 0, 0), -1) ``` 使用dlib: 1. 加载图像。 ``` import dlib image = dlib.load_rgb_image('image.jpg') ``` 2. 加载人脸检测器。 ``` detector = dlib.get_frontal_face_detector() ``` 3. 使用人脸检测器检测图像中的人脸。 ``` faces = detector(image, 1) ``` 4. 对于每个检测到的人脸,使用dlib特征点检测器提取特征点。 ``` predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') for face in faces: landmarks = predictor(image, face) for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(image, (x, y), 2, (0, 255, 0), -1) ``` 以上是两种常用的提取人脸特征点的方法,具体实现可以根据自己的需求进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值