本文主要介紹三個點:
1. 如何單獨建立一個工程,使用dlib的人臉檢測功能。
2. 提高人臉檢測率的兩個方法
3. 加速人臉檢測的方法
下面圍繞這幾個點展開敘述。
建人臉檢測工程
1 . 首先我們先使用上期說的examples里的人臉檢測。
我們只要將face_detection_ex設為啟動項,即可運行。效果如下:
2. 建立單獨的工程。像其他正常的方法,建立一般的工程。然后
在項目 屬性中選擇C/C++ :
常規-》附加包含目錄:填寫之前準備好的dlib的include的路徑,我這里是:D:\dlib_win32\include
預處理器定義:
WIN32
_WINDOWS
DLIB_PNG_SUPPORT
DLIB_JPEG_SUPPORT
NDEBUG
DLIB_HAVE_AVX
鏈接器-》常規-》附加庫目錄:填寫你要加的庫目錄。我這里是
D:\dlib_win32\lib
輸入-》附加依賴項:dlib.lib
命令行-》其它選項:/arch:AVX
最后在配置屬性-》調試中添加:你要檢測的圖片的路徑,
main.cpp可以使用dlib提供的官方示例:
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*This example program shows how to find frontal human faces in an image. Inparticular, this program shows how you can take a list of images from thecommand line and display each on the screen with red boxes overlaid on eachhuman face.The examples/faces folder contains some jpg images of people. You can runthis program on them and see the detections by executing the following command:./face_detection_ex faces/*.jpgThis face detector is made using the now classic Histogram of OrientedGradients (HOG) feature combined with a linear classifier, an image pyramid,and sliding window detection scheme. This type of object detector is fairlygeneral and capable of detecting many types of semi-rigid objects inaddition to human faces. Therefore, if you are interested in making yourown object detectors then read the fhog_object_detector_ex.cpp exampleprogram. It shows how to use the machine learning tools which were used tocreate dlib's face detector. Finally, note that the face detector is fastest when compiled with at leastSSE2 instructions enabled. So if you are using a PC with an Intel or AMDchip then you should enable at least SSE2 instructions. If you are usingcmake to compile this program you can enable them by using one of thefollowing commands when you create the build project:cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ONcmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ONcmake path_to_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();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]);// 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.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);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;}
}// ----------------------------------------------------------------------------------------
然后就可以人臉檢測了。如下是我的效果。
提高人臉檢測率的兩個方法
- 確保檢測圖片是檢測器的兩倍。這第一點是十分有用的,因為臉部檢測器搜尋的人臉大小是80*80或者更大。
因此,如果你想找到比80*80小的人臉,需要將檢測圖片進行上采樣,我們可以調用pyramid_up()函數。
執行一次pyramid_up()我們能檢測40*40大小的了,如果我們想檢測更小的人臉,那還需要再次執行pyramid_up()函數。
注意,上采樣后,速度會減慢!*/
pyramid_up(img);//對圖像進行上采用,檢測更小的人臉。 - 在程序中使用:
array2d<rgb_pixel> img;
取代:
array2d<unsigned char> img;
這個我試驗過了,有些圖片使用’rgb_pixel‘就檢測不出來,‘unsigned \ char’就可以。可能是前者使用的rgb信息而后者只使用了灰度信息。
加速人臉檢測
可以參考這兩篇文章。這也是為什么我們要在命令行-》其它選項:/arch:AVX 加的原因。
跨平臺使用Intrinsic函數范例1——使用SSE、AVX指令集 處理 單精度浮點數組求和(支持vc、gcc,兼容Windows、Linux、Mac)
Why is dlib slow?
參考文獻:
- http://dlib.net/
- http://blog.csdn.net/sunshine_in_moon/article/details/50149339