dlib人脸特征点对齐

标签: 视频 dlib
6652人阅读 评论(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

查看评论

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:42
  • 4683

python dlib学习(七):人脸特征点对齐

前言前面的博客介绍过人脸特征点标定:python dlib学习(二):人脸特征点标定。这次试着使用这些人脸特征点来对人脸进行对齐。程序上代码,程序中使用了python-opencv,事先要配置好环境。...
  • hongbin_xu
  • hongbin_xu
  • 2017-11-12 13:43:02
  • 1239

dlib人脸对齐源码详解

一般的人脸识别应用通常都包括三个过程: 1 人脸detect,这一步主要是定位人脸在图像中的位置,输出人脸的位置矩形框 2 人脸shape predictor,这一步主要是找出眼睛眉毛鼻子嘴巴的6...
  • qq_29573053
  • qq_29573053
  • 2017-11-13 13:51:00
  • 861

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

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

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

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

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

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

python︱利用dlib和opencv实现简单换脸、人脸对齐、关键点定位与画图

这是一个利用dlib进行关键点定位 + opencv处理的人脸对齐、换脸、关键点识别的小demo。原文来自于《Switching Eds: Face swapping with Python, dli...
  • sinat_26917383
  • sinat_26917383
  • 2017-11-17 18:48:12
  • 1636

python dlib 人脸对齐代码

  • 2017年11月12日 13:44
  • 74.01MB
  • 下载

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

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

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

权声明:本文为博主原创文章,未经博主允许不得转载。         本篇博客是Dlib库学习的第三篇---人脸对齐。人脸对齐与人脸检测工程建立与配置基本相同,在此不再赘述。可参照我上一篇博客。闲...
  • BBZZ2
  • BBZZ2
  • 2016-11-22 11:17:07
  • 1500
    个人资料
    持之以恒
    等级:
    访问量: 34万+
    积分: 4754
    排名: 7613
    个人网站
    最新评论