Dlib机器学习库系列人脸检测

来源:互联网 发布:ubuntu安装深度商店 编辑:程序博客网 时间:2024/06/05 06:42
这是Dlib库学习系列的第二篇,主要介绍人脸检测。Dlib库的人脸检测算法使用的hog特征与级联分类器。废话少说,下面开始。
步骤一:建立工程,配置工程。
建立工程就不多说了,不用预编译头,建立一个空项目就可以。下面主要说配置。
(1)属性->VC++目录
(2)这里写图片描述
(3)就是把上一篇博客中生成的dlib.lib导入工程中
步骤二:编写代码
在此我使用的dlib库提供的例子,我只是增加了中文注释
dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ONThis will set the appropriate compiler options for GCC, clang, VisualStudio, or the Intel compiler.  If you are using another compiler then youneed to consult your compiler's manual to determine how to enable theseinstructions.  Note that AVX is the fastest but requires a CPU from at least2011.  SSE4 is the next fastest and is supported by most current machines.*/#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();//定义一个frontal_face_detctor类的实例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;            load_image(img, argv[i]);// 加载一张图片,从argv[i](图片路劲)加载到变量img            // 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.            //开始检测,返回一系列的边界框            std::vector<rectangle> dets = detector(img);//detector()函数检测人脸,返回一系列边界盒子            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;    }}// 

(2)这个代码是带参主函数,我们可以编译完后用命令行的形式运行。如果你不想那么麻烦,我们可以在属性里设置命令参数
这里写图片描述

做完这一切,编译运行就可以了。

注意!!!
如果有如下报错1>dlib.lib(base_widgets.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(dlib_face.obj 中)
1>dlib.lib(base_widgets.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“MD_DynamicRelease”(dlib_face.obj 中)
原因是,你生成的dlib.lib是debug版本,而你的工程建立的是release版本,所有会有这个包括,只要将两者保持一致,就没有在这个报错了!


0 0
原创粉丝点击