(1)下载并解压Dlib
下载链接 http://dlib.net/
(2)设置环境变量
此步可以省略,不过后面就需要写全路径了。
新建环境变量:
DLIB = D:\Program Files\dlib-19.0\,其中“D:\Program Files\dlib-19.0”是我的解压目录
(3)建立VS2013空项目
3.1 设置包含目录
右键项目工程->属性->C/C++->常规->附加包含目录
如果没有像我一样设置环境变量的话,就需要把上面的$(DLIB)替换成你的解压目录。
3.2 设置预处理器定义
这里是为了添加对JPG和PNG图片的支持。
右键项目工程->属性->C/C++->预处理器->预处理器定义
右键项目工程->属性->链接器->常规->附加库目录
3.4 添加源文件
只需要添加两个源文件:
dlib\all\source.cpp
examples\face_detection_ex.cpp
3.5 关闭SDL检查
当SDL检查启用时,编译器会严格检测缓冲区的溢出,这将导致一些函数编译失败。
项目属性->配置属性->C/C++->SDL检查,选测否。
3.5 添加资源文件
将下面三个文件夹下的所有文件添加到资源文件中
dlib\external\libjpeg
dlib\external\libpng
dlib\external\zlib
(4)检测人脸关键点
#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#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>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
//image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while (cv::waitKey(30) != 27)
{
// Grab a frame
cv::Mat temp;
cap >> temp;
cv_image<bgr_pixel> cimg(temp);
// 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]));
if (!shapes.empty()) {
for (int i = 0; i < 68; i++) {
circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
// shapes[0].part(i).x();//68个
}
}
//Display it all on the screen
imshow("Dlib特征点", temp);
}
}
catch (serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
其中的问题:
Debug模式成功后,Release模式失败,原因是没有将Release配置文件进行配置,以及应该在“配置属性-->C/C++-->代码生成-->运行时库”中将“多线程(/MT)或者(/MD)”统一改为“多线程调试(/MTd)”。
添加源文件,要把source和face的源文件直接添加到源文件列表中