Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.52 KB | None | 0 0
  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.  
  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. The pose estimator was created by
  14. using dlib's implementation of the paper:
  15. One Millisecond Face Alignment with an Ensemble of Regression Trees by
  16. Vahid Kazemi and Josephine Sullivan, CVPR 2014
  17. and was trained on the iBUG 300-W face landmark dataset.
  18.  
  19. Also, note that you can train your own models using dlib's machine learning
  20. tools. See train_shape_predictor_ex.cpp to see an example.
  21.  
  22.  
  23.  
  24.  
  25. Finally, note that the face detector is fastest when compiled with at least
  26. SSE2 instructions enabled. So if you are using a PC with an Intel or AMD
  27. chip then you should enable at least SSE2 instructions. If you are using
  28. cmake to compile this program you can enable them by using one of the
  29. following commands when you create the build project:
  30. cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON
  31. cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON
  32. cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON
  33. This will set the appropriate compiler options for GCC, clang, Visual
  34. Studio, or the Intel compiler. If you are using another compiler then you
  35. need to consult your compiler's manual to determine how to enable these
  36. instructions. Note that AVX is the fastest but requires a CPU from at least
  37. 2011. SSE4 is the next fastest and is supported by most current machines.
  38. */
  39.  
  40. #include "stdafx.h"
  41. #include <stdlib.h>
  42. #include <string>
  43. #include <boost/filesystem.hpp>
  44. #include <boost/foreach.hpp>
  45. #include <dlib/image_processing/frontal_face_detector.h>
  46. #include <dlib/image_processing/render_face_detections.h>
  47. #include <dlib/image_processing.h>
  48. #include <dlib/gui_widgets.h>
  49. #include <dlib/image_io.h>
  50. #include <dlib/svm_threaded.h>
  51. #include <iostream>
  52. #include <fstream>
  53.  
  54.  
  55.  
  56. using namespace dlib;
  57. using namespace std;
  58. namespace fs = boost::filesystem;
  59.  
  60.  
  61.  
  62. typedef matrix<double, 3, 1> sample_type;
  63.  
  64. std::vector<double> mouthareas;
  65. std::vector<double> lefteyeareas;
  66. std::vector<double> righteyeareas;
  67. std::vector<string> labelpaths;
  68.  
  69. void generate_data(std::vector<sample_type>& samples, std::vector<string>& labels);
  70. void classify();
  71.  
  72.  
  73.  
  74.  
  75. // ----------------------------------------------------------------------------------------
  76.  
  77. int main(int argc, char** argv)
  78. {
  79. try
  80. {
  81. fs::path targetDirIm("facesemotion");
  82. fs::directory_iterator itim(targetDirIm), eodim;
  83. // This example takes in a shape model file and then a list of images to
  84. // process. We will take these filenames in as command line arguments.
  85. // Dlib comes with example images in the examples/faces folder so give
  86. // those as arguments to this program.
  87. /*if (argc == 1)
  88. {
  89. cout << "Call this program like this:" << endl;
  90. cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
  91. cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
  92. cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
  93. return 0;
  94. }
  95. */
  96.  
  97. // We need a face detector. We will use this to get bounding boxes for
  98. // each face in an image.
  99. frontal_face_detector detector = get_frontal_face_detector();
  100. // And we also need a shape_predictor. This is the tool that will predict face
  101. // landmark positions given an image and face bounding box. Here we are just
  102. // loading the model from the shape_predictor_68_face_landmarks.dat file you gave
  103. // as a command line argument.
  104. shape_predictor sp;
  105. deserialize(argv[1]) >> sp;
  106.  
  107.  
  108. //image_window win, win_faces;
  109. string emotionname;
  110. string photofilename;
  111.  
  112.  
  113.  
  114. // Loop over all the images provided in folder.
  115. BOOST_FOREACH(fs::path const &p, std::make_pair(itim, eodim))
  116. {
  117. if (fs::is_regular_file(p))
  118. {
  119. emotionname = p.string();
  120. emotionname = emotionname.substr(13, emotionname.size() - 17);
  121. emotionname = "labels/" + emotionname + "_emotion.txt";
  122. cout << emotionname << endl;
  123. if (fs::is_regular_file(emotionname)) {
  124. cout << "yes" << endl;
  125.  
  126.  
  127. /*
  128. //cout << p << endl;
  129. labelpaths.push_back(emotionname);
  130.  
  131.  
  132. string line;
  133. string labelname;
  134. std::vector<string> featurepoints;
  135. ifstream myfile(p.string());
  136. if (myfile.is_open())
  137. {
  138. while (getline(myfile, line))
  139. {
  140. featurepoints.push_back(line);
  141. }
  142. myfile.close();
  143. }
  144.  
  145. else {
  146. cout << "Unable to open file";
  147. }
  148. */
  149. cout << "processing image " << p << endl;
  150.  
  151. array2d<rgb_pixel> img;
  152. load_image(img, p.string());
  153. // Make the image larger so we can detect small faces.
  154. pyramid_up(img);
  155.  
  156. // Now tell the face detector to give us a list of bounding boxes
  157. // around all the faces in the image.
  158. std::vector<rectangle> dets = detector(img);
  159. cout << "Number of faces detected: " << dets.size() << endl;
  160.  
  161. // Now we will go ask the shape_predictor to tell us the pose of
  162. // each face we detected.
  163.  
  164. for (unsigned long j = 0; j < dets.size(); ++j)
  165. {
  166. full_object_detection shape = sp(img, dets[j]);
  167. cout << "number of parts: " << shape.num_parts() << endl;
  168. cout << "pixel position of first part: " << shape.part(0) << endl;
  169. cout << "pixel position of second part: " << shape.part(1) << endl;
  170.  
  171. string textface = emotionname.substr(7, emotionname.size() - 19);
  172. textface = "textfaces/" + textface + ".txt";
  173. ofstream myfile;
  174. myfile.open(textface);
  175. cout << textface;
  176. for (int i = 0; i < shape.num_parts(); i++) {
  177. myfile << shape.part(i) << "\n" ;
  178.  
  179. }
  180. myfile.close();
  181.  
  182. // myfile.close();
  183. //assign facial landmarks to part of face
  184. std::vector<point> mouthlist;
  185. std::vector<point >leftbrowlist;
  186. std::vector<point > rightbrowlist;
  187. std::vector<point > righteyelist;
  188. std::vector<point > lefteyelist;
  189. std::vector<point> noselist;
  190. std::vector<point > jawlist;
  191.  
  192. for (int i = 0; i < shape.num_parts(); i++) {
  193. if (i <= 17) {
  194. jawlist.push_back(shape.part(i));
  195. }
  196. else if (i <= 22) {
  197. leftbrowlist.push_back(shape.part(i));
  198. }
  199. else if (i <= 27) {
  200. rightbrowlist.push_back(shape.part(i));
  201. }
  202. else if (i <= 36) {
  203. noselist.push_back(shape.part(i));
  204. }
  205. else if (i <= 42) {
  206. lefteyelist.push_back(shape.part(i));
  207. }
  208. else if (i <= 48) {
  209. righteyelist.push_back(shape.part(i));
  210. }
  211. else {
  212. mouthlist.push_back(shape.part(i));
  213. }
  214. }
  215.  
  216. //compute geometrical features
  217. /*
  218. int mouthxmin = 20000;
  219. int mouthxmax = -1;
  220. int mouthymin = 20000;
  221. int mouthymax = -1;
  222.  
  223. int lefteyexmin = 20000;
  224. int lefteyexmax = -1;
  225. int lefteyeymin = 20000;
  226. int lefteyeymax = -1;
  227.  
  228. int righteyexmin = 20000;
  229. int righteyexmax = -1;
  230. int righteyeymin = 20000;
  231. int righteyeymax = -1;
  232.  
  233.  
  234. for (int ii = 0; ii < mouthlist.size(); ii++) {
  235. int xvalue = std::stoi(mouthlist.at(ii).substr(1, 3));
  236. int yvalue = std::stoi(mouthlist.at(ii).substr(6, 3));
  237. if (xvalue > mouthxmax) {
  238. mouthxmax = xvalue;
  239. }
  240. if (xvalue < mouthxmin) {
  241. mouthxmin = xvalue;
  242. }
  243. if (yvalue > mouthymax) {
  244. mouthymax = yvalue;
  245. }
  246. if (yvalue < mouthymin) {
  247. mouthymin = yvalue;
  248. }
  249. }
  250.  
  251. int mouthwidth = mouthxmax - mouthxmin;
  252. int mouthlength = mouthymax - mouthymin;
  253. int mouthA = mouthwidth * mouthlength;
  254. int mouthP = 2 * (1 + mouthwidth);
  255. mouthareas.push_back(mouthA/1000);
  256.  
  257. //cout << mouthareas.size() << endl;
  258.  
  259.  
  260. for (int ii = 0; ii < lefteyelist.size(); ii++) {
  261. int xvalue = std::stoi(lefteyelist.at(ii).substr(1, 3));
  262. int yvalue = std::stoi(lefteyelist.at(ii).substr(6, 3));
  263. if (xvalue> lefteyexmax) {
  264. lefteyexmax = xvalue;
  265. }
  266. if (xvalue < lefteyexmin) {
  267. lefteyexmin = xvalue;
  268. }
  269. if (yvalue > lefteyeymax) {
  270. lefteyeymax = yvalue;
  271. }
  272. if (yvalue < lefteyeymin) {
  273. lefteyeymin = yvalue;
  274. }
  275. }
  276.  
  277.  
  278. int lefteyewidth = lefteyexmax - lefteyexmin;
  279. int lefteyelength = lefteyeymax - lefteyeymin;
  280. int lefteyeA = lefteyewidth * lefteyelength;
  281.  
  282.  
  283. int lefteyeP = 2 * (1 + lefteyewidth);
  284. lefteyeareas.push_back(lefteyeA/1000);
  285.  
  286. // cout << lefteyeareas.size() << endl;
  287.  
  288. for (int ii = 0; ii < righteyelist.size(); ii++) {
  289. int xvalue = std::stoi(righteyelist.at(ii).substr(1, 3));
  290. int yvalue = std::stoi(righteyelist.at(ii).substr(6, 3));
  291. if (xvalue > righteyexmax) {
  292. righteyexmax = xvalue;
  293. }
  294. if (xvalue < righteyexmin) {
  295. righteyexmin = xvalue;
  296. }
  297. if (yvalue > righteyeymax) {
  298. righteyeymax = yvalue;
  299. }
  300. if (yvalue < righteyeymin) {
  301. righteyeymin = yvalue;
  302. }
  303. }
  304.  
  305.  
  306. int righteyewidth = righteyexmax - righteyexmin;
  307. int righteyelength = righteyeymax - righteyeymin;
  308. int righteyeA = righteyewidth * righteyelength;
  309. int righteyeP = 2 * (1 + righteyewidth);
  310. righteyeareas.push_back(righteyeA/1000);
  311. */
  312. }
  313. }
  314. }
  315. }
  316.  
  317. classify();
  318.  
  319. }
  320.  
  321.  
  322.  
  323. catch (exception& e)
  324. {
  325. cout << "\nexception thrown!" << endl;
  326. cout << e.what() << endl;
  327. }
  328. }
  329.  
  330.  
  331. //svm classifier using cross-fold classification
  332. void classify()
  333. {
  334. //cout << "classifying" << endl;
  335. std::vector<sample_type> samples;
  336. std::vector<string> labels;
  337.  
  338. generate_data(samples, labels);
  339.  
  340.  
  341. // cout << "generated data" << endl;
  342.  
  343. typedef linear_kernel<sample_type> lin_kernel;
  344.  
  345. typedef svm_multiclass_linear_trainer <lin_kernel, string> svm_mc_trainer;
  346. svm_mc_trainer trainer;
  347.  
  348. multiclass_linear_decision_function<lin_kernel, string> df;
  349. deserialize("df.dat") >> df;
  350. randomize_samples(samples, labels);
  351. //cout << "randomized" << endl;
  352. cout << "cross validation: \n" << cross_validate_multiclass_trainer(trainer, samples, labels, 8) << endl;
  353.  
  354. serialize("df.dat") << df;
  355.  
  356. }
  357.  
  358. void generate_data(std::vector<sample_type>& samples, std::vector<string>& labels)
  359. {
  360. //make data with all features + labels in order to train classifier
  361. //cout << "generating data" << endl;
  362. sample_type m;
  363.  
  364. for (int i = 0; i < labelpaths.size(); i++) {
  365. string emotionname = labelpaths.at(i);
  366. m = { mouthareas.at(i), righteyeareas.at(i), lefteyeareas.at(i) };
  367. // cout << m << endl;
  368. samples.push_back(m);
  369.  
  370. string line;
  371. string labelname;
  372. ifstream myfile(emotionname);
  373. if (myfile.is_open())
  374. {
  375. while (getline(myfile, line))
  376. {
  377. labelname = line;
  378. }
  379. myfile.close();
  380. }
  381.  
  382. else {
  383. // cout << "Unable to open file";
  384. }
  385.  
  386. labels.push_back(labelname);
  387. cout << labelname << endl;
  388.  
  389.  
  390.  
  391.  
  392. }
  393.  
  394.  
  395. }
  396.  
  397.  
  398. // ----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement