Dlib 实现人脸的68点检测

Posted 喜欢雨天的我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dlib 实现人脸的68点检测相关的知识,希望对你有一定的参考价值。

Dlib实现68点标定

效果图展示:
使用了Dlib库进行的人脸68点的标定操作,拿到68点后 会很方便通过特征点追加3D挂件。
这里写图片描述


使用前准备:配置VS2015的Dlib库的支持


主要是通过68点的模型进行提取脸部的68点的特征值。(相应细节都已经注释)

//设置人脸的标记点
#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>

//声明dlib的域
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;
        }
        //Load face detection and pos estimation models 加载我们需要的脸部识别和姿态估计模型
        frontal_face_detector detector = get_frontal_face_detector();
        shape_predictor pos_modle;
        //将文件中的模型放置再pos_modle中
        deserialize("shape_predictor_68_face_landmarks.dat") >> pos_modle;

        //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;
            //将其转化为RGB像素图片
            cv_image<bgr_pixel> cimg(temp);
            //开始进行脸部识别
            std::vector<rectangle> faces = detector(cimg);
            //发现每一个脸的pos估计 Find the pose of each face
            std::vector<full_object_detection> shapes;
            unsigned faceNumber=    faces.size();
            //将所有脸的区域放入集合之中
            for (unsigned i = 0; i < faceNumber; i++)
                shapes.push_back(pos_modle(cimg, faces[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));

                    }
                }
            }
            //Display it all on the screen  展示每一帧的图片
            cv::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;

    }



}

以上是关于Dlib 实现人脸的68点检测的主要内容,如果未能解决你的问题,请参考以下文章

给定 dlib 的 68 点面部标志,确定它们有多好

python实现人脸关键部位检测(附源码)

dlib库包的介绍与使用,opencv+dlib检测人脸框opencv+dlib进行人脸68关键点检测,opencv+dlib实现人脸识别,dlib进行人脸特征聚类dlib视频目标跟踪

图片人脸检测——Dlib版

Python 3.6.3 利用 Dlib 19.7 和 opencv 实现人脸68点定位

基于 dlib 的人脸检测(68关键点)