MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/dnn_face_recognition_ex.cpp:81–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

79// ----------------------------------------------------------------------------------------
80
81int main(int argc, char** argv) try
82{
83 if (argc != 2)
84 {
85 cout << "Run this example by invoking it like this: " << endl;
86 cout << " ./dnn_face_recognition_ex faces/bald_guys.jpg" << endl;
87 cout << endl;
88 cout << "You will also need to get the face landmarking model file as well as " << endl;
89 cout << "the face recognition model file. Download and then decompress these files from: " << endl;
90 cout << "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2" << endl;
91 cout << "http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2" << endl;
92 cout << endl;
93 return 1;
94 }
95
96 // The first thing we are going to do is load all our models. First, since we need to
97 // find faces in the image we will need a face detector:
98 frontal_face_detector detector = get_frontal_face_detector();
99 // We will also use a face landmarking model to align faces to a standard pose: (see face_landmark_detection_ex.cpp for an introduction)
100 shape_predictor sp;
101 deserialize("shape_predictor_5_face_landmarks.dat") >> sp;
102 // And finally we load the DNN responsible for face recognition.
103 anet_type net;
104 deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;
105
106 matrix<rgb_pixel> img;
107 load_image(img, argv[1]);
108 // Display the raw image on the screen
109 image_window win(img);
110
111 // Run the face detector on the image of our action heroes, and for each face extract a
112 // copy that has been normalized to 150x150 pixels in size and appropriately rotated
113 // and centered.
114 std::vector<matrix<rgb_pixel>> faces;
115 for (auto face : detector(img))
116 {
117 auto shape = sp(img, face);
118 matrix<rgb_pixel> face_chip;
119 extract_image_chip(img, get_face_chip_details(shape,150,0.25), face_chip);
120 faces.push_back(move(face_chip));
121 // Also put some boxes on the faces so we can see that the detector is finding
122 // them.
123 win.add_overlay(face);
124 }
125
126 if (faces.size() == 0)
127 {
128 cout << "No faces found in image!" << endl;
129 return 1;
130 }
131
132 // This call asks the DNN to convert each face image in faces into a 128D vector.
133 // In this 128D vector space, images from the same person will be close to each other
134 // but vectors from different people will be far apart. So we can use these vectors to
135 // identify if a pair of images are from the same person or from different people.
136 std::vector<matrix<float,0,1>> face_descriptors = net(faces);
137
138

Callers

nothing calls this directly

Calls 15

extract_image_chipFunction · 0.85
get_face_chip_detailsFunction · 0.85
moveFunction · 0.85
chinese_whispersFunction · 0.85
cast_to_stringFunction · 0.85
tile_imagesFunction · 0.85
meanFunction · 0.85
deserializeFunction · 0.70
jitter_imageFunction · 0.70
load_imageFunction · 0.50
lengthFunction · 0.50

Tested by

no test coverage detected