基于opencv和dlib的人脸特征点检测。

8 篇文章 0 订阅
//设置人脸的标记点
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.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>
#include <opencv2/imgproc/imgproc.hpp>
// #include <iostream>
// #include <opencv2/objdetect/objdetect.hpp>
// #include <fstream>
// #include <stdlib.h>
// #include <opencv2/opencv.hpp>
// #include <locale>
// #include <string>
// #include <vector>

using namespace dlib;
using namespace std;
//using namespace cv;

int main() {

    try {
        cv::VideoCapture cap(0);
        if (!cap.isOpened()) {
            cerr << "Unable to connect to camera" << endl;
            return 1;
        }
        frontal_face_detector detector = get_frontal_face_detector();
        shape_predictor pos_modle;                             //将文件中的模型放置再pos_modle中
        deserialize("/home/xxp/Desktop/face/shape_predictor_68_face_landmarks.dat") >> pos_modle;
        
        while (cv::waitKey(30)!=27)
        {
                                                                //获取一帧
            cv::Mat temp;                                       //将摄像头获取的当前帧图片放入到中间文件中
            cap >> temp;                                        //将其转化为RGB像素图片
            cv_image<bgr_pixel> cimg(temp);                     //开始进行脸部识别
            //std::vector<rectangle> faces = detector(cimg);      //检测人脸
            std::vector<rectangle> faces = detector(cimg);
            std::vector<full_object_detection> shapes;          //发现每一个脸的pos估计
            unsigned faceNumber=faces.size();                   //将所有脸的区域放入集合之中
            for (unsigned i = 0; i < faceNumber; i++)
                shapes.push_back(pos_modle(cimg, faces[i]));    //第i个人的关键点。
            if (!shapes.empty()) {
                int faceNumber = shapes.size();
                for (int j = 0; j < faceNumber; j++)
                {
                    for (int i = 0; i < 68; i++)
                    {                                                
                        cv::circle(temp, cvPoint(shapes[j].part(i).x(), shapes[j].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
                        //cv::putText(temp,to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN,1, cv::Scalar(0, 0, 255));
                    }
                }
            }
            imshow("Dlib标记", temp);
        }
    }
    catch (serialization_error &e) {
        cout << "You need dlib's default face landmarking file to run this example.(你需要添加landmark的bat文件,才可以跑这个实例)" << endl;
            cout << endl << e.what() << endl;
    }
    catch(exception &e){
        cout <<  e.what() << endl;

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值