使用dlib人脸识别的例子

来自官方的例子: http://dlib.net/face_detection_ex.cpp.html
做了一些修改:

#include 
#include 
#include 
#include 

using namespace dlib;
using namespace std;

int main(int argc, char *argv[])
{
    try
    {
        //定义一个frontal_face_detctor类的实例detector,用get_frontal_face_detector函数初始化该实例
        frontal_face_detector detector = get_frontal_face_detector();
        image_window win;//一个显示窗口

        array2d<unsigned char> img;
        // 加载一张图片,从(图片路劲)加载到变量img
        load_image(img, "D:\\img.jpg");
        // Make the image bigger by a factor of two.  This is useful since
        // the face detector looks for faces that are about 80 by 80 pixels
        // or larger.  Therefore, if you want to find faces that are smaller
        // than that then you need to upsample the image as we do here by
        // calling pyramid_up().  So this will allow it to detect faces that
        // are at least 40 by 40 pixels in size.  We could call pyramid_up()
        // again to find even smaller faces, but note that every time we
        // upsample the image we make the detector run slower since it must
        // process a larger image.
        /*确保检测图片是检测器的两倍。这第一点是十分有用的,因为脸部检测器搜寻的人脸大小是80*80或者更大。
        因此,如果你想找到比80*80小的人脸,需要将检测图片进行上采样,我们可以调用pyramid_up()函数。
        执行一次pyramid_up()我们能检测40*40大小的了,如果我们想检测更小的人脸,那还需要再次执行pyramid_up()函数。
        注意,上采样后,速度会减慢!*/
        pyramid_up(img);//对图像进行上采用,检测更小的人脸

        // Now tell the face detector to give us a list of bounding boxes
        // around all the faces it can find in the image.
        //detector()函数检测人脸,返回一系列边界盒子
        std::vector dets = detector(img);

        cout << "Number of faces detected: " << dets.size() << endl;//dets.size 人脸数量
        // Now we show the image on the screen and the face detections as
        // red overlay boxes.
        // 在原图片上显示结果
        win.clear_overlay();
        win.set_image(img);
        win.add_overlay(dets, rgb_pixel(255, 0, 0));

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

我使用QT+VS2015编译测试的,附上我的qmake

win32:CONFIG(release, debug|release): LIBS += $$PWD/dlib/dlib.lib
else:win32:CONFIG(debug, debug|release): LIBS += $$PWD/dlib/dlibd.lib
DEFINES += DLIB_PNG_SUPPORT
DEFINES += DLIB_JPEG_SUPPORT
DEFINES += DLIB_HAVE_AVX

INCLUDEPATH += D:/OpenCV/dlib-19.2/

环境路径参考上一篇dlib编译博文。


有一点疑问:为什么显示出来的图片变成了黑白的?

你可能感兴趣的:(人脸检测)