dlib人脸特征点对齐

版权声明:本文为博主原创文章,转载请注明原地址。 https://blog.csdn.net/xiamentingtao/article/details/51396948

前面我们介绍了使用dlib进行人脸检测,下面我们给出如何使用dlib进行人脸特征点检测。我们直接贴出代码。我们的代码包括如下几部分功能:

  • 检测单张图片
  • 检测一个视频
  • 检测一个camera
    先给出代码:

#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 <dlib/image_io.h>
#include <iostream>
#include <string> 
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>




using namespace dlib;
using namespace std;


// ----------------------------------------------------------------------------------------
void PrintHelp();
int FaceDetectionAndAlignment(const char* inputname);

int main(int argc, char** argv)
{
    try
    {
        // This example takes in a shape model file and then a list of images to
        // process.  We will take these filenames in as command line arguments.
        // Dlib comes with example images in the examples/faces folder so give
        // those as arguments to this program.
        if (argc == 1)
        {
            PrintHelp();
            return 0;
        }

        if (strcmp(argv[1],"Demo")==0)
        {
            if (2==argc)
            {
                return FaceDetectionAndAlignment("");
            }
            else if (3==argc)
            {
                return FaceDetectionAndAlignment(argv[2]);
            }

        }
        else
        {
            PrintHelp();
            return 0;
        }

    }
    catch (exception& e)
    {
        cout << "\nexception thrown!" << endl;
        cout << e.what() << endl;
    }
}

// ----------------------------------------------------------------------------------------
void PrintHelp()
{
    cout << "Useage:" << endl;
    cout << "1. test model via a camera: face_alignment.exe  Demo " << endl;
    cout << "2. test model on a pic:     face_alignment.exe  Demo xx.jpg" << endl;  
    cout << "3. test model on a video:   face_alignment.exe  Demo xx.avi" << endl;
    cout << endl;
}
int FaceDetectionAndAlignment(const char* inputname)
{
    string inputName;
    CvCapture* capture = 0; 
    cv::Mat frame, frameCopy, image;
    image_window win;

    frontal_face_detector detector = get_frontal_face_detector();
    shape_predictor pose_model;
    deserialize("D:/dlib/model/shape_predictor_68_face_landmarks.dat") >> pose_model;

    if (inputname != NULL){
        inputName.assign(inputname);
    }

    // name is empty or a number
    if (inputName.empty()){
        capture = cvCaptureFromCAM(0);
        if (!capture){
            cout << "Capture from camera didn't work" << endl;
            return -1;
        }
    }
    // name is not empty
    else if (inputName.size()){
        if (inputName.find(".jpg") != string::npos || inputName.find(".png") != string::npos|| inputName.find(".bmp") != string::npos)
        {
            image = cv::imread(inputName, 1);
            if (image.empty())
            {
                cout << "Read Image fail" << endl;
                return -1;
            }
        }
        else if (inputName.find(".mp4") != string::npos || inputName.find(".avi") != string::npos|| inputName.find(".wmv") != string::npos)
        {
            capture = cvCaptureFromAVI(inputName.c_str());
            if (!capture)
            {
                cout << "Capture from AVI didn't work" << endl;
                return -1;
            }
        }
    }

    // -- 2. Read the video stream
    if (capture!=nullptr){
        cout << "In capture ..." << endl;

        // Grab and process frames until the main window is closed by the user.
        while (!win.is_closed())
        {
            // Grab a frame
            IplImage* iplImg = cvQueryFrame(capture);
            iplImg = cvQueryFrame(capture);
            frame = cv::cvarrToMat(iplImg);
            if (frame.empty())
                break;
            if (iplImg->origin == IPL_ORIGIN_TL) //
                frame.copyTo(frameCopy);
            else
                cv::flip(frame, frameCopy, 0);

            // Turn OpenCV's Mat into something dlib can deal with.  Note that this just
            // wraps the Mat object, it doesn't copy anything.  So cimg is only valid as
            // long as temp is valid.  Also don't do anything to temp that would cause it
            // to reallocate the memory which stores the image as that will make cimg
            // contain dangling pointers.  This basically means you shouldn't modify temp
            // while using cimg.
            cv_image<bgr_pixel> cimg(frameCopy);

            // 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[i]));

            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));

            if (cv::waitKey(10) >= 0)
                goto _cleanup_;
        }

        cv::waitKey(0);

    _cleanup_:
        cvReleaseCapture(&capture);

    }
    else{

        if (!image.empty())
        {
            cout << "In image read" << endl;
            cv_image<bgr_pixel> cimg(image);

            // 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[i]));

            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));

            cout << "Hit enter to exit..." << endl;
            cin.get();
        }

    }

    return 0;


}

再逐一介绍。

检测单张图片

右击项目,选择属性,调试填写:

Demo  E:\\datasets\\helen\\testset\\30427236_1.jpg

如下图:

实验结果:

相当不错的效果了。

检测视频

右击项目,选择属性,调试填写:

Demo  E:\\datasets\\vid.wmv


效果视频地址:
http://7xtmgn.com1.z0.glb.clouddn.com/dlibvideo.mp4

检测camera

右击项目,选择属性,调试填写:

Demo 

效果视频地址:
http://7xtmgn.com1.z0.glb.clouddn.com/dlibcamera.mp4

完整源代码:dlib face_alignment

阅读更多

扫码向博主提问

xiamentingtao

随便问吧,无论学术还是人生
  • 擅长领域:
  • cv
  • math
  • 考博
  • 算法
  • AI
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页