01 dlib的fhog人脸检测器训练资源
dlib提供了fhog的人脸图片及对应的数据文件。
代码:dlib\examples\fhog_object_detector_ex.cpp
工程名:fhog_object_detector_ex
测试数据:dlib\examples\faces
训练数据集图片
dlib\examples\faces\training.xml
dlib\examples\faces\2007_007763.jpg
dlib\examples\faces\2008_001009.jpg
dlib\examples\faces\2008_001322.jpg
dlib\examples\faces\2008_002079.jpg
测试数据及图片
dlib\examples\faces\testing.xml
dlib\examples\faces\2008_002470.jpg
dlib\examples\faces\2008_002506.jpg
dlib\examples\faces\2008_004176.jpg
dlib\examples\faces\2008_007676.jpg
dlib\examples\faces\2009_004587.jpg
02 项目设置
把examples解决方案中的fhog_object_detector_ex工程设置为启动项。
配置属性==>调试==>命令参数==>..\..\..\examples\faces
配置属性==>调试==>工作目录==>$(OutDir)
03 运行训练效果
04 参考
http://blog.csdn.net/u014365862/article/details/53338999
http://blog.csdn.net/qq_14845119/article/details/52625426
http://blog.csdn.net/elaine_bao/article/details/53046542
05 主要代码
dlib\examples\fhog_object_detector_ex.cpp
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<rectangle> > face_boxes_train, face_boxes_test;
load_image_dataset(images_train, face_boxes_train, faces_directory + "/training.xml");
load_image_dataset(images_test, face_boxes_test, faces_directory + "/testing.xml");
//2.图片上采样
upsample_image_dataset<pyramid_down<2> >(images_train, face_boxes_train);
upsample_image_dataset<pyramid_down<2> >(images_test, face_boxes_test);
//3.训练图片做镜像处理,扩充训练集
add_image_left_right_flips(images_train, face_boxes_train);
//二、training
//1.定义scanner类型,用于扫描图片并提取特征(HOG)
typedef scan_fhog_pyramid<pyramid_down<6> > image_scanner_type;
image_scanner_type scanner;
//2. 设置scanner扫描窗口大小
scanner.set_detection_window_size(80, 80);
//3.定义trainer类型(SVM),用于训练人脸检测器
structural_object_detection_trainer<image_scanner_type> trainer(scanner);
// Set this to the number of processing cores on your machine.
trainer.set_num_threads(4);
// 设置SVM的参数C,C越大表示更好地去拟合训练集,当然也有可能造成过拟合。通过尝试不同C在测试集上的效果得到最佳值
trainer.set_c(1);
trainer.be_verbose();
//设置训练结束条件,"risk gap"<0.01时训练结束,值越小表示SVM优化问题越精确,训练时间也会越久。
//通常取0.1-0.01.在verbose模式下每一轮的risk gap都会打印出来。
trainer.set_epsilon(0.01);
//4.训练,生成object_detector
object_detector<image_scanner_type> detector = trainer.train(images_train, face_boxes_train);
//三、测试
// 输出precision, recall, average precision.
cout << "training results: " << test_object_detection_function(detector, images_train, face_boxes_train) << endl;
cout << "testing results: " << test_object_detection_function(detector, images_test, face_boxes_test) << endl;
//显示hog
image_window hogwin(draw_fhog(detector), "Learned fHOG detector");
// 显示测试集的人脸检测结果
image_window win;
for (unsigned long i = 0; i < images_test.size(); ++i)
{
// Run the detector and get the face detections.
std::vector<rectangle> dets = detector(images_test[i]);
win.clear_overlay();
win.set_image(images_test[i]);
win.add_overlay(dets, rgb_pixel(255, 0, 0));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
//四、模型存储
serialize("face_detector.svm") << detector;
// you can recall it using the deserialize() function.
object_detector<image_scanner_type> detector2;
deserialize("face_detector.svm") >> detector2;
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}