Dlib官网地址:http://dlib.net/
Dlib移植到Android:http://blog.csdn.net/brightming/article/details/50595977
Dlib人脸对齐(特征点检测):http://blog.csdn.net/sunshine_in_moon/article/details/50150435
1、解压Dlib;
E:\Dlib\dlib-19.1
E:\Dlib\dlib-19.1-debug
2、CMake;
3、VS2013+Dlib;
#include "stdafx.h"
#include <iostream>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
#include <fstream>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
frontal_face_detector detector = get_frontal_face_detector();//定义一个frontal_face_detctor类的实例detector,用get_frontal_face_detector函数初始化该实例
image_window win;//一个显示窗口
array2d<unsigned char> img;
load_image(img, "33.jpg");// 加载一张图片,从argv[i](图片路劲)加载到变量img
pyramid_up(img);//对图像进行上采用,检测更小的人脸
// Now tell the face detector to give us a list of bounding boxes
// around all the faces it can find in the image.
//开始检测,返回一系列的边界框
std::vector<rectangle> dets = detector(img);//detector()函数检测人脸,返回一系列边界盒子
cout << "Number of faces detected: " << dets.size() << endl;//dets.size 人脸数量
// 在原图片上显示结果
win.clear_overlay();
win.set_image(img);
win.add_overlay(dets, rgb_pixel(255, 0, 0));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
#include "stdafx.h"
#include <iostream>
#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 <fstream>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
frontal_face_detector detector = get_frontal_face_detector();//定义一个frontal_face_detctor类的实例detector,用get_frontal_face_detector函数初始化该实例
image_window win, win_faces;//一个显示窗口
shape_predictor sp;//定义个shape_predictor类的实例
deserialize("shape_predictor_68_face_landmarks.dat") >> sp;
// Loop over all the images provided on the command line.
// 循环所有的图片
array2d<unsigned char> img;
load_image(img, "02.jpg");// 加载一张图片,从argv[i](图片路劲)加载到变量img
pyramid_up(img);//对图像进行上采用,检测更小的人脸
// Now tell the face detector to give us a list of bounding boxes
// around all the faces in the image.
std::vector<rectangle> dets = detector(img);//检测人脸,获得边界框
cout << "Number of faces detected: " << dets.size() << endl;//检测到人脸的数量
// Now we will go ask the shape_predictor to tell us the pose of
// each face we detected.
//****调用shape_predictor类函数,返回每张人脸的姿势
std::vector<full_object_detection> shapes;//注意形状变量的类型,full_object_detection
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);//预测姿势,注意输入是两个,一个是图片,另一个是从该图片检测到的边界框
cout << "number of parts: " << shape.num_parts() << endl;
//cout << "pixel position of first part: " << shape.part(0) << endl;//获得第一个点的坐标,注意第一个点是从0开始的
//cout << "pixel position of second part: " << shape.part(1) << endl;//获得第二个点的坐标
/*自己改写,打印出全部68个点*/
for (int i = 1; i < 69; i++)
{
cout << "第 " << i << " 个点的坐标: " << shape.part(i - 1) << endl;
}
// You get the idea, you can get all the face part locations if
// you want them. Here we just store them in shapes so we can
// put them on the screen.
shapes.push_back(shape);
}
// Now let's view our face poses on the screen.
//**** 显示结果
win.clear_overlay();
win.set_image(img);
win.add_overlay(render_face_detections(shapes));
// We can also extract copies of each face that are cropped, rotated upright,
// and scaled to a standard size as shown here:
//****我们也能提取每张剪裁后的人脸的副本,旋转和缩放到一个标准尺寸
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 << "Hit enter to process the next image..." << endl;
cin.get();
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
#include "stdafx.h"
#include <iostream>
#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>
#include <dlib/image_io.h>
#include <iostream>
#include <fstream>
using namespace dlib;
using namespace std;
using namespace cv;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
char img_file[] = "02.jpg";
char landmark_file[] = "shape_predictor_68_face_landmarks.dat";
//
Mat img = imread(img_file);
//
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize(landmark_file) >> sp;
array2d<rgb_pixel> arrImg;
load_image(arrImg, img_file);
std::vector<dlib::rectangle> dets = detector(arrImg);
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(arrImg, dets[j]);
for (unsigned long i = 0; i<shape.num_parts(); i++)
{
point pt = shape.part(i);
int x = pt.x();
int y = pt.y();
line(img, Point(pt.x(), pt.y()), Point(pt.x(), pt.y()), Scalar(0, 0, 255), 2);
}
}
//
imshow("img", img);
waitKey();
return 0;
}
Dlib使用OpenCV的Mat格式
#include "stdafx.h"
#include <iostream>
#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>
#include <dlib/image_io.h>
#include <dlib/opencv.h>
#include <iostream>
#include <fstream>
using namespace dlib;
using namespace std;
using namespace cv;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
char img_file[] = "02.jpg";
char landmark_file[] = "shape_predictor_68_face_landmarks.dat";
//
//Mat img = imread(img_file);
namedWindow("img", 1);
Mat img;
VideoCapture cap("Duan2.mp4");
int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
cap >> img;
int w = img.cols;
int h = img.rows;
cout << w << ", " << h << endl;
//
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize(landmark_file) >> sp;
for (int nFrmNum = 0; nFrmNum < totalFrameNumber; nFrmNum++) {
// get frame from the video
cap >> img;
cout << nFrmNum << " =======================" << endl;
if (!img.empty())
{
cv_image<bgr_pixel> cimg(img);
// Detect faces
std::vector<dlib::rectangle> dets = detector(cimg);
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(cimg, dets[j]);
for (unsigned long i = 0; i < shape.num_parts(); i++)
{
point pt = shape.part(i);
int x = pt.x();
int y = pt.y();
line(img, Point(pt.x(), pt.y()), Point(pt.x(), pt.y()), Scalar(0, 0, 255), 2);
char ptr[20];
sprintf(ptr, "%d", i);
putText(img, ptr, Point(pt.x(), pt.y()), 1, 1.0, Scalar(0, 0, 255));
}
}
imshow("img", img);
waitKey(1);
}
}
}
catch (exception& e)
{
cout << e.what() << endl;
}
waitKey();
return 0;
}
Taily老段的微信公众号,欢迎交流学习
https://blog.csdn.net/taily_duan/article/details/81214815