【dlib代码解读】人脸关键点检测器的训练

##1. 源代码
先给出测试的结果,关键点并不是特别准,原因是训练样本数据量太少。
这里写图片描述
以下给出完整的人脸关键点检测器训练代码。详细的代码解读请看第二部分。

/* faceLandmarksTrain.cpp
function:借助dlib训练自己的人脸关键点检测器(参考dlib/examples/train_shape_predictor_ex)
date:2016/11/6
author:Elaine_Bao
*/

#include <dlib/image_processing.h>
#include <dlib/data_io.h>
#include <iostream>

using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------
//获取两眼间距离,输出D[i][j]表示objects[i][j]中人脸的两眼间距离
std::vector<std::vector<double> > get_interocular_distances(
	const std::vector<std::vector<full_object_detection> >& objects
	);

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

int main(int argc, char** argv)
{
	try
	{
		//一、preprocessing
		//1. 载入训练集,测试集
		const std::string faces_directory = "faces";
		dlib::array<array2d<unsigned char> > images_train, images_test;
		std::vector<std::vector<full_object_detection> > faces_train, faces_test;

		load_image_dataset(images_train, faces_train, faces_directory + "/training_with_face_landmarks.xml");
		load_image_dataset(images_test, faces_test, faces_directory + "/testing_with_face_landmarks.xml");

		// 二、training
		//1. 定义trainer类型
		shape_predictor_trainer trainer;
		//设置训练参数
		trainer.set_oversampling_amount(300); 
		trainer.set_nu(0.05);
		trainer.set_tree_depth(2);
		trainer.be_verbose();

		// 2. 训练,生成人脸关键点检测器
		shape_predictor sp = trainer.train(images_train, faces_train);


		// 三、测试
		cout << "mean training error: " <<
			test_shape_predictor(sp, images_train, faces_train, get_interocular_distances(faces_train)) << endl;
		cout << "mean testing error:  " <<
			test_shape_predictor(sp, images_test, faces_test, get_interocular_distances(faces_test)) << endl;

		// 四、存储
		serialize("sp.dat") << sp;
	}
	catch (exception& e)
	{
		cout << "\nexception thrown!" << endl;
		cout << e.what() << endl;
	}
}

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

double interocular_distance(
	const full_object_detection& det
	)
{
	dlib::vector<double, 2> l, r;
	double cnt = 0;
	// Find the center of the left eye by averaging the poi
  • 6
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值