总述:此例子是根据dlib两个关键的例子
1.训练:train_shape_predictor_ex.cpp
2.关键点检测:face_landmark_detection_ex
相关代码如下:
1 #include "stdafx.h" 2 #include <dlib/image_processing.h> 3 #include <dlib/data_io.h> 4 #include <iostream> 5 #include <dlib/image_processing/frontal_face_detector.h> 6 #include <dlib/image_processing/render_face_detections.h> 7 #include <dlib/image_processing.h> 8 #include <dlib/gui_widgets.h> 9 #include <dlib/image_io.h> 10 11 using namespace dlib; 12 using namespace std; 13 14 double interocular_distance( 15 const full_object_detection& det 16 ) 17 { 18 dlib::vector<double, 2> l, r; 19 double cnt = 0; 20 // Find the center of the left eye by averaging the points around 21 // the eye. 22 for (unsigned long i = 36; i <= 41; ++i) 23 { 24 l += det.part(i); 25 ++cnt; 26 } 27 l /= cnt; 28 29 // Find the center of the right eye by averaging the points around 30 // the eye. 31 cnt = 0; 32 for (unsigned long i = 42; i <= 47; ++i) 33 { 34 r += det.part(i); 35 ++cnt; 36 } 37 r /= cnt; 38 39 // Now return the distance between the centers of the eyes 40 return length(l - r); 41 } 42 43 std::vector<std::vector<double> > get_interocular_distances( 44 const std::vector<std::vector<full_object_detection> >& objects 45 ) 46 { 47 std::vector<std::vector<double> > temp(objects.size()); 48 for (unsigned long i = 0; i < objects.size(); ++i) 49 { 50 for (unsigned long j = 0; j < objects[i].size(); ++j) 51 { 52 temp[i].push_back(interocular_distance(objects[i][j])); 53 } 54 } 55 return temp; 56 } 57 58 //训练函数 59 void train(){ 60 try 61 { 62 //一、preprocessing 63 //1. 载入训练集,测试集 64 const std::string faces_directory = "faces"; 65 dlib::array<array2d<unsigned char> > images_train, images_test; 66 std::vector<std::vector<full_object_detection> > faces_train, faces_test; 67 68 load_image_dataset(images_train, faces_train, faces_directory + "/training_with_face_landmarks.xml"); 69 load_image_dataset(images_test, faces_test, faces_directory + "/testing_with_face_landmarks.xml"); 70 71 // 二、training 72 //1. 定义trainer类型 73 shape_predictor_trainer trainer; 74 //设置训练参数 75 trainer.set_oversampling_amount(300); 76 trainer.set_nu(0.05); 77 trainer.set_tree_depth(2); 78 trainer.be_verbose(); 79 80 // 2. 训练,生成人脸关键点检测器 81 shape_predictor sp = trainer.train(images_train, faces_train); 82 83 84 // 三、测试 85 cout << "mean training error: " << 86 test_shape_predictor(sp, images_train, faces_train, get_interocular_distances(faces_train)) << endl; 87 cout << "mean testing error: " << 88 test_shape_predictor(sp, images_test, faces_test, get_interocular_distances(faces_test)) << endl; 89 90 // 四、存储 91 serialize("sp.dat") << sp; 92 } 93 catch (exception& e) 94 { 95 cout << " exception thrown!" << endl; 96 cout << e.what() << endl; 97 } 98 } 99 100 //关键点检测函数 101 int main(int argc, char** argv) 102 { 103 try 104 { 105 106 frontal_face_detector detector = get_frontal_face_detector(); 107 shape_predictor sp; 108 //将上一步训练好的sp.dat 载入 109 deserialize("sp.dat") >> sp; 110 image_window win, win_faces; 111 const std::string image = "faces/2007_007763.jpg"; 112 cout << "processing image " << image << endl; 113 array2d<rgb_pixel> img; 114 load_image(img, image); 115 // Make the image larger so we can detect small faces. 116 pyramid_up(img); 117 118 // Now tell the face detector to give us a list of bounding boxes 119 // around all the faces in the image. 120 std::vector<rectangle> dets = detector(img); 121 cout << "Number of faces detected: " << dets.size() << endl; 122 123 // Now we will go ask the shape_predictor to tell us the pose of 124 // each face we detected. 125 std::vector<full_object_detection> shapes; 126 for (unsigned long j = 0; j < dets.size(); ++j) 127 { 128 full_object_detection shape = sp(img, dets[j]); 129 cout << "number of parts: "<< shape.num_parts() << endl; 130 cout << "pixel position of first part: " << shape.part(0) << endl; 131 cout << "pixel position of second part: " << shape.part(1) << endl; 132 // You get the idea, you can get all the face part locations if 133 // you want them. Here we just store them in shapes so we can 134 // put them on the screen. 135 shapes.push_back(shape); 136 } 137 138 // Now let's view our face poses on the screen. 139 win.clear_overlay(); 140 win.set_image(img); 141 win.add_overlay(render_face_detections(shapes)); 142 143 // We can also extract copies of each face that are cropped, rotated upright, 144 // and scaled to a standard size as shown here: 145 dlib::array<array2d<rgb_pixel> > face_chips; 146 extract_image_chips(img, get_face_chip_details(shapes), face_chips); 147 win_faces.set_image(tile_images(face_chips)); 148 149 cout << "Hit enter to process the next image..." << endl; 150 cin.get(); 151 152 } 153 catch (exception& e) 154 { 155 cout << " exception thrown!" << endl; 156 cout << e.what() << endl; 157 } 158 }