test_facemark_aam.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /*Usage:
  36. download the opencv_extra from https://github.com/opencv/opencv_extra
  37. and then execute the following commands:
  38. export OPENCV_TEST_DATA_PATH=/home/opencv/opencv_extra/testdata
  39. <build_folder>/bin/opencv_test_face
  40. */
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. static bool customDetector( InputArray image, OutputArray ROIs, CascadeClassifier *face_detector){
  44. Mat gray;
  45. std::vector<Rect> & faces = *(std::vector<Rect>*) ROIs.getObj();
  46. faces.clear();
  47. if(image.channels()>1){
  48. cvtColor(image.getMat(),gray, COLOR_BGR2GRAY);
  49. }else{
  50. gray = image.getMat().clone();
  51. }
  52. equalizeHist( gray, gray );
  53. face_detector->detectMultiScale( gray, faces, 1.4, 2, CASCADE_SCALE_IMAGE, Size(30, 30) );
  54. return true;
  55. }
  56. TEST(CV_Face_FacemarkAAM, can_create_default) {
  57. FacemarkAAM::Params params;
  58. Ptr<FacemarkAAM> facemark;
  59. EXPECT_NO_THROW(facemark = FacemarkAAM::create(params));
  60. EXPECT_FALSE(facemark.empty());
  61. }
  62. TEST(CV_Face_FacemarkAAM, can_set_custom_detector) {
  63. string cascade_filename =
  64. cvtest::findDataFile("cascadeandhog/cascades/lbpcascade_frontalface.xml", true);
  65. CascadeClassifier face_detector;
  66. EXPECT_TRUE(face_detector.load(cascade_filename));
  67. Ptr<FacemarkAAM> facemark = FacemarkAAM::create();
  68. EXPECT_TRUE(facemark->setFaceDetector((cv::face::FN_FaceDetector)customDetector, &face_detector));
  69. }
  70. TEST(CV_Face_FacemarkAAM, test_workflow) {
  71. string i1 = cvtest::findDataFile("face/david1.jpg", true);
  72. string p1 = cvtest::findDataFile("face/david1.pts", true);
  73. string i2 = cvtest::findDataFile("face/david2.jpg", true);
  74. string p2 = cvtest::findDataFile("face/david2.pts", true);
  75. std::vector<string> images_train;
  76. images_train.push_back(i1);
  77. images_train.push_back(i2);
  78. std::vector<String> points_train;
  79. points_train.push_back(p1);
  80. points_train.push_back(p2);
  81. string cascade_filename =
  82. cvtest::findDataFile("cascadeandhog/cascades/lbpcascade_frontalface.xml", true);
  83. CascadeClassifier face_detector;
  84. EXPECT_TRUE(face_detector.load(cascade_filename));
  85. FacemarkAAM::Params params;
  86. params.n = 1;
  87. params.m = 1;
  88. params.verbose = false;
  89. params.save_model = false;
  90. Ptr<FacemarkAAM> facemark = FacemarkAAM::create(params);
  91. Mat image;
  92. std::vector<Point2f> landmarks;
  93. for(size_t i=0;i<images_train.size();i++)
  94. {
  95. image = imread(images_train[i].c_str());
  96. EXPECT_TRUE(loadFacePoints(points_train[i].c_str(),landmarks));
  97. EXPECT_TRUE(landmarks.size()>0);
  98. EXPECT_TRUE(facemark->addTrainingSample(image, landmarks));
  99. }
  100. EXPECT_NO_THROW(facemark->training());
  101. /*------------ Fitting Part ---------------*/
  102. EXPECT_TRUE(facemark->setFaceDetector((cv::face::FN_FaceDetector)customDetector, &face_detector));
  103. string image_filename = cvtest::findDataFile("face/david1.jpg", true);
  104. image = imread(image_filename.c_str());
  105. EXPECT_TRUE(!image.empty());
  106. std::vector<Rect> rects;
  107. std::vector<std::vector<Point2f> > facial_points;
  108. EXPECT_TRUE(facemark->getFaces(image, rects));
  109. EXPECT_TRUE(rects.size()>0);
  110. EXPECT_TRUE(facemark->fit(image, rects, facial_points));
  111. EXPECT_TRUE(facial_points[0].size()>0);
  112. /*------------ Test getData ---------------*/
  113. FacemarkAAM::Data data;
  114. EXPECT_TRUE(facemark->getData(&data));
  115. EXPECT_TRUE(data.s0.size()>0);
  116. }
  117. }} // namespace