可以使用dlib库来检测图像中的人脸,选取其中最大的一张人脸,并标出眼部区域。下面是一个简单的C++代码示例:
``` #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h>
using namespace dlib; using namespace std;
int main(int argc, char** argv) { try { if (argc == 1) { cout << "Call this program like this: " << endl; cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl; cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n"; cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl; return 0; }
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize(argv[1]) >> sp;
image_window win, win_eyes;
for (int i = 2; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<rgb_pixel> img;
load_image(img, argv[i]);
pyramid_up(img);
// detect faces
std::vector<rectangle> faces = detector(img);
if(faces.size()>0){
// find the biggest face
auto max_area = 0;
auto max_area_idx = 0;
for (int i = 0; i < faces.size(); i++) {
auto area = faces[i].area();
if (area > max_area) {
max_area = area;
max_area_idx = i;
}
}
// Find the pose of the face
full_object_detection shape = sp(img, faces[max_area_idx]);
// Draw the face landmarks on the screen.
win.clear_overlay();
win.set_image(img);
win.add_overlay(render_face_detections(shape));
// Draw the eyes on the screen
array2d<rgb_pixel> face_chip;
extract_image_chip(img, get_face_chip_details(shape,150,0.25), face_chip);
win_eyes.set_image(face_chip);
win_eyes.add_overlay