face_detection.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "opencv2/imgproc/imgproc.hpp"
  15. #include "ultra_infer/vision/visualize/visualize.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. cv::Mat VisFaceDetection(const cv::Mat &im, const FaceDetectionResult &result,
  19. int line_size, float font_size) {
  20. auto color_map = GenerateColorMap();
  21. int h = im.rows;
  22. int w = im.cols;
  23. auto vis_im = im.clone();
  24. bool vis_landmarks = false;
  25. if ((result.landmarks_per_face > 0) &&
  26. (result.boxes.size() * result.landmarks_per_face ==
  27. result.landmarks.size())) {
  28. vis_landmarks = true;
  29. }
  30. for (size_t i = 0; i < result.boxes.size(); ++i) {
  31. cv::Rect rect(result.boxes[i][0], result.boxes[i][1],
  32. result.boxes[i][2] - result.boxes[i][0],
  33. result.boxes[i][3] - result.boxes[i][1]);
  34. int color_id = i % 333;
  35. int c0 = color_map[3 * color_id + 0];
  36. int c1 = color_map[3 * color_id + 1];
  37. int c2 = color_map[3 * color_id + 2];
  38. cv::Scalar rect_color = cv::Scalar(c0, c1, c2);
  39. std::string text = std::to_string(result.scores[i]);
  40. if (text.size() > 4) {
  41. text = text.substr(0, 4);
  42. }
  43. int font = cv::FONT_HERSHEY_SIMPLEX;
  44. cv::Size text_size = cv::getTextSize(text, font, font_size, 1, nullptr);
  45. cv::Point origin;
  46. origin.x = rect.x;
  47. origin.y = rect.y;
  48. cv::Rect text_background =
  49. cv::Rect(result.boxes[i][0], result.boxes[i][1] - text_size.height,
  50. text_size.width, text_size.height);
  51. cv::rectangle(vis_im, rect, rect_color, line_size);
  52. cv::putText(vis_im, text, origin, font, font_size,
  53. cv::Scalar(255, 255, 255), 1);
  54. // vis landmarks (if have)
  55. if (vis_landmarks) {
  56. cv::Scalar landmark_color = rect_color;
  57. for (size_t j = 0; j < result.landmarks_per_face; ++j) {
  58. cv::Point landmark;
  59. landmark.x = static_cast<int>(
  60. result.landmarks[i * result.landmarks_per_face + j][0]);
  61. landmark.y = static_cast<int>(
  62. result.landmarks[i * result.landmarks_per_face + j][1]);
  63. cv::circle(vis_im, landmark, line_size, landmark_color, -1);
  64. }
  65. }
  66. }
  67. return vis_im;
  68. }
  69. // Default only support visualize num_classes <= 1000
  70. // If need to visualize num_classes > 1000
  71. // Please call Visualize::GetColorMap(num_classes) first
  72. cv::Mat Visualize::VisFaceDetection(const cv::Mat &im,
  73. const FaceDetectionResult &result,
  74. int line_size, float font_size) {
  75. FDWARNING
  76. << "DEPRECATED: ultra_infer::vision::Visualize::VisFaceDetection is "
  77. "deprecated, please use ultra_infer::vision:VisFaceDetection "
  78. "function instead."
  79. << std::endl;
  80. auto color_map = GetColorMap();
  81. int h = im.rows;
  82. int w = im.cols;
  83. auto vis_im = im.clone();
  84. bool vis_landmarks = false;
  85. if ((result.landmarks_per_face > 0) &&
  86. (result.boxes.size() * result.landmarks_per_face ==
  87. result.landmarks.size())) {
  88. vis_landmarks = true;
  89. }
  90. for (size_t i = 0; i < result.boxes.size(); ++i) {
  91. cv::Rect rect(result.boxes[i][0], result.boxes[i][1],
  92. result.boxes[i][2] - result.boxes[i][0],
  93. result.boxes[i][3] - result.boxes[i][1]);
  94. int color_id = i % 333;
  95. int c0 = color_map[3 * color_id + 0];
  96. int c1 = color_map[3 * color_id + 1];
  97. int c2 = color_map[3 * color_id + 2];
  98. cv::Scalar rect_color = cv::Scalar(c0, c1, c2);
  99. std::string text = std::to_string(result.scores[i]);
  100. if (text.size() > 4) {
  101. text = text.substr(0, 4);
  102. }
  103. int font = cv::FONT_HERSHEY_SIMPLEX;
  104. cv::Size text_size = cv::getTextSize(text, font, font_size, 1, nullptr);
  105. cv::Point origin;
  106. origin.x = rect.x;
  107. origin.y = rect.y;
  108. cv::Rect text_background =
  109. cv::Rect(result.boxes[i][0], result.boxes[i][1] - text_size.height,
  110. text_size.width, text_size.height);
  111. cv::rectangle(vis_im, rect, rect_color, line_size);
  112. cv::putText(vis_im, text, origin, font, font_size,
  113. cv::Scalar(255, 255, 255), 1);
  114. // vis landmarks (if have)
  115. if (vis_landmarks) {
  116. cv::Scalar landmark_color = rect_color;
  117. for (size_t j = 0; j < result.landmarks_per_face; ++j) {
  118. cv::Point landmark;
  119. landmark.x = static_cast<int>(
  120. result.landmarks[i * result.landmarks_per_face + j][0]);
  121. landmark.y = static_cast<int>(
  122. result.landmarks[i * result.landmarks_per_face + j][1]);
  123. cv::circle(vis_im, landmark, line_size, landmark_color, -1);
  124. }
  125. }
  126. }
  127. return vis_im;
  128. }
  129. } // namespace vision
  130. } // namespace ultra_infer