Dlib example运行

转载请注明出处:http://blog.csdn.net/ouyangying123/article/details/70850533


本文章主要进行dlib中....\dlib-18.18\examples下的三个例子的实现。

本小白用的是:VS2013 + dlib18.18 + opencv2.4.11

1.opencv用于读取数据源(待测图片、视频、摄像机等)。

2.dlib用于人脸检测,特征点检测等。
3.dlib环境配置在上一篇文章已介绍,接下来加上opencv的环境即可运行:
4.还需从官网上下一个人脸训练数据: shape_predictor_68_face_landmarks.dat
5.一定要是Release模式下,Debug模式的摄像机一帧的检测速度慢到难以置信。

Opencv配置

下载opencv,配置属性如下:

VC++目录->包含目录:...\opencv\build\include

VC++目录->库目录:...\opencv\build\x86\vc12\lib  (vs2013 对应vc12  x86对应win32系统)

链接器->输入->附加依赖项(release模式):

opencv_calib3d2411.lib
opencv_contrib2411.lib
opencv_core2411.lib
opencv_features2d2411.lib
opencv_flann2411.lib
opencv_gpu2411.lib
opencv_highgui2411.lib
opencv_imgproc2411.lib
opencv_legacy2411.lib
opencv_ml2411.lib
opencv_nonfree2411.lib
opencv_objdetect2411.lib
opencv_ocl2411.lib
opencv_photo2411.lib
opencv_stitching2411.lib
opencv_superres2411.lib
opencv_ts2411.lib
opencv_video2411.lib
opencv_videostab2411.lib


三个例子:


face_detection_ex :图像人脸检测

copy文件....\dlib-18.18\examples\face_detection_ex .cpp

上代码:

[cpp]  view plain  copy
  1. #include <dlib/image_processing/frontal_face_detector.h>  
  2. #include <dlib/gui_widgets.h>  
  3. #include <dlib/image_io.h>  
  4. #include <iostream>  
  5.   
  6. using namespace dlib;  
  7. using namespace std;  
  8.   
  9.   
  10. int main(int argc, char** argv)  
  11. {    
  12.     try  
  13.     {  
  14.         if (argc == 1)  
  15.         {  
  16.             cout << "Give some image files as arguments to this program." << endl;  
  17.             return 0;  
  18.         }  
  19.   
  20.         frontal_face_detector detector = get_frontal_face_detector();  
  21.         image_window win;  
  22.   
  23.         // Loop over all the images provided on the command line.  
  24.         for (int i = 1; i < argc; ++i)  
  25.         {  
  26.             cout << "processing image " << argv[i] << endl;  
  27.             array2d<unsigned char> img;  
  28.             load_image(img, argv[i]);  
  29.   
  30.             pyramid_up(img);  
  31.   
  32.             // Now tell the face detector to give us a list of bounding boxes  
  33.             // around all the faces it can find in the image.  
  34.             std::vector<rectangle> dets = detector(img);  
  35.   
  36.             cout << "Number of faces detected: " << dets.size() << endl;  
  37.             // Now we show the image on the screen and the face detections as  
  38.             // red overlay boxes.  
  39.             win.clear_overlay();  
  40.             win.set_image(img);  
  41.             win.add_overlay(dets, rgb_pixel(255,0,0));  
  42.   
  43.             cout << "Hit enter to process the next image..." << endl;  
  44.             cin.get();  
  45.         }  
  46.     }  
  47.     catch (exception& e)  
  48.     {  
  49.         cout << "\nexception thrown!" << endl;  
  50.         cout << e.what() << endl;  
  51.     }  
  52.     system("pause");  
  53. }  
右键生成后,把faces文件夹(...\dlib-18.18\examples\faces)复制到该项目的Release文件夹下,使用命令行进入该项目的Release目录下,运行该命令:
face_detection_ex.exe faces/2008_001322.jpg
上结果:




face_landmark_detection_ex :图像人脸特征点提取

copy文件....\dlib-18.18\examples\face_landmark_detection_ex.cpp

上代码:
[cpp]  view plain  copy
  1. #include <dlib/image_processing/frontal_face_detector.h>  
  2. #include <dlib/image_processing/render_face_detections.h>  
  3. #include <dlib/image_processing.h>  
  4. #include <dlib/gui_widgets.h>  
  5. #include <dlib/image_io.h>  
  6. #include <dlib/opencv.h>    
  7. #include <iostream>  
  8.   
  9. #include <opencv2/opencv.hpp>   
  10. using namespace dlib;  
  11. using namespace std;  
  12.   
  13.   
  14.   
  15. int main(int argc, char** argv)  
  16. {    
  17.     try  
  18.     {  
  19.         // This example takes in a shape model file and then a list of images to  
  20.         // process.  We will take these filenames in as command line arguments.  
  21.         // Dlib comes with example images in the examples/faces folder so give  
  22.         // those as arguments to this program.  
  23.         if (argc == 1)  
  24.         {  
  25.             cout << "Call this program like this:" << endl;  
  26.             cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;  
  27.             cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";  
  28.             cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;  
  29.             return 0;  
  30.         }  
  31.         std::cout <<"argc:"<< argc << std::endl;  
  32.         // We need a face detector.  We will use this to get bounding boxes for  
  33.         // each face in an image.  
  34.         frontal_face_detector detector = get_frontal_face_detector();  
  35.         // And we also need a shape_predictor.  This is the tool that will predict face  
  36.         // landmark positions given an image and face bounding box.  Here we are just  
  37.         // loading the model from the shape_predictor_68_face_landmarks.dat file you gave  
  38.         // as a command line argument.  
  39.         shape_predictor sp;  
  40.         std::cout << "argv[1]:"<< argv[1] << std::endl;  
  41.         deserialize(argv[1]) >> sp;  
  42.   
  43.   
  44.         image_window win;// win_faces;  
  45.         // Loop over all the images provided on the command line.  
  46.         for (int i = 2; i < argc; ++i)  
  47.         {  
  48.             cout << "processing image " << argv[i] << endl;  
  49.             array2d<rgb_pixel> img;  
  50.             load_image(img, argv[i]);  
  51.             // Make the image larger so we can detect small faces.  
  52.             pyramid_up(img);  
  53.   
  54.             // Now tell the face detector to give us a list of bounding boxes  
  55.             // around all the faces in the image.  
  56.             std::vector<rectangle> dets = detector(img);  
  57.             cout << "Number of faces detected: " << dets.size() << endl;  
  58.   
  59.             // Now we will go ask the shape_predictor to tell us the pose of  
  60.             // each face we detected.  
  61.             std::vector<full_object_detection> shapes;  
  62.             for (unsigned long j = 0; j < dets.size(); ++j)  
  63.             {  
  64.                 full_object_detection shape = sp(img, dets[j]);  
  65.                 cout << "number of parts: "<< shape.num_parts() << endl;  
  66.                 cout << "pixel position of first part:  " << shape.part(0) << endl;  
  67.                 cout << "pixel position of second part: " << shape.part(1) << endl;  
  68.                 // You get the idea, you can get all the face part locations if  
  69.                 // you want them.  Here we just store them in shapes so we can  
  70.                 // put them on the screen.  
  71.                 shapes.push_back(shape);  
  72.                 cv::Mat temp = dlib::toMat(img);  
  73.   
  74.                 for (int k = 0; k < 68; ++k){  
  75.                     circle(temp, cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);  
  76.                 }  
  77.             }  
  78.   
  79.             // Now let's view our face poses on the screen.  
  80.             win.clear_overlay();  
  81.             win.set_image(img);  
  82.             //win.add_overlay(render_face_detections(shapes));  
  83.   
  84.              We can also extract copies of each face that are cropped, rotated upright,  
  85.              and scaled to a standard size as shown here:  
  86.             //dlib::array<array2d<rgb_pixel> > face_chips;  
  87.             //extract_image_chips(img, get_face_chip_details(shapes), face_chips);  
  88.             //win_faces.set_image(tile_images(face_chips));  
  89.   
  90.             cout << "Hit enter to process the next image..." << endl;  
  91.             cin.get();  
  92.         }  
  93.     }  
  94.     catch (exception& e)  
  95.     {  
  96.         cout << "\nexception thrown!" << endl;  
  97.         cout << e.what() << endl;  
  98.     }  
  99.     system("pause");  
  100. }  

shape_predictor_68_face_landmarks.dat文件 和 faces文件夹(...\dlib-18.18\examples\faces)复制到该项目的Release文件夹下,使用命令行进入该项目的Release目录下,运行该命令:

face_landmark_detection_ex.exe shape_predictor_68_face_landmarks.dat faces/2008_001322.jpg


上结果:


webcam_face_pose_ex: 摄像机人脸特征点提取

copy文件....\dlib-18.18\examples\webcam_face_pose_ex.cpp
shape_predictor_68_face_landmarks.dat文件复制到webcam_face_pose_ex.cpp所在目录下
上代码:(需要有摄像头,添加了使用opencv画点的代码)
其中
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
用于设置摄像头分辨率。
#define RATIO 1
#define SKIP_FRAMES 2
用于加速检测
[cpp]  view plain  copy
  1. #include <dlib/opencv.h>  
  2. #include <opencv2/opencv.hpp>  
  3. #include <dlib/image_processing/frontal_face_detector.h>  
  4. #include <dlib/image_processing/render_face_detections.h>  
  5. #include <dlib/image_processing.h>  
  6. #include <dlib/gui_widgets.h>  
  7.   
  8. using namespace dlib;  
  9. using namespace std;  
  10.   
  11. #define RATIO 1  
  12. #define SKIP_FRAMES 2  
  13. int main()  
  14. {  
  15.     try  
  16.     {  
  17.         cv::VideoCapture cap(0);  
  18.         image_window win;  
  19.         //cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);  
  20.         //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);  
  21.         // Load face detection and pose estimation models.  
  22.         frontal_face_detector detector = get_frontal_face_detector();  
  23.         shape_predictor pose_model;  
  24.         deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;  
  25.           
  26.         int count = 0;  
  27.         std::vector<rectangle> faces;  
  28.         // Grab and process frames until the main window is closed by the user.  
  29.         while (!win.is_closed())  
  30.         {  
  31.             // Grab a frame  
  32.             cv::Mat img,img_small;  
  33.             cap >> img;  
  34.             cv::resize(img, img_small, cv::Size(), 1.0 / RATIO, 1.0 / RATIO);  
  35.   
  36.             cv_image<bgr_pixel> cimg(img);  
  37.             cv_image<bgr_pixel> cimg_small(img_small);  
  38.   
  39.             // Detect faces   
  40.             if (count++ % SKIP_FRAMES == 0){  
  41.                 faces = detector(cimg_small);  
  42.             }  
  43.             // Find the pose of each face.  
  44.             std::vector<full_object_detection> shapes;  
  45.             for (unsigned long i = 0; i < faces.size(); ++i){  
  46.                 rectangle r(  
  47.                     (long)(faces[i].left() * RATIO),  
  48.                     (long)(faces[i].top() * RATIO),  
  49.                     (long)(faces[i].right() * RATIO),  
  50.                     (long)(faces[i].bottom() * RATIO)  
  51.                     );  
  52.                 shapes.push_back(pose_model(cimg, r));  
  53.                 for (int k = 0; k < 68; ++k){  
  54.                     circle(img, cvPoint(shapes[i].part(k).x(), shapes[i].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);  
  55.                 }  
  56.             }  
  57.             std::cout << "count:" << count << std::endl;  
  58.             // Display it all on the screen  
  59.             win.clear_overlay();  
  60.             win.set_image(cimg);  
  61.             win.add_overlay(render_face_detections(shapes));  
  62.         }  
  63.     }  
  64.     catch (serialization_error& e)  
  65.     {  
  66.         cout << "You need dlib's default face landmarking model file to run this example." << endl;  
  67.         cout << "You can get it from the following URL: " << endl;  
  68.         cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;  
  69.         cout << endl << e.what() << endl;  
  70.     }  
  71.     catch (exception& e)  
  72.     {  
  73.         cout << e.what() << endl;  
  74.     }  
  75.     system("pause");  
  76. }  

直接运行即可得到结果:


附上参考链接:
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值