Dlib人脸检测+对齐

1dlib库的安装

准备工具:

(1)cmake,https://cmake.org/download/

(2)dlib库,http://dlib.net/,19.2的版本需要vs2015才可以支持,我这里安装的是18.17,http://download.csdn.net/detail/qq_14845119/9717274只需vs2013就可以支持。

 

开始编译,

这里,需要使用cmake编译2个东东,一个是dlib的库,生成vs可以使用的.sln工程,另一个就是examples,同样生成vs可以使用的.sln工程。

where is the source code 和where tobuild the binaries可以参考本人的配置,配置好后,点击Configure,选择vs2013编译器,完毕后,点击Generate,到此就可以在build路径下生成Project.sln,build_examples路径下生成examples.sln

 

用vs2013点击上面的sln分别打开,然后选择ALL_BUILD,右键生成即可。完毕后就可以生成,dlib.lib和各个examples的exe文件。

2,人脸检测+对齐

这里检测用的hog特征进行的人脸检测,本人测试是150MS的样子,

对齐使用的是2014的cvpr,One Millisecond FaceAlignment with an Ensemble of Regression Trees by Vahid Kazemi and JosephineSullivan,对齐速度,本人测试也是150MS的样子。

对齐效果还是很不错的。

训练好的对齐模型需要自行下载,http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2,95M的样子。

VS配置:

vc++目录:

E:\dlib-18.17\

E:\dlib-18.17\dlib\external\libjpeg

C/C++,链接,输入:

dlib.lib

程序代码:

 

#define DLIB_JPEG_SUPPORT
#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>

using namespace dlib;
using namespace std;

int main(int argc, char** argv)
{
	argc = 3;
	argv[1] = "shape_predictor_68_face_landmarks.dat";
	argv[2] = "2008_007676.jpg";

	try
	{
		// 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 == 1)
		{
			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;
			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(argv[1]) >> sp;


		image_window win, win_faces;
		// Loop over all the images provided on the command line.
		for (int i = 2; i < argc; ++i)
		{
			cout << "processing image " << argv[i] << endl;
			array2d<rgb_pixel> img;
			load_image(img, argv[i]);
			// Make the image larger so we can detect small faces.
			pyramid_up(img);

			// Now tell the face detector to give us a list of bounding boxes
			// around all the faces in the image.
			clock_t start2, end2;
			start2 = clock();
			std::vector<rectangle> dets = detector(img);
			end2 = (double)(1000 * (clock() - start2) / CLOCKS_PER_SEC);
			cout << "face_detection_time:" << end2 <<"ms"<< std::endl;

			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.
			std::vector<full_object_detection> shapes;
			for (unsigned long j = 0; j < dets.size(); ++j)
			{
				clock_t start2, end2;
				start2 = clock();
				std::vector<rectangle> dets = detector(img);

				full_object_detection shape = sp(img, dets[j]);

				end2 = (double)(1000 * (clock() - start2) / CLOCKS_PER_SEC);
				cout << "face_aligement_time:" << end2 <<"ms"<< std::endl;

				cout << "number of parts: " << shape.num_parts() << endl;
				cout << "pixel position of first part:  " << shape.part(0) << endl;
				cout << "pixel position of second part: " << shape.part(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;
	}
}

 

实验结果:

 

程序+论文下载链接:

http://download.csdn.net/detail/qq_14845119/9717266

 

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
人脸比对是指将两张人脸图像进行比较,判断它们是否是同一个人。利用dlib、opencv和facenet可以实现人脸比对的功能。 首先,需要用dlib进行人脸检测对齐,再利用opencv将人脸图像转换为facenet所需的格式,最后使用facenet进行人脸特征提取和比对。 以下是一个简单的c++程序,实现了两张人脸图像的比对: ```c++ #include <iostream> #include <dlib/opencv.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include "dlib/image_processing/frontal_face_detector.h" #include "dlib/image_processing.h" #include "dlib/dnn.h" using namespace std; using namespace dlib; // 人脸对齐 void align_face(cv::Mat& img, full_object_detection& shape) { // 人脸对齐点 const int left_eye_points[6] = {36, 37, 38, 39, 40, 41}; const int right_eye_points[6] = {42, 43, 44, 45, 46, 47}; // 获取左右眼平均坐标 cv::Point2d left_eye(0, 0); cv::Point2d right_eye(0, 0); for (int i = 0; i < 6; i++) { left_eye.x += shape.part(left_eye_points[i]).x(); left_eye.y += shape.part(left_eye_points[i]).y(); right_eye.x += shape.part(right_eye_points[i]).x(); right_eye.y += shape.part(right_eye_points[i]).y(); } left_eye.x /= 6; left_eye.y /= 6; right_eye.x /= 6; right_eye.y /= 6; // 计算旋转角度 double angle = atan2(right_eye.y - left_eye.y, right_eye.x - left_eye.x) * 180 / M_PI; // 计算缩放比例 double scale = 1.0 / (right_eye.x - left_eye.x); // 构造仿射变换矩阵 cv::Mat rot_mat = cv::getRotationMatrix2D(left_eye, angle, scale); cv::Mat warp_dst; cv::warpAffine(img, warp_dst, rot_mat, img.size()); // 截取对齐后的人脸 int x = left_eye.x - (right_eye.x - left_eye.x); int y = left_eye.y - (right_eye.y - left_eye.y) / 2; int w = right_eye.x - left_eye.x + (right_eye.x - left_eye.x); int h = right_eye.y - left_eye.y + (right_eye.y - left_eye.y) / 2; cv::Mat aligned_face = warp_dst(cv::Rect(x, y, w, h)); img = aligned_face.clone(); } // 人脸比对 double face_compare(cv::Mat& img1, cv::Mat& img2) { // 加载人脸检测器和关键点检测器 frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("shape_predictor_68_face_landmarks.dat") >> sp; // 检测人脸并对齐 std::vector<rectangle> faces1 = detector(img1); std::vector<rectangle> faces2 = detector(img2); if (faces1.empty() || faces2.empty()) { return -1.0; } full_object_detection shape1 = sp(img1, faces1[0]); full_object_detection shape2 = sp(img2, faces2[0]); align_face(img1, shape1); align_face(img2, shape2); // 转换图像格式 matrix<rgb_pixel> face1; matrix<rgb_pixel> face2; assign_image(face1, cv_image<rgb_pixel>(img1)); assign_image(face2, cv_image<rgb_pixel>(img2)); // 加载facenet模型 anet_type net; deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net; // 提取人脸特征向量 std::vector<matrix<float,0,1>> face1_feats = net(face1); std::vector<matrix<float,0,1>> face2_feats = net(face2); // 计算欧氏距离 double distance = length(face1_feats[0] - face2_feats[0]); return distance; } int main() { // 加载图片 cv::Mat img1 = cv::imread("face1.jpg"); cv::Mat img2 = cv::imread("face2.jpg"); // 人脸比对 double distance = face_compare(img1, img2); // 输出结果 if (distance >= 0) { cout << "distance: " << distance << endl; } else { cout << "no face detected" << endl; } return 0; } ``` 其中,`align_face`函数用于人脸对齐,`face_compare`函数用于人脸比对。两个函数都使用了dlib和opencv库。 在这个程序中,我们使用了预训练好的人脸检测器、关键点检测器和facenet模型,它们都可以在dlib官网上下载。如果你想实现更高效的人脸比对,可以考虑使用GPU加速。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值