Dlib机器学习库学习系列三人脸对齐(特征点检测)

权声明:本文为博主原创文章,未经博主允许不得转载。

        本篇博客是Dlib库学习的第三篇---人脸对齐。人脸对齐与人脸检测工程建立与配置基本相同,在此不再赘述。可参照我上一篇博客。闲话少说,来点干货。

     步骤一:建立并配置工程,参照上一篇博客。

     步骤二:下载形状模型文件

     下载地址:模型文件

         步骤三:具体代码,这段代码也是dlib提供的例子,我自己添加的中文注释!

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

This example program shows how to find frontal human faces in an image and
estimate their pose.  The pose takes the form of 68 landmarks.  These are
points on the face such as the corners of the mouth, along the eyebrows, on
the eyes, and so forth.
****这个例子展示了怎样在一张图片中找到正脸和他们的姿势.姿势是由68个点的形式组成的.


//This face detector is made using the classic Histogram of Oriented
Gradients (HOG) feature combined with a linear classifier, an image pyramid,
and sliding window detection scheme.//
****人脸检测器的原理

The pose estimator was created by
using dlib's implementation of the paper://根据这篇论文编写的程序
One Millisecond Face Alignment with an Ensemble of Regression Trees by
Vahid Kazemi and Josephine Sullivan, CVPR 2014
and was trained on the iBUG 300-W face landmark dataset.

Also, note that you can train your own models using dlib's machine learning
tools.  See train_shape_predictor_ex.cpp to see an example.
****我们可以训练自己的模型,用train_shape_predictor_ex.exe


Finally, note that the face detector is fastest when compiled with at least
SSE2 instructions enabled.  So if you are using a PC with an Intel or AMD
chip then you should enable at least SSE2 instructions.  If you are using
cmake to compile this program you can enable them by using one of the
following commands when you create the build project:
cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON
This will set the appropriate compiler options for GCC, clang, Visual
Studio, or the Intel compiler.  If you are using another compiler then you
need to consult your compiler's manual to determine how to enable these
instructions.  Note that AVX is the fastest but requires a CPU from at least
2011.  SSE4 is the next fastest and is supported by most current machines.
*/


#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)
{
	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_68_face_landmarks.dat文件加载模型
		shape_predictor sp;//定义个shape_predictor类的实例
		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;//注意变量类型 rgb_pixel 三通道彩色图像
			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.
			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;
	}
}

// ----------------------------------------------------------------------------------------


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值