机器学习实践系列之1 - dlib
2017-01-15 13:07
274 查看
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)。
![](http://img.blog.csdn.net/20170621225314613)
注:V19.2版本需要C++11支持,对应VS2015以上版本。
二. 测试例
dlib 提供了丰富的算法例子,包含 3D点云、SURF特征、贝叶斯分类、SVM、深度学习、多种回归 等算法,也包含Thread、Timer、XML、Socket、Sqlite 等 底层基本工具,有时间的话大家可以仔细跑一遍例子看看。
![](http://img.blog.csdn.net/20170621225319981)
作为入门级实例,作者采用 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),先上代码:
编译成功后下载训练好的 模型文件(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),运行程序(右键-属性-调试-命令参数),看一下检测效果:
Dlib 目前已更新到 V19.2。下载及文档可以参见 官网地址: http://www.dlib.net/ml.html
一. 编译
下载的dlib库解压后,需要通过CMake进行编译,在windows下作者采用的是 CMake3.5.1。
选择 源码位置(source code)和 编译位置(binaries)后 依次选择configure->Generate进行编译和生成(编译后生成一个文件 dlib.lib)。
注:V19.2版本需要C++11支持,对应VS2015以上版本。
二. 测试例
dlib 提供了丰富的算法例子,包含 3D点云、SURF特征、贝叶斯分类、SVM、深度学习、多种回归 等算法,也包含Thread、Timer、XML、Socket、Sqlite 等 底层基本工具,有时间的话大家可以仔细跑一遍例子看看。
作为入门级实例,作者采用 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 <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> 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<rgb_pixel> img; load_image(img, argv[i]); // 放大图片,确保能检测到尺寸较小的人脸 pyramid_up(img); // 检测图像中的所有人脸 std::vector<rectangle> dets = detector(img); cout << "检测到的人脸数量: " << dets.size() << endl; // 调用形状预测器,获取每个人脸的姿态 std::vector<full_object_detection> 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<array2d<rgb_pixel> > 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),运行程序(右键-属性-调试-命令参数),看一下检测效果:
相关文章推荐
- 机器学习实践系列之3 - 人脸对齐(上)
- 机器学习实践系列(一)普通线性回归
- 机器学习实践系列之4 - 人脸对齐(下)
- 机器学习实践系列之5 - 目标跟踪
- 机器学习实践系列 1 线性代数计算的python实现
- 机器学习实践系列之6 - OpenCV实战光流
- 机器学习实践系列之7 - 车辆检测
- 机器学习三人行(系列七)----支持向量机实践指南(附代码)
- 机器学习实践系列之8 - 人眼定位
- 机器学习实践系列(二)岭回归(Ridge Regression)
- 机器学习实践系列之13 - OpenCV之图像去雾
- 机器学习实践系列之9 - 视频结构化
- 机器学习实践系列之14 - OpenCV之傅里叶变换
- 机器学习实践系列(三)LASSO回归
- 机器学习实践系列之5 - 目标跟踪
- 机器学习实践系列之15 - OpenCV之MSER
- 机器学习实践系列之2 - GPUImage
- 机器学习实践系列之16 - OpenCV之手写体识别
- 【机器学习系列】机器学习界大牛林达华推荐的书籍
- 机器学习系列(1)_逻辑回归初步