facemark_train.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. /*
  5. This file was part of GSoC Project: Facemark API for OpenCV
  6. Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
  7. Student: Laksono Kurnianggoro
  8. Mentor: Delia Passalacqua
  9. */
  10. #ifndef __OPENCV_FACELANDMARKTRAIN_HPP__
  11. #define __OPENCV_FACELANDMARKTRAIN_HPP__
  12. /**
  13. @defgroup face Face Analysis
  14. - @ref tutorial_table_of_content_facemark
  15. - The Facemark API
  16. */
  17. #include "opencv2/face/facemark.hpp"
  18. #include "opencv2/objdetect.hpp"
  19. #include <vector>
  20. #include <string>
  21. namespace cv {
  22. namespace face {
  23. //! @addtogroup face
  24. //! @{
  25. typedef bool(*FN_FaceDetector)(InputArray, OutputArray, void* userData);
  26. struct CParams{
  27. String cascade; //!< the face detector
  28. double scaleFactor; //!< Parameter specifying how much the image size is reduced at each image scale.
  29. int minNeighbors; //!< Parameter specifying how many neighbors each candidate rectangle should have to retain it.
  30. Size minSize; //!< Minimum possible object size.
  31. Size maxSize; //!< Maximum possible object size.
  32. CV_EXPORTS CParams(
  33. String cascade_model,
  34. double sf = 1.1,
  35. int minN = 3,
  36. Size minSz = Size(30, 30),
  37. Size maxSz = Size()
  38. );
  39. CascadeClassifier face_cascade;
  40. };
  41. /** @brief Default face detector
  42. This function is mainly utilized by the implementation of a Facemark Algorithm.
  43. End users are advised to use function Facemark::getFaces which can be manually defined
  44. and circumvented to the algorithm by Facemark::setFaceDetector.
  45. @param image The input image to be processed.
  46. @param faces Output of the function which represent region of interest of the detected faces.
  47. Each face is stored in cv::Rect container.
  48. @param params detector parameters
  49. <B>Example of usage</B>
  50. @code
  51. std::vector<cv::Rect> faces;
  52. CParams params("haarcascade_frontalface_alt.xml");
  53. cv::face::getFaces(frame, faces, &params);
  54. for(int j=0;j<faces.size();j++){
  55. cv::rectangle(frame, faces[j], cv::Scalar(255,0,255));
  56. }
  57. cv::imshow("detection", frame);
  58. @endcode
  59. */
  60. CV_EXPORTS bool getFaces(InputArray image, OutputArray faces, CParams* params);
  61. CV_EXPORTS_W bool getFacesHAAR(InputArray image, OutputArray faces, const String& face_cascade_name);
  62. /** @brief A utility to load list of paths to training image and annotation file.
  63. @param imageList The specified file contains paths to the training images.
  64. @param annotationList The specified file contains paths to the training annotations.
  65. @param images The loaded paths of training images.
  66. @param annotations The loaded paths of annotation files.
  67. Example of usage:
  68. @code
  69. String imageFiles = "images_path.txt";
  70. String ptsFiles = "annotations_path.txt";
  71. std::vector<String> images_train;
  72. std::vector<String> landmarks_train;
  73. loadDatasetList(imageFiles,ptsFiles,images_train,landmarks_train);
  74. @endcode
  75. */
  76. CV_EXPORTS_W bool loadDatasetList(String imageList,
  77. String annotationList,
  78. std::vector<String> & images,
  79. std::vector<String> & annotations);
  80. /** @brief A utility to load facial landmark dataset from a single file.
  81. @param filename The filename of a file that contains the dataset information.
  82. Each line contains the filename of an image followed by
  83. pairs of x and y values of facial landmarks points separated by a space.
  84. Example
  85. @code
  86. /home/user/ibug/image_003_1.jpg 336.820955 240.864510 334.238298 260.922709 335.266918 ...
  87. /home/user/ibug/image_005_1.jpg 376.158428 230.845712 376.736984 254.924635 383.265403 ...
  88. @endcode
  89. @param images A vector where each element represent the filename of image in the dataset.
  90. Images are not loaded by default to save the memory.
  91. @param facePoints The loaded landmark points for all training data.
  92. @param delim Delimiter between each element, the default value is a whitespace.
  93. @param offset An offset value to adjust the loaded points.
  94. <B>Example of usage</B>
  95. @code
  96. cv::String imageFiles = "../data/images_train.txt";
  97. cv::String ptsFiles = "../data/points_train.txt";
  98. std::vector<String> images;
  99. std::vector<std::vector<Point2f> > facePoints;
  100. loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0f);
  101. @endcode
  102. */
  103. CV_EXPORTS_W bool loadTrainingData( String filename , std::vector<String> & images,
  104. OutputArray facePoints,
  105. char delim = ' ', float offset = 0.0f);
  106. /** @brief A utility to load facial landmark information from the dataset.
  107. @param imageList A file contains the list of image filenames in the training dataset.
  108. @param groundTruth A file contains the list of filenames
  109. where the landmarks points information are stored.
  110. The content in each file should follow the standard format (see face::loadFacePoints).
  111. @param images A vector where each element represent the filename of image in the dataset.
  112. Images are not loaded by default to save the memory.
  113. @param facePoints The loaded landmark points for all training data.
  114. @param offset An offset value to adjust the loaded points.
  115. <B>Example of usage</B>
  116. @code
  117. cv::String imageFiles = "../data/images_train.txt";
  118. cv::String ptsFiles = "../data/points_train.txt";
  119. std::vector<String> images;
  120. std::vector<std::vector<Point2f> > facePoints;
  121. loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0f);
  122. @endcode
  123. example of content in the images_train.txt
  124. @code
  125. /home/user/ibug/image_003_1.jpg
  126. /home/user/ibug/image_004_1.jpg
  127. /home/user/ibug/image_005_1.jpg
  128. /home/user/ibug/image_006.jpg
  129. @endcode
  130. example of content in the points_train.txt
  131. @code
  132. /home/user/ibug/image_003_1.pts
  133. /home/user/ibug/image_004_1.pts
  134. /home/user/ibug/image_005_1.pts
  135. /home/user/ibug/image_006.pts
  136. @endcode
  137. */
  138. CV_EXPORTS_W bool loadTrainingData( String imageList, String groundTruth,
  139. std::vector<String> & images,
  140. OutputArray facePoints,
  141. float offset = 0.0f);
  142. /** @brief This function extracts the data for training from .txt files which contains the corresponding image name and landmarks.
  143. *The first file in each file should give the path of the image whose
  144. *landmarks are being described in the file. Then in the subsequent
  145. *lines there should be coordinates of the landmarks in the image
  146. *i.e each line should be of the form x,y
  147. *where x represents the x coordinate of the landmark and y represents
  148. *the y coordinate of the landmark.
  149. *
  150. *For reference you can see the files as provided in the
  151. *<a href="http://www.ifp.illinois.edu/~vuongle2/helen/">HELEN dataset</a>
  152. *
  153. * @param filename A vector of type cv::String containing name of the .txt files.
  154. * @param trainlandmarks A vector of type cv::Point2f that would store shape or landmarks of all images.
  155. * @param trainimages A vector of type cv::String which stores the name of images whose landmarks are tracked
  156. * @returns A boolean value. It returns true when it reads the data successfully and false otherwise
  157. */
  158. CV_EXPORTS_W bool loadTrainingData(std::vector<String> filename,std::vector< std::vector<Point2f> >
  159. &trainlandmarks,std::vector<String> & trainimages);
  160. /** @brief A utility to load facial landmark information from a given file.
  161. @param filename The filename of file contains the facial landmarks data.
  162. @param points The loaded facial landmark points.
  163. @param offset An offset value to adjust the loaded points.
  164. <B>Example of usage</B>
  165. @code
  166. std::vector<Point2f> points;
  167. face::loadFacePoints("filename.txt", points, 0.0f);
  168. @endcode
  169. The annotation file should follow the default format which is
  170. @code
  171. version: 1
  172. n_points: 68
  173. {
  174. 212.716603 499.771793
  175. 230.232816 566.290071
  176. ...
  177. }
  178. @endcode
  179. where n_points is the number of points considered
  180. and each point is represented as its position in x and y.
  181. */
  182. CV_EXPORTS_W bool loadFacePoints( String filename, OutputArray points,
  183. float offset = 0.0f);
  184. /** @brief Utility to draw the detected facial landmark points
  185. @param image The input image to be processed.
  186. @param points Contains the data of points which will be drawn.
  187. @param color The color of points in BGR format represented by cv::Scalar.
  188. <B>Example of usage</B>
  189. @code
  190. std::vector<Rect> faces;
  191. std::vector<std::vector<Point2f> > landmarks;
  192. facemark->getFaces(img, faces);
  193. facemark->fit(img, faces, landmarks);
  194. for(int j=0;j<rects.size();j++){
  195. face::drawFacemarks(frame, landmarks[j], Scalar(0,0,255));
  196. }
  197. @endcode
  198. */
  199. CV_EXPORTS_W void drawFacemarks( InputOutputArray image, InputArray points,
  200. Scalar color = Scalar(255,0,0));
  201. /** @brief Abstract base class for trainable facemark models
  202. To utilize this API in your program, please take a look at the @ref tutorial_table_of_content_facemark
  203. ### Description
  204. The AAM and LBF facemark models in OpenCV are derived from the abstract base class FacemarkTrain, which
  205. provides a unified access to those facemark algorithms in OpenCV.
  206. Here is an example on how to declare facemark algorithm:
  207. @code
  208. // Using Facemark in your code:
  209. Ptr<Facemark> facemark = FacemarkLBF::create();
  210. @endcode
  211. The typical pipeline for facemark detection is listed as follows:
  212. - (Non-mandatory) Set a user defined face detection using FacemarkTrain::setFaceDetector.
  213. The facemark algorithms are designed to fit the facial points into a face.
  214. Therefore, the face information should be provided to the facemark algorithm.
  215. Some algorithms might provides a default face recognition function.
  216. However, the users might prefer to use their own face detector to obtains the best possible detection result.
  217. - (Non-mandatory) Training the model for a specific algorithm using FacemarkTrain::training.
  218. In this case, the model should be automatically saved by the algorithm.
  219. If the user already have a trained model, then this part can be omitted.
  220. - Load the trained model using Facemark::loadModel.
  221. - Perform the fitting via the Facemark::fit.
  222. */
  223. class CV_EXPORTS_W FacemarkTrain : public Facemark
  224. {
  225. public:
  226. /** @brief Add one training sample to the trainer.
  227. @param image Input image.
  228. @param landmarks The ground-truth of facial landmarks points corresponds to the image.
  229. <B>Example of usage</B>
  230. @code
  231. String imageFiles = "../data/images_train.txt";
  232. String ptsFiles = "../data/points_train.txt";
  233. std::vector<String> images_train;
  234. std::vector<String> landmarks_train;
  235. // load the list of dataset: image paths and landmark file paths
  236. loadDatasetList(imageFiles,ptsFiles,images_train,landmarks_train);
  237. Mat image;
  238. std::vector<Point2f> facial_points;
  239. for(size_t i=0;i<images_train.size();i++){
  240. image = imread(images_train[i].c_str());
  241. loadFacePoints(landmarks_train[i],facial_points);
  242. facemark->addTrainingSample(image, facial_points);
  243. }
  244. @endcode
  245. The contents in the training files should follows the standard format.
  246. Here are examples for the contents in these files.
  247. example of content in the images_train.txt
  248. @code
  249. /home/user/ibug/image_003_1.jpg
  250. /home/user/ibug/image_004_1.jpg
  251. /home/user/ibug/image_005_1.jpg
  252. /home/user/ibug/image_006.jpg
  253. @endcode
  254. example of content in the points_train.txt
  255. @code
  256. /home/user/ibug/image_003_1.pts
  257. /home/user/ibug/image_004_1.pts
  258. /home/user/ibug/image_005_1.pts
  259. /home/user/ibug/image_006.pts
  260. @endcode
  261. */
  262. virtual bool addTrainingSample(InputArray image, InputArray landmarks)=0;
  263. /** @brief Trains a Facemark algorithm using the given dataset.
  264. Before the training process, training samples should be added to the trainer
  265. using face::addTrainingSample function.
  266. @param parameters Optional extra parameters (algorithm dependent).
  267. <B>Example of usage</B>
  268. @code
  269. FacemarkLBF::Params params;
  270. params.model_filename = "ibug68.model"; // filename to save the trained model
  271. Ptr<Facemark> facemark = FacemarkLBF::create(params);
  272. // add training samples (see Facemark::addTrainingSample)
  273. facemark->training();
  274. @endcode
  275. */
  276. virtual void training(void* parameters=0)=0;
  277. /** @brief Set a user defined face detector for the Facemark algorithm.
  278. @param detector The user defined face detector function
  279. @param userData Detector parameters
  280. <B>Example of usage</B>
  281. @code
  282. MyDetectorParameters detectorParameters(...);
  283. facemark->setFaceDetector(myDetector, &detectorParameters);
  284. @endcode
  285. Example of a user defined face detector
  286. @code
  287. bool myDetector( InputArray image, OutputArray faces, void* userData)
  288. {
  289. MyDetectorParameters* params = (MyDetectorParameters*)userData;
  290. // -------- do something --------
  291. }
  292. @endcode
  293. TODO Lifetime of detector parameters is uncontrolled. Rework interface design to "Ptr<FaceDetector>".
  294. */
  295. virtual bool setFaceDetector(FN_FaceDetector detector, void* userData = 0)=0;
  296. /** @brief Detect faces from a given image using default or user defined face detector.
  297. Some Algorithm might not provide a default face detector.
  298. @param image Input image.
  299. @param faces Output of the function which represent region of interest of the detected faces. Each face is stored in cv::Rect container.
  300. <B>Example of usage</B>
  301. @code
  302. std::vector<cv::Rect> faces;
  303. facemark->getFaces(img, faces);
  304. for(int j=0;j<faces.size();j++){
  305. cv::rectangle(img, faces[j], cv::Scalar(255,0,255));
  306. }
  307. @endcode
  308. */
  309. virtual bool getFaces(InputArray image, OutputArray faces)=0;
  310. /** @brief Get data from an algorithm
  311. @param items The obtained data, algorithm dependent.
  312. <B>Example of usage</B>
  313. @code
  314. Ptr<FacemarkAAM> facemark = FacemarkAAM::create();
  315. facemark->loadModel("AAM.yml");
  316. FacemarkAAM::Data data;
  317. facemark->getData(&data);
  318. std::vector<Point2f> s0 = data.s0;
  319. cout<<s0<<endl;
  320. @endcode
  321. */
  322. virtual bool getData(void * items=0)=0; // FIXIT
  323. }; /* Facemark*/
  324. //! @}
  325. } /* namespace face */
  326. } /* namespace cv */
  327. #endif //__OPENCV_FACELANDMARKTRAIN_HPP__