基于dlib的68个特征点检测

dlib有个很炫的功能,就是可以检测人脸的68个特征点。本博客目的是教怎么调用dlib的API去实现这一功能,还是老规矩,代码会提供。下载地址在这儿。之所以提供下载地址,是因为文件要下载,从官网上下载速度贼慢贼慢,我特么都下了个把小时了,最后还是csdn上一哥们儿给下载到了。另外一个方面,我很讨厌从那种控制台或者是terminal输入参数,因为我是程序员呀,直接从代码改个路径不久好了,至于那么麻烦么。。。
最后,我用的是范冰冰女士的照片,如果要侵权的话,我可以删掉。。。

/**
This example program shows how to find frontal human faces in an image and estimate their pose.
The pose takes the form of 68 landmarks. These are points on the face such as the corners of the mouth, 
along the eyebrows, on the eyes, and so forth.

The face detector we use is made using the classic Histogram of Oriented Gradient(HOG) feature
combined with a linear classifier, an image pyramid, and sliding window detection scheme. The pose estimator was created by 
using dlib's implemention of the paper:
*/
#include <string>
#include <iostream>
#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>

void face_landmark_detection_ex()
{
    try
    {
        std::string face_landmarks_path = "../Resources/dlib_info_files/shape_predictor_68_face_landmarks.dat";
        std::string face_img_path = "../Resources/dlib_info_files/timg.jpg";        // 此为图片路径,可根据自己图片来更改路径

        dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
        dlib::shape_predictor sp;
        dlib::deserialize(face_landmarks_path) >> sp;
        dlib::image_window win;
        dlib::image_window win_faces;
        dlib::array2d<dlib::rgb_pixel> img;
        dlib::load_image(img, face_img_path);
        dlib::pyramid_up(img);

        // Now tell the face detector to give us a list of bounding boxes
        // around all the faces in the image.
        std::vector<dlib::rectangle> dets = detector(img);
        std::cout << "Number of faces detected: " << dets.size() << std::endl;

        // Now we will go ask the shape_predictor to tell us the pose of
        // each face we detected.
        std::vector<dlib::full_object_detection> shapes;
        for (unsigned long j = 0; j < dets.size(); ++j)
        {
            dlib::full_object_detection shape = sp(img, dets[j]);
            std::cout << "number of parts: " << shape.num_parts() << std::endl;
            std::cout << "pixel position of first part:  " << shape.part(0) << std::endl;
            std::cout << "pixel position of second part: " << shape.part(1) << std::endl;
            // You get the idea, you can get all the face part locations if
            // you want them.  Here we just store them in shapes so we can
            // put them on the screen.
            shapes.push_back(shape);
        }

        // Now let's view our face poses on the screen.
        win.clear_overlay();
        win.set_image(img);
        win.add_overlay(render_face_detections(shapes));

        // We can also extract copies of each face that are cropped, rotated upright,
        // and scaled to a standard size as shown here:
        dlib::array<dlib::array2d<dlib::rgb_pixel> > face_chips;
        extract_image_chips(img, get_face_chip_details(shapes), face_chips);
        win_faces.set_image(tile_images(face_chips));

        std::cout << "Hit enter to process the next image..." << std::endl;
        std::cin.get();
    }
    catch (std::exception& e)
    {
        std::cout << "\n exception thrown!" << std::endl;
        std::cout << e.what() << std::endl;
    }
}

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值