下載文件
Dlib下載
- 官網
http://dlib.net/
只push了最新版本;且最新版本要求vs2015 - github
https://github.com/davisking/dlib
在分支裏可以找到各種版本 - 其它
http://dlib.net/files/
這裏有很多版本,還有人臉數據,以及模型文件等
cmake下載
- 版本數:需要2.8及以上
- 官網:https://cmake.org/download/
OpenCV
關於OpenCV就不在贅述了,很簡單;
C++相關配置:利用CMake編譯
- 首先在解壓後的dlib根目錄下新建一個文件夾,我這裏命名爲build2013x64,因爲我這裏使用vs2013,64位
- 如果你的vs是32位,請選擇win32,64位則選擇win64
- 選擇完畢,點擊‘Configure’,出現configure done,再點擊‘Generate’
編譯完成。 - 點擊‘Open Project’,打開工程
- 如果你是64位,記得一定要將設備管理器平臺改爲x64
- 找到ALLBuild,右鍵工程名,點擊‘生成’
- 這個過程中報了一條錯誤:basetsd.h(72): error C2371: “INT32”: 重定義;不同的基類型
- 解決方法:參考這篇article
http://blog.csdn.net/zhangxiangweide/article/details/76718686 - 重新生成,ok, 沒有問題,打開你的dlib下你剛創建的文件夾,找到debug文件夾,會看到生成有dlib的lib文件:dlib.lib,這個文件就是我們要用的庫文件
dlib的使用
vs2013的相關配置:OpenCV和Dlib
- 包含目錄
- 庫目錄
- 附加依賴項
測試:人臉檢測及對齊
#include "stdafx.h"
#include <iostream>
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
dlib::shape_predictor pose_model;
dlib::deserialize("D:\\zhlWorkDocs\\dl\\faceRecognition\\detection\\faceDetect\\Dlib\\shape_predictor_68_face_landmarks.dat") >> pose_model;
cv::Mat img = imread("D:\\zhlWorkDocs\\testWorksDemo\\detectface\\faceRecognition\\test_image\\test.jpg", 1);
dlib::cv_image<dlib::bgr_pixel> cimg(img);
double t = GetTickCount();
vector<dlib::rectangle> faces = detector(cimg);
cout << "dlib detect face time is: " << (GetTickCount() - t) / getTickFrequency() << endl;
vector<dlib::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 j = 0; j < shapes.size(); j++)
{
for (int i = 0; i < 68; i++)
{
circle(img, cv::Point(shapes[j].part(i).x(), shapes[j].part(i).y()),
3, Scalar(0, 255, 0), -1);
//putText(img, to_string(i), cv::Point(shapes[0].part(i).x(), shapes[0].part(i).y()),
// CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1, Scalar(0, 0, 255), 1, 4);
}
}
}
imshow("show_point", img);
waitKey();
return 0;
}
運行結果:
從結果上來看,Dlib的效果其實並不是想象中的那麼好,漏檢率很高,只檢測到了10個人臉,顯然對大傾斜角度,遮擋半邊臉的這種情況無法很好檢測,相比於MTCNN的效果差了好多,但是速度比MTCNN快,對這幅圖而言,檢測時間爲12個毫秒;果然是魚和熊掌不可兼得啊。