Facial landmark detection (Facial keypoints detection)
OpenSourceLibrary: DLib
Project Home: http://dlib.net/
Git address: https://github.com/davisking/dlib.git
Example file: git/dlib/examples/face_landmark_detection_ex.cpp
1 #include <dlib/image_processing/frontal_face_detector.h> 2 #include <dlib/image_processing/render_face_detections.h> 3 #include <dlib/image_processing.h> 4 #include <dlib/gui_widgets.h> 5 #include <dlib/image_io.h> 6 #include <iostream> 7 8 using namespace dlib; 9 using namespace std; 10 11 // ---------------------------------------------------------------------------------------- 12 13 int main(int argc, char** argv) 14 { 15 try 16 { 17 // This example takes in a shape model file and then a list of images to 18 // process. We will take these filenames in as command line arguments. 19 // Dlib comes with example images in the examples/faces folder so give 20 // those as arguments to this program. 21 if (argc == 1) 22 { 23 cout << "Call this program like this:" << endl; 24 cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl; 25 cout << " You can get the shape_predictor_68_face_landmarks.dat file from: "; 26 cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl; 27 return 0; 28 } 29 30 // We need a face detector. We will use this to get bounding boxes for 31 // each face in an image. 32 frontal_face_detector detector = get_frontal_face_detector(); 33 // And we also need a shape_predictor. This is the tool that will predict face 34 // landmark positions given an image and face bounding box. Here we are just 35 // loading the model from the shape_predictor_68_face_landmarks.dat file you gave 36 // as a command line argument. 37 shape_predictor sp; 38 deserialize(argv[1]) >> sp; 39 40 41 image_window win, win_faces; 42 // Loop over all the images provided on the command line. 43 for (int i = 2; i < argc; ++i) 44 { 45 cout << "processing image " << argv[i] << endl; 46 array2d<rgb_pixel> img; 47 load_image(img, argv[i]); 48 // Make the image larger so we can detect small faces. 49 pyramid_up(img); 50 51 // Now tell the face detector to give us a list of bounding boxes 52 // around all the faces in the image. 53 std::vector<rectangle> dets = detector(img); 54 cout << "Number of faces detected: " << dets.size() << endl; 55 56 // Now we will go ask the shape_predictor to tell us the pose of 57 // each face we detected. 58 std::vector<full_object_detection> shapes; 59 for (unsigned long j = 0; j < dets.size(); ++j) 60 { 61 full_object_detection shape = sp(img, dets[j]); 62 cout << "number of parts: "<< shape.num_parts() << endl; 63 cout << "pixel position of first part: " << shape.part(0) << endl; 64 cout << "pixel position of second part: " << shape.part(1) << endl; 65 // You get the idea, you can get all the face part locations if 66 // you want them. Here we just store them in shapes so we can 67 // put them on the screen. 68 shapes.push_back(shape); 69 } 70 71 // Now let's view our face poses on the screen. 72 win.clear_overlay(); 73 win.set_image(img); 74 win.add_overlay(render_face_detections(shapes)); 75 76 // We can also extract copies of each face that are cropped, rotated upright, 77 // and scaled to a standard size as shown here: 78 dlib::array<array2d<rgb_pixel> > face_chips; 79 extract_image_chips(img, get_face_chip_details(shapes), face_chips); 80 win_faces.set_image(tile_images(face_chips)); 81 82 cout << "Hit enter to process the next image..." << endl; 83 cin.get(); 84 } 85 } 86 catch (exception& e) 87 { 88 cout << " exception thrown!" << endl; 89 cout << e.what() << endl; 90 } 91 }