分享

Dlib机器学习库学习系列三

 黄建校 2015-12-08

        本篇博客是Dlib库学习的第三篇---人脸对齐。人脸对齐与人脸检测工程建立与配置基本相同,在此不再赘述。可参照我上一篇博客。闲话少说,来点干货。

     步骤一:建立并配置工程,参照上一篇博客。

     步骤二:下载形状模型文件

     下载地址:模型文件

         步骤三:具体代码,这段代码也是dlib提供的例子,我自己添加的中文注释!

  1. // The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt  
  2. /* 
  3.  
  4. This example program shows how to find frontal human faces in an image and 
  5. estimate their pose.  The pose takes the form of 68 landmarks.  These are 
  6. points on the face such as the corners of the mouth, along the eyebrows, on 
  7. the eyes, and so forth. 
  8. ****这个例子展示了怎样在一张图片中找到正脸和他们的姿势.姿势是由68个点的形式组成的. 
  9.  
  10.  
  11. //This face detector is made using the classic Histogram of Oriented 
  12. Gradients (HOG) feature combined with a linear classifier, an image pyramid, 
  13. and sliding window detection scheme.// 
  14. ****人脸检测器的原理 
  15.  
  16. The pose estimator was created by 
  17. using dlib's implementation of the paper://根据这篇论文编写的程序 
  18. One Millisecond Face Alignment with an Ensemble of Regression Trees by 
  19. Vahid Kazemi and Josephine Sullivan, CVPR 2014 
  20. and was trained on the iBUG 300-W face landmark dataset. 
  21.  
  22. Also, note that you can train your own models using dlib's machine learning 
  23. tools.  See train_shape_predictor_ex.cpp to see an example. 
  24. ****我们可以训练自己的模型,用train_shape_predictor_ex.exe 
  25.  
  26.  
  27. Finally, note that the face detector is fastest when compiled with at least 
  28. SSE2 instructions enabled.  So if you are using a PC with an Intel or AMD 
  29. chip then you should enable at least SSE2 instructions.  If you are using 
  30. cmake to compile this program you can enable them by using one of the 
  31. following commands when you create the build project: 
  32. cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON 
  33. cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON 
  34. cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON 
  35. This will set the appropriate compiler options for GCC, clang, Visual 
  36. Studio, or the Intel compiler.  If you are using another compiler then you 
  37. need to consult your compiler's manual to determine how to enable these 
  38. instructions.  Note that AVX is the fastest but requires a CPU from at least 
  39. 2011.  SSE4 is the next fastest and is supported by most current machines. 
  40. */  
  41.   
  42.   
  43. #include <dlib/image_processing/frontal_face_detector.h>  
  44. #include <dlib/image_processing/render_face_detections.h>  
  45. #include <dlib/image_processing.h>  
  46. #include <dlib/gui_widgets.h>  
  47. #include <dlib/image_io.h>  
  48. #include <iostream>  
  49.   
  50. using namespace dlib;  
  51. using namespace std;  
  52.   
  53. // ----------------------------------------------------------------------------------------  
  54.   
  55. int main(int argc, char** argv)  
  56. {  
  57.     try  
  58.     {  
  59.         // This example takes in a shape model file and then a list of images to  
  60.         // process.  We will take these filenames in as command line arguments.  
  61.         // Dlib comes with example images in the examples/faces folder so give  
  62.         // those as arguments to this program.  
  63.         // 这个例子需要一个形状模型文件和一系列的图片.  
  64.         if (argc == 1)  
  65.         {  
  66.             cout << "Call this program like this:" << endl;  
  67.             cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;  
  68.             cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";  
  69.             cout << "http:///files/shape_predictor_68_face_landmarks.dat.bz2" << endl;//从这个地址下载模型标记点数据  
  70.             return 0;  
  71.         }  
  72.   
  73.         // We need a face detector.  We will use this to get bounding boxes for  
  74.         // each face in an image.  
  75.         //****需要一个人脸检测器,获得一个边界框  
  76.         frontal_face_detector detector = get_frontal_face_detector();  
  77.   
  78.         // And we also need a shape_predictor.  This is the tool that will predict face  
  79.         // landmark positions given an image and face bounding box.  Here we are just  
  80.         // loading the model from the shape_predictor_68_face_landmarks.dat file you gave  
  81.         // as a command line argument.  
  82.         //****也需要一个形状预测器,这是一个工具用来预测给定的图片和脸边界框的标记点的位置。  
  83.         //****这里我们仅仅从shape_predictor_68_face_landmarks.dat文件加载模型  
  84.         shape_predictor sp;//定义个shape_predictor类的实例  
  85.         deserialize(argv[1]) >> sp;  
  86.   
  87.   
  88.         image_window win, win_faces;  
  89.         // Loop over all the images provided on the command line.  
  90.         // ****循环所有图片  
  91.         for (int i = 2; i < argc; ++i)  
  92.         {  
  93.             cout << "processing image " << argv[i] << endl;  
  94.             array2d<rgb_pixel> img;//注意变量类型 rgb_pixel 三通道彩色图像  
  95.             load_image(img, argv[i]);  
  96.             // Make the image larger so we can detect small faces.  
  97.             pyramid_up(img);  
  98.   
  99.             // Now tell the face detector to give us a list of bounding boxes  
  100.             // around all the faces in the image.  
  101.             std::vector<rectangle> dets = detector(img);//检测人脸,获得边界框  
  102.             cout << "Number of faces detected: " << dets.size() << endl;//检测到人脸的数量  
  103.   
  104.             // Now we will go ask the shape_predictor to tell us the pose of  
  105.             // each face we detected.  
  106.             //****调用shape_predictor类函数,返回每张人脸的姿势  
  107.             std::vector<full_object_detection> shapes;//注意形状变量的类型,full_object_detection  
  108.             for (unsigned long j = 0; j < dets.size(); ++j)  
  109.             {  
  110.                 full_object_detection shape = sp(img, dets[j]);//预测姿势,注意输入是两个,一个是图片,另一个是从该图片检测到的边界框  
  111.                 cout << "number of parts: " << shape.num_parts() << endl;  
  112.                 //cout << "pixel position of first part:  " << shape.part(0) << endl;//获得第一个点的坐标,注意第一个点是从0开始的  
  113.                 //cout << "pixel position of second part: " << shape.part(1) << endl;//获得第二个点的坐标  
  114.                 /*自己改写,打印出全部68个点*/  
  115.                 for (int i = 1; i < 69; i++)  
  116.                 {  
  117.                     cout << "第 " << i<< " 个点的坐标: " << shape.part(i-1) << endl;  
  118.                 }  
  119.                 // You get the idea, you can get all the face part locations if  
  120.                 // you want them.  Here we just store them in shapes so we can  
  121.                 // put them on the screen.  
  122.                 shapes.push_back(shape);  
  123.             }  
  124.   
  125.             // Now let's view our face poses on the screen.  
  126.             //**** 显示结果  
  127.             win.clear_overlay();  
  128.             win.set_image(img);  
  129.             win.add_overlay(render_face_detections(shapes));  
  130.   
  131.             // We can also extract copies of each face that are cropped, rotated upright,  
  132.             // and scaled to a standard size as shown here:  
  133.             //****我们也能提取每张剪裁后的人脸的副本,旋转和缩放到一个标准尺寸  
  134.             dlib::array<array2d<rgb_pixel> > face_chips;  
  135.             extract_image_chips(img, get_face_chip_details(shapes), face_chips);  
  136.             win_faces.set_image(tile_images(face_chips));  
  137.   
  138.             cout << "Hit enter to process the next image..." << endl;  
  139.             cin.get();  
  140.         }  
  141.     }  
  142.     catch (exception& e)  
  143.     {  
  144.         cout << "\nexception thrown!" << endl;  
  145.         cout << e.what() << endl;  
  146.     }  
  147. }  
  148.   
  149. // ----------------------------------------------------------------------------------------  
其他的和上一篇博客相同,祝大家好运!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多