关闭

dlib人脸特征点对齐

标签: 视频dlib
6037人阅读 评论(12) 收藏 举报
分类:

前面我们介绍了使用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

1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Dlib人脸检测+对齐

1,dlib库的安装 准备工具: (1)cmake,https://cmake.org/download/ (2)dlib库,http://dlib.net/,19.2的版本需要vs2015才可以支持...
  • qq_14845119
  • qq_14845119
  • 2016-12-21 10:53
  • 3755

Dlib机器学习库学习系列三----人脸对齐(特征点检测)

本篇博客是Dlib库学习的第三篇---人脸对齐。人脸对齐与人脸检测工程建立与配置基本相同,在此不再赘述。可参照我上一篇博客。闲话少说,来点干货。      步骤一:建立并配置工程,参照上一篇博客。 ...
  • Sunshine_in_Moon
  • Sunshine_in_Moon
  • 2015-12-02 16:51
  • 16260

使用Dlib库进行人脸检测,人脸对齐和人脸识别

简介在之前的博客中,我已经介绍了如何使用dlib-18.17进行人脸检测和人脸对齐。 Windows10+VS2013环境下Dlib库的编译与使用-邬小阳 使用Dlib库进行人脸检测与对齐-邬小阳...
  • u013078356
  • u013078356
  • 2017-04-20 09:12
  • 6120

Dlib人脸特征点检测(速度优化)

Dlib
  • Leo_812
  • Leo_812
  • 2016-07-18 21:25
  • 15637

使用Dlib库进行68个人脸特征点检测

dlib人脸检测共可检测出68个检测点 官网上的例子:http://dlib.net/face_landmark_detection_ex.cpp.html 进行适当的改写。 其中:D:\Ope...
  • liukang325
  • liukang325
  • 2017-02-15 17:54
  • 3694

DLib中调用人脸对齐+VS2013

第一步: 下载DLib的库,解压在 D:\Program Files\下 第二步: 右键项目工程->属性->C/C++->常规->附加包含目录 D:\Program Files\dlib-19.0 ...
  • wfei101
  • wfei101
  • 2017-05-31 22:04
  • 439

dlib人脸检测68点训练模型

  • 2015-11-22 10:46
  • 61.07MB
  • 下载

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

Dlib+opencv 68点特征点的使用以及绘图。
  • zmdsjtu
  • zmdsjtu
  • 2016-12-04 14:31
  • 20858

Opencv与dlib联合进行人脸关键点检测与识别

前言依赖库:opencv 2.4.9 /dlib 19.0/libfacedetection 本篇不记录如何配置,重点在算法实现上。使用libfacedetection实现人脸区域检测,联合dlib...
  • Mr_Curry
  • Mr_Curry
  • 2016-07-22 14:28
  • 18742

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

主要在官网给的Demo基础之上用OpenCV把特征点描绘出来了。 很早之前写过一篇配置Dlib环境的博客,现在来稍微梳理下提取特征点的使用方法。 上一篇配置环境博客地址:http://...
  • luyaran
  • luyaran
  • 2017-01-03 11:22
  • 2077
    个人资料
    • 访问:282153次
    • 积分:4103
    • 等级:
    • 排名:第8738名
    • 原创:117篇
    • 转载:81篇
    • 译文:8篇
    • 评论:106条
    个人网站
    最新评论