facemark_lbf_fitting.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. This file was part of GSoC Project: Facemark API for OpenCV
  31. Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
  32. Student: Laksono Kurnianggoro
  33. Mentor: Delia Passalacqua
  34. */
  35. /*----------------------------------------------
  36. * Usage:
  37. * facemark_lbf_fitting <face_cascade_model> <lbf_model> <video_name>
  38. *
  39. * example:
  40. * facemark_lbf_fitting ../face_cascade.xml ../LBF.model ../video.mp4
  41. *
  42. * note: do not forget to provide the LBF_MODEL and DETECTOR_MODEL
  43. * the model are available at opencv_contrib/modules/face/data/
  44. *--------------------------------------------------*/
  45. #include <stdio.h>
  46. #include <ctime>
  47. #include <iostream>
  48. #include "opencv2/core.hpp"
  49. #include "opencv2/highgui.hpp"
  50. #include "opencv2/imgproc.hpp"
  51. #include "opencv2/face.hpp"
  52. using namespace std;
  53. using namespace cv;
  54. using namespace cv::face;
  55. static bool myDetector(InputArray image, OutputArray ROIs, CascadeClassifier *face_cascade);
  56. static bool parseArguments(int argc, char** argv,
  57. String & cascade, String & model,String & video);
  58. int main(int argc, char** argv ){
  59. String cascade_path,model_path,images_path, video_path;
  60. if(!parseArguments(argc, argv, cascade_path,model_path,video_path))
  61. return -1;
  62. CascadeClassifier face_cascade;
  63. face_cascade.load(cascade_path);
  64. FacemarkLBF::Params params;
  65. params.model_filename = model_path;
  66. params.cascade_face = cascade_path;
  67. Ptr<FacemarkLBF> facemark = FacemarkLBF::create(params);
  68. facemark->setFaceDetector((FN_FaceDetector)myDetector, &face_cascade);
  69. facemark->loadModel(params.model_filename.c_str());
  70. VideoCapture capture(video_path);
  71. Mat frame;
  72. if( !capture.isOpened() ){
  73. printf("Error when reading vide\n");
  74. return 0;
  75. }
  76. Mat img;
  77. String text;
  78. char buff[255];
  79. double fittime;
  80. int nfaces;
  81. std::vector<Rect> rects,rects_scaled;
  82. std::vector<std::vector<Point2f> > landmarks;
  83. CascadeClassifier cc(params.cascade_face.c_str());
  84. namedWindow( "w", 1);
  85. for( ; ; )
  86. {
  87. capture >> frame;
  88. if(frame.empty())
  89. break;
  90. double __time__ = (double)getTickCount();
  91. float scale = (float)(400.0/frame.cols);
  92. resize(frame, img, Size((int)(frame.cols*scale), (int)(frame.rows*scale)), 0, 0, INTER_LINEAR_EXACT);
  93. facemark->getFaces(img, rects);
  94. rects_scaled.clear();
  95. for(int j=0;j<(int)rects.size();j++){
  96. rects_scaled.push_back(Rect(
  97. (int)(rects[j].x/scale),
  98. (int)(rects[j].y/scale),
  99. (int)(rects[j].width/scale),
  100. (int)(rects[j].height/scale)));
  101. }
  102. rects = rects_scaled;
  103. fittime=0;
  104. nfaces = (int)rects.size();
  105. if(rects.size()>0){
  106. double newtime = (double)getTickCount();
  107. facemark->fit(frame, rects, landmarks);
  108. fittime = ((getTickCount() - newtime)/getTickFrequency());
  109. for(int j=0;j<(int)rects.size();j++){
  110. landmarks[j] = Mat(Mat(landmarks[j]));
  111. drawFacemarks(frame, landmarks[j], Scalar(0,0,255));
  112. }
  113. }
  114. double fps = (getTickFrequency()/(getTickCount() - __time__));
  115. sprintf(buff, "faces: %i %03.2f fps, fit:%03.0f ms",nfaces,fps,fittime*1000);
  116. text = buff;
  117. putText(frame, text, Point(20,40), FONT_HERSHEY_PLAIN , 2.0,Scalar::all(255), 2, 8);
  118. imshow("w", frame);
  119. waitKey(1); // waits to display frame
  120. }
  121. waitKey(0); // key press to close window
  122. }
  123. bool myDetector(InputArray image, OutputArray faces, CascadeClassifier *face_cascade)
  124. {
  125. Mat gray;
  126. if (image.channels() > 1)
  127. cvtColor(image, gray, COLOR_BGR2GRAY);
  128. else
  129. gray = image.getMat().clone();
  130. equalizeHist(gray, gray);
  131. std::vector<Rect> faces_;
  132. face_cascade->detectMultiScale(gray, faces_, 1.4, 2, CASCADE_SCALE_IMAGE, Size(30, 30));
  133. Mat(faces_).copyTo(faces);
  134. return true;
  135. }
  136. bool parseArguments(int argc, char** argv,
  137. String & cascade,
  138. String & model,
  139. String & video
  140. ){
  141. const String keys =
  142. "{ @c cascade | | (required) path to the cascade model file for the face detector }"
  143. "{ @m model | | (required) path to the trained model }"
  144. "{ @v video | | (required) path input video}"
  145. "{ help h usage ? | | facemark_lbf_fitting -cascade -model -video [-t]\n"
  146. " example: facemark_lbf_fitting ../face_cascade.xml ../LBF.model ../video.mp4}"
  147. ;
  148. CommandLineParser parser(argc, argv,keys);
  149. parser.about("hello");
  150. if (parser.has("help")){
  151. parser.printMessage();
  152. return false;
  153. }
  154. cascade = String(parser.get<String>("cascade"));
  155. model = String(parser.get<string>("model"));
  156. video = String(parser.get<string>("video"));
  157. if(cascade.empty() || model.empty() || video.empty() ){
  158. std::cerr << "one or more required arguments are not found" << '\n';
  159. cout<<"cascade : "<<cascade.c_str()<<endl;
  160. cout<<"model : "<<model.c_str()<<endl;
  161. cout<<"video : "<<video.c_str()<<endl;
  162. parser.printMessage();
  163. return false;
  164. }
  165. return true;
  166. }