基于机器学习CNN方法来检测人脸比之前介绍的效率要慢很多
需要先下载一个训练好的模型数据:
// dlib_cnn_facedetect.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h> using namespace std;
using namespace dlib; // ---------------------------------------------------------------------------------------- template <long num_filters, typename SUBNET> using con5d = con<num_filters, , , , , SUBNET>;
template <long num_filters, typename SUBNET> using con5 = con<num_filters, , , , , SUBNET>; template <typename SUBNET> using downsampler = relu<affine<con5d<, relu<affine<con5d<, relu<affine<con5d<, SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5 = relu<affine<con5<, SUBNET>>>; using net_type = loss_mmod<con<, , , , , rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<>>>>>>>>; // ---------------------------------------------------------------------------------------- int main(int argc, char** argv)
{
try { if (argc == )
{
cout << "Call this program like this:" << endl;
cout << "./dnn_mmod_face_detection_ex mmod_human_face_detector.dat faces/*.jpg" << endl;
cout << "\nYou can get the mmod_human_face_detector.dat file from:\n";
cout << "http://dlib.net/files/mmod_human_face_detector.dat.bz2" << endl;
return ;
} net_type net;
deserialize(argv[]) >> net; image_window win;
for (int i = ; i < argc; ++i)
{
matrix<rgb_pixel> img;
load_image(img, argv[i]); // Upsampling the image will allow us to detect smaller faces but will cause the
// program to use more RAM and run longer.
while (img.size() < * )
pyramid_up(img); // Note that you can process a bunch of images in a std::vector at once and it runs
// much faster, since this will form mini-batches of images and therefore get
// better parallelism out of your GPU hardware. However, all the images must be
// the same size. To avoid this requirement on images being the same size we
// process them individually in this example.
auto dets = net(img);
win.clear_overlay();
win.set_image(img);
for (auto&& d : dets)
win.add_overlay(d); cout << "Hit enter to process the next image." << endl;
cin.get();
}
}
catch (std::exception& e)
{
cout << e.what() << endl;
} }