使用dilib检测图中的人脸,选取其中最大一张人脸,标出眼部区域,c++实现

可以使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值