test_face_align.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. using namespace cv::face;
  7. static bool myDetector( InputArray image, OutputArray ROIs, CascadeClassifier* face_cascade)
  8. {
  9. Mat gray;
  10. std::vector<Rect> faces;
  11. if(image.channels()>1){
  12. cvtColor(image.getMat(),gray,COLOR_BGR2GRAY);
  13. }
  14. else{
  15. gray = image.getMat().clone();
  16. }
  17. equalizeHist( gray, gray );
  18. face_cascade->detectMultiScale( gray, faces, 1.1, 3, 0, Size(30, 30) );
  19. Mat(faces).copyTo(ROIs);
  20. return true;
  21. }
  22. TEST(CV_Face_FacemarkKazemi, can_create_default) {
  23. string cascade_name = cvtest::findDataFile("face/lbpcascade_frontalface_improved.xml", true);
  24. string configfile_name = cvtest::findDataFile("face/config.xml", true);
  25. CascadeClassifier face_cascade;
  26. EXPECT_TRUE(face_cascade.load(cascade_name));
  27. FacemarkKazemi::Params params;
  28. params.configfile = configfile_name;
  29. Ptr<FacemarkKazemi> facemark;
  30. EXPECT_NO_THROW(facemark = FacemarkKazemi::create(params));
  31. EXPECT_TRUE(facemark->setFaceDetector((cv::face::FN_FaceDetector)myDetector, &face_cascade));
  32. EXPECT_FALSE(facemark.empty());
  33. }
  34. TEST(CV_Face_FacemarkKazemi, can_loadTrainingData) {
  35. string filename = cvtest::findDataFile("face/lbpcascade_frontalface_improved.xml", true);
  36. string configfile_name = cvtest::findDataFile("face/config.xml", true);
  37. CascadeClassifier face_cascade;
  38. EXPECT_TRUE(face_cascade.load(filename));
  39. FacemarkKazemi::Params params;
  40. params.configfile = configfile_name;
  41. Ptr<FacemarkKazemi> facemark;
  42. EXPECT_NO_THROW(facemark = FacemarkKazemi::create(params));
  43. EXPECT_TRUE(facemark->setFaceDetector((cv::face::FN_FaceDetector)myDetector, &face_cascade));
  44. vector<String> filenames;
  45. filename = cvtest::findDataFile("face/1.txt", true);
  46. filenames.push_back(filename);
  47. filename = cvtest::findDataFile("face/2.txt", true);
  48. filenames.push_back(filename);
  49. vector<String> imagenames;
  50. vector< vector<Point2f> > trainlandmarks,Trainlandmarks;
  51. vector<Rect> rectangles;
  52. //Test getData function
  53. EXPECT_NO_THROW(loadTrainingData(filenames,trainlandmarks,imagenames));
  54. vector<Mat> trainimages;
  55. for(unsigned long i=0;i<imagenames.size();i++){
  56. string img = cvtest::findDataFile(imagenames[i], true);
  57. Mat src = imread(img);
  58. EXPECT_TRUE(!src.empty());
  59. trainimages.push_back(src);
  60. Trainlandmarks.push_back(trainlandmarks[i]);
  61. }
  62. string modelfilename = "face_landmark_model.dat";
  63. Size scale = Size(460,460);
  64. EXPECT_TRUE(facemark->training(trainimages,Trainlandmarks,configfile_name,scale,modelfilename));
  65. }
  66. TEST(CV_Face_FacemarkKazemi, can_detect_landmarks) {
  67. string cascade_name = cvtest::findDataFile("face/lbpcascade_frontalface_improved.xml", true);
  68. CascadeClassifier face_cascade;
  69. face_cascade.load(cascade_name);
  70. FacemarkKazemi::Params params;
  71. Ptr<FacemarkKazemi> facemark;
  72. EXPECT_NO_THROW(facemark = FacemarkKazemi::create(params));
  73. EXPECT_TRUE(facemark->setFaceDetector((cv::face::FN_FaceDetector)myDetector, &face_cascade));
  74. string imgname = cvtest::findDataFile("face/detect.jpg");
  75. string modelfilename = cvtest::findDataFile("face/face_landmark_model.dat",true);
  76. Mat img = imread(imgname);
  77. EXPECT_TRUE(!img.empty());
  78. EXPECT_FALSE(facemark.empty());
  79. EXPECT_NO_THROW(facemark->loadModel(modelfilename));
  80. vector<Rect> faces;
  81. //Detect faces in the current image
  82. EXPECT_TRUE(facemark->getFaces(img,faces));
  83. //vector to store the landmarks of all the faces in the image
  84. vector< vector<Point2f> > shapes;
  85. EXPECT_NO_THROW(facemark->fit(img,faces,shapes));
  86. shapes.clear();
  87. }
  88. }} // namespace