机器学习实践系列之1 - dlib

        Dlib 是一个机器学习库,采用C++编写(提供C++和python接口),里面包含 许多常用机器学习算法。

        Dlib 目前已更新到 V19.2。下载及文档可以参见 官网地址: http://www.dlib.net/ml.html

一. 编译

        下载的dlib库解压后,需要通过CMake进行编译,在windows下作者采用的是 CMake3.5.1。

        选择 源码位置(source code)和 编译位置(binaries)后 依次选择configure->Generate进行编译和生成(编译后生成一个文件 dlib.lib)。

        机器学习实践系列之1 - dlib_第1张图片

        注:V19.2版本需要C++11支持,对应VS2015以上版本


二. 测试例

        dlib 提供了丰富的算法例子,包含 3D点云、SURF特征、贝叶斯分类、SVM、深度学习、多种回归 等算法,也包含Thread、Timer、XML、Socket、Sqlite 等 底层基本工具,有时间的话大家可以仔细跑一遍例子看看。

        机器学习实践系列之1 - dlib_第2张图片

        作为入门级实例,作者采用 Face LandmarkDetection人脸对齐的例子进行说明(当然有很多人喜欢 上面的Face Detection或者GUI)。

        人脸对齐的代码比较简单,通过注释也能够看到其采用的是 HOG+线性分类器人脸对齐 采用了 2014年的一篇CVPR:

        One Millisecond Face Alignment with an Ensemble of Regression Trees by Vahid Kazemi and Josephine Sullivan, CVPR 2014

        PS:这篇文章号称速度极快(单人脸关键点耗时约为1ms),简称 1MS

        还有一篇文章 LBF(Face Alignment at 3000 FPS via Regressing Local Binary Features),也是基于回归的方法,简称 3000FPS

        程序中直接加载了训练好的模型,当然 你也可以训练自己的模型(参考 train_shape_predictor_ex.cpp),先上代码:

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This example program shows how to find frontal human faces in an image and
    estimate their pose.  The pose takes the form of 68 landmarks.  These are
    points on the face such as the corners of the mouth, along the eyebrows, on
    the eyes, and so forth.  

    This face detector is made using the classic Histogram of Oriented
    Gradients (HOG) feature combined with a linear classifier, an image pyramid,
    and sliding window detection scheme.  The pose estimator was created by
    using dlib's implementation of the paper:
        One Millisecond Face Alignment with an Ensemble of Regression Trees by
        Vahid Kazemi and Josephine Sullivan, CVPR 2014
    and was trained on the iBUG 300-W face landmark dataset.  

    Also, note that you can train your own models using dlib's machine learning
    tools.  See train_shape_predictor_ex.cpp to see an example.

    Finally, note that the face detector is fastest when compiled with at least
    SSE2 instructions enabled.  So if you are using a PC with an Intel or AMD
    chip then you should enable at least SSE2 instructions.  If you are using
    cmake to compile this program you can enable them by using one of the
    following commands when you create the build project:
        cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON
        cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON
        cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON
    This will set the appropriate compiler options for GCC, clang, Visual
    Studio, or the Intel compiler.  If you are using another compiler then you
    need to consult your compiler's manual to determine how to enable these
    instructions.  Note that AVX is the fastest but requires a CPU from at least
    2011.  SSE4 is the next fastest and is supported by most current machines.  
*/

#include 
#include 
#include 
#include 
#include 
#include 

using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv)
{  
    try
    {
        // 需要一个形状模型文件(标记68个点) 和一系列的图片(examples/faces文件夹下)
        if (argc == 1)
        {
            cout << "命令行调用方法:" << endl;
            cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
            cout << "模型下载地址:" << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
            return 0;
        }

        // 需要一个人脸检测器,获取人脸外框
        frontal_face_detector detector = get_frontal_face_detector();

        // 然后需要一个形状预测器,用来预测已知的脸部外框的标记点位置
        // 我们仅仅从shape_predictor_68_face_landmarks.dat文件加载模型  
        shape_predictor sp;
        deserialize(argv[1]) >> sp;

        image_window win, win_faces;
        // 加载所有图像
        for (int i = 2; i < argc; ++i)
        {
            cout << "处理图像 " << argv[i] << endl;
            array2d img;
            load_image(img, argv[i]);
            // 放大图片,确保能检测到尺寸较小的人脸
            pyramid_up(img);

            // 检测图像中的所有人脸
            std::vector dets = detector(img);
            cout << "检测到的人脸数量: " << dets.size() << endl;

            // 调用形状预测器,获取每个人脸的姿态
            std::vector shapes;
            for (unsigned long j = 0; j < dets.size(); ++j)
            {
                full_object_detection shape = sp(img, dets[j]);
                cout << "Parts个数:"<< shape.num_parts() << endl;
                cout << "pixel position of first part:  "<< shape.part(0) << endl;
                cout << "pixel position of second part: "<< shape.part(1) << endl;
                // 你可以得到所有的Face Part,这里我们把它保存到shapes里,用于后面的绘制
                shapes.push_back(shape);
            }

            // 绘制姿态检测结果
            win.clear_overlay();
            win.set_image(img);
            win.add_overlay(render_face_detections(shapes));

            // 我们也能提取每张剪裁后的人脸的副本,旋转到恰当位置,然后缩放到一个标准尺寸
            dlib::array > face_chips;
            extract_image_chips(img, get_face_chip_details(shapes), face_chips);
            win_faces.set_image(tile_images(face_chips));

            cout << "回车处理下一张图片..." << endl;
            cin.get();
        }
    }
    catch (exception& e)
    {
        cout << "\n 异常!" << endl;
        cout << e.what() << endl;
    }
}

        编译成功后下载训练好的 模型文件(61M):shape_predictor_68_face_landmarks.dat.bz2

        文件解压后放在生成的exe根目录下,将 dlib-19.2\examples\faces 文件夹也放在exe目录下,修改工作目录为:$(SolutionDir)$(Platform)\$(Configuration)\

        添加命令参数(shape_predictor_68_face_landmarks.dat faces/2007_007763.jpg),运行程序(右键-属性-调试-命令参数),看一下检测效果:

机器学习实践系列之1 - dlib_第3张图片

你可能感兴趣的:(机器学习,计算机视觉)