部署环境如下:
WIN7+VS2015+dlib19.4+opencv249
示例代码如下:
#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 "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <dlib/opencv.h>
//#include <dlib/opencv/to_open_cv.h>
using namespace dlib;
using namespace std;
using namespace cv;
string int2str(int num) {
stringstream ss;
ss << num;
string str;
ss >> str;
return str;
}
int main(int argc, char** argv)
{
// 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 == 9999)
{
cout << "Call this program like this:" << endl;
cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
system("pause");
return 0;
}
// We need a face detector. We will use this to get bounding boxes for
// each face in an image.
frontal_face_detector detector = get_frontal_face_detector();
// And we also need a shape_predictor. This is the tool that will predict face
// landmark positions given an image and face bounding box. Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
shape_predictor sp;
deserialize("E:\\Workspace\\CppWorkspace2015\\DlibTest\\src\\shape_predictor_68_face_landmarks.dat") >> sp;
string filename = "E:\\TestData\\Vedio\\vedio_480P.mp4";//打开的视频文件
cv::VideoCapture capture(filename);
Mat frame;
while (capture.read(frame))
{
cout << "图像大小为:" << frame.rows << "*" << frame.cols << "\t\t";
dlib::array2d<bgr_pixel> temp;
dlib::assign_image(temp, dlib::cv_image<bgr_pixel>(frame));
long start_det = getTickCount();
std::vector<dlib::rectangle> dets = detector(temp);
long end_det = getTickCount();
cout << "人脸检测用时:" << (end_det - start_det) << "\t";
std::vector<full_object_detection> shapes;
for (unsigned long j = 0; j < dets.size(); ++j)
{
long start_landmark = getTickCount();
full_object_detection shape = sp(temp, dets[j]);
long end_landmark = getTickCount();
cout << "关键点定位用时" << (end_landmark - start_landmark) << endl;
for (unsigned long i = 0; i<shape.num_parts(); i++)
{
point pt = shape.part(i);
int x = pt.x();
int y = pt.y();
cv::line(frame, Point(pt.x(), pt.y()), Point(pt.x(), pt.y()), Scalar(0, 0, 255), 5);
//cv::imshow("img", frame);
}
}
cv::imshow("img", frame);
waitKey(1);
//cout << "detection..." << endl;
}
return 0;
}
其中要注意的是:把opencv::mat格式转换为dlib::cv_image<bgr_pixel>然后再转换为dlib::array2d<bgr_pixel>是尝试了多种方法后最终确定的方法.
时间性能如下:
图像大小如图所示,当前算法支持最小80X80人脸检测,性能如上所示,可以看出人脸检测的时间要远远大于关键点定位的耗时时长。在dlib的人脸检测算法用时较长,而关键点定位算法的模型较大(90M左右),但速度却是非常快的,已经有人将该算法移植到手机上实现。