效果
代码
Vs2017下使用Dlib检测人脸,并通过OpenCv将结果绘制出来。(由于Dlib库已编译好,Vs工程环境自行搭建,OPenCv环境参考本人之前的专栏文章)
#include <iostream>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include "opencv2/opencv.hpp"
int main()
{
char filename[] = "./Face/face2.png";
clock_t start_t = (double)clock();
// 加载人脸检测器
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
dlib::array2d <unsigned char> img;
dlib::load_image(img, filename);
clock_t end_t = (double)clock();
int64_t curTime = 1000 * (end_t - start_t) / (double)CLOCKS_PER_SEC;
std::cout << "rrrr:" << curTime; // 计算初始化耗时
start_t = (double)clock();
std::vector<dlib::rectangle> dets = detector(img);
end_t = (double)clock();
curTime = 1000 * (end_t - start_t) / (double)CLOCKS_PER_SEC;
std::cout << "dddddd:" << curTime; // 计算检测耗时
cv::Mat Mat = cv::imread(filename);
for (std::vector<dlib::rectangle>::iterator it = dets.begin(); it != dets.end(); ++it)
{
dlib::rectangle rect = *it;
unsigned short l = rect.left();
unsigned short u = rect.top();
unsigned short w = rect.width();
unsigned short h = rect.height();
cv::rectangle(Mat, cv::Rect(l, u, w, h), cv::Scalar(255, 0, 0), 2);
}
cv::imshow("Mat", Mat);
cv::waitKey(0);
system("pause");
return 0;
}
关注
笔者 - jxd