/FaceAlignment/src/test/face_alignment_test2.cpp

https://github.com/fateleak/FriggaVision · C++ · 126 lines · 66 code · 15 blank · 45 comment · 4 complexity · 916f85e0320b2aa12c25849bfc49b2eb MD5 · raw file

  1. /*
  2. *
  3. * This file is part of the open-source SeetaFace engine, which includes three modules:
  4. * SeetaFace Detection, SeetaFace Alignment, and SeetaFace Identification.
  5. *
  6. * This file is an example of how to use SeetaFace engine for face alignment, the
  7. * face alignment method described in the following paper:
  8. *
  9. *
  10. * Coarse-to-Fine Auto-Encoder Networks (CFAN) for Real-Time Face Alignment,
  11. * Jie Zhang, Shiguang Shan, Meina Kan, Xilin Chen. In Proceeding of the
  12. * European Conference on Computer Vision (ECCV), 2014
  13. *
  14. *
  15. * Copyright (C) 2016, Visual Information Processing and Learning (VIPL) group,
  16. * Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
  17. *
  18. * The codes are mainly developed by Jie Zhang (a Ph.D supervised by Prof. Shiguang Shan)
  19. *
  20. * As an open-source face recognition engine: you can redistribute SeetaFace source codes
  21. * and/or modify it under the terms of the BSD 2-Clause License.
  22. *
  23. * You should have received a copy of the BSD 2-Clause License along with the software.
  24. * If not, see < https://opensource.org/licenses/BSD-2-Clause>.
  25. *
  26. * Contact Info: you can send an email to SeetaFace@vipl.ict.ac.cn for any problems.
  27. *
  28. * Note: the above information must be kept whenever or wherever the codes are used.
  29. *
  30. */
  31. #include <cstdint>
  32. #include <fstream>
  33. #include <iostream>
  34. #include <string>
  35. #include "cv.h"
  36. #include "highgui.h"
  37. #include "face_alignment.h"
  38. #ifdef _WIN32
  39. std::string DATA_DIR = "../../data/";
  40. std::string MODEL_DIR = "../../model/";
  41. #else
  42. std::string DATA_DIR = "./data/";
  43. std::string MODEL_DIR = "./model/";
  44. #endif
  45. int main2(int argc, char** argv)
  46. {
  47. const char* model_path = argv[1];
  48. const char* img_path = argv[2];
  49. const char* img_out_path = argv[3];
  50. int pos_x = atoi(argv[4]);
  51. int pos_y = atoi(argv[5]);
  52. int pos_w = atoi(argv[6]);
  53. int pos_h = atoi(argv[7]);
  54. // Initialize face alignment model
  55. seeta::FaceAlignment point_detector(model_path);
  56. //load image
  57. IplImage *img_grayscale = NULL;
  58. img_grayscale = cvLoadImage(img_path, 0);
  59. if (img_grayscale == NULL)
  60. {
  61. return 0;
  62. }
  63. IplImage *img_color = cvLoadImage(img_path, 1);
  64. int pts_num = 5;
  65. int im_width = img_grayscale->width;
  66. int im_height = img_grayscale->height;
  67. unsigned char* data = new unsigned char[im_width * im_height];
  68. unsigned char* data_ptr = data;
  69. unsigned char* image_data_ptr = (unsigned char*)img_grayscale->imageData;
  70. int h = 0;
  71. for (h = 0; h < im_height; h++) {
  72. memcpy(data_ptr, image_data_ptr, im_width);
  73. data_ptr += im_width;
  74. image_data_ptr += img_grayscale->widthStep;
  75. }
  76. seeta::ImageData image_data;
  77. image_data.data = data;
  78. image_data.width = im_width;
  79. image_data.height = im_height;
  80. image_data.num_channels = 1;
  81. // Detect faces
  82. // std::vector<seeta::FaceInfo> faces = detector.Detect(image_data);
  83. // int32_t face_num = static_cast<int32_t>(faces.size());
  84. // if (face_num == 0)
  85. // {
  86. // delete[]data;
  87. // cvReleaseImage(&img_grayscale);
  88. // cvReleaseImage(&img_color);
  89. // return 0;
  90. // }
  91. // Detect 5 facial landmarks
  92. seeta::FaceInfo info;
  93. info.bbox.x = pos_x;
  94. info.bbox.y = pos_y;
  95. info.bbox.width = pos_w;
  96. info.bbox.height = pos_h;
  97. seeta::FacialLandmark points[5];
  98. point_detector.PointDetectLandmarks(image_data, info, points);
  99. // Visualize the results
  100. cvRectangle(img_color, cvPoint(info.bbox.x, info.bbox.y), cvPoint(info.bbox.x + info.bbox.width - 1, info.bbox.y + info.bbox.height - 1), CV_RGB(255, 0, 0));
  101. for (int i = 0; i<pts_num; i++)
  102. {
  103. cvCircle(img_color, cvPoint(points[i].x, points[i].y), 2, CV_RGB(0, 255, 0), CV_FILLED);
  104. }
  105. cvSaveImage(img_out_path, img_color);
  106. // Release memory
  107. cvReleaseImage(&img_color);
  108. cvReleaseImage(&img_grayscale);
  109. delete[]data;
  110. return 0;
  111. }