欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dlib 05 dlib自带demo 人脸检测

程序员文章站 2022-04-08 08:20:39
...

01 dlib的人脸检测资源

dlib提供了fhog的人脸图片及对应的数据文件。
代码:dlib\examples\face_detection_ex.cpp
工程名:face_detection_ex
检测图像文件:dlib\examples\faces
dlib\examples\faces\2007_007763.jpg

02 项目设置

把examples解决方案中的face_detection_ex工程设置为启动项。

配置属性==>调试==>命令参数==>..\..\..\examples\faces
配置属性==>调试==>工作目录==>$(OutDir)

dlib 05 dlib自带demo 人脸检测

03 检测结果

processing image ..\..\..\examples\faces\2007_007763.jpg
Number of faces detected: 7
Hit enter to process the next image...

dlib 05 dlib自带demo 人脸检测

04 代码

本地代码:dlib\examples\face_detection_ex.cpp
官网:http://dlib.net/face_detection_ex.cpp.html

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>

using namespace dlib;
using namespace std;

int main(int argc, char** argv)
{  
    try
    {
        if (argc == 1)
        {
            cout << "Give some image files as arguments to this program." << endl;
            return 0;
        }

        frontal_face_detector detector = get_frontal_face_detector();
        image_window win;

        // Loop over all the images provided on the command line.
        for (int i = 1; i < argc; ++i)
        {
            cout << "processing image " << argv[i] << endl;
            //array2d<unsigned char> img;  // 黑白,检测速度快
            dlib::array2d<dlib::rgb_pixel> img;  // 彩色
            load_image(img, argv[i]);
            // 如果需要查找小于80*80的脸,需要上采样,执行一次pyramid_up()能检测40*40像素的量
            // 上采样速度很慢
            pyramid_up(img);  // 上采样1次,能够检测到40*40-80*80的脸,如果注释掉这句上采样,检测不到2007_007763.jpg的脸

            // Now tell the face detector to give us a list of bounding boxes
            // around all the faces it can find in the image.
            std::vector<rectangle> dets = detector(img);  // 人脸框

            cout << "Number of faces detected: " << dets.size() << endl; // 图片中人脸数
            // 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;
    }
}
相关标签: dlib