visualize.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #pragma once
  15. #include "opencv2/imgproc/imgproc.hpp"
  16. #include "ultra_infer/vision/common/result.h"
  17. #include "ultra_infer/vision/tracking/pptracking/model.h"
  18. namespace ultra_infer {
  19. /** \brief All C++ UltraInfer Vision Models APIs are defined inside this
  20. * namespace
  21. *
  22. */
  23. namespace vision {
  24. class ULTRAINFER_DECL Visualize {
  25. public:
  26. static int num_classes_;
  27. static std::vector<int> color_map_;
  28. static const std::vector<int> &GetColorMap(int num_classes = 1000);
  29. static cv::Mat VisDetection(const cv::Mat &im, const DetectionResult &result,
  30. float score_threshold = 0.0, int line_size = 1,
  31. float font_size = 0.5f);
  32. static cv::Mat VisPerception(const cv::Mat &im,
  33. const PerceptionResult &result,
  34. const std::string &config_file,
  35. float score_threshold = 0.0, int line_size = 1,
  36. float font_size = 0.5f);
  37. static cv::Mat VisFaceDetection(const cv::Mat &im,
  38. const FaceDetectionResult &result,
  39. int line_size = 1, float font_size = 0.5f);
  40. static cv::Mat VisSegmentation(const cv::Mat &im,
  41. const SegmentationResult &result);
  42. static cv::Mat VisMattingAlpha(const cv::Mat &im, const MattingResult &result,
  43. bool remove_small_connected_area = false);
  44. static cv::Mat RemoveSmallConnectedArea(const cv::Mat &alpha_pred,
  45. float threshold);
  46. static cv::Mat
  47. SwapBackgroundMatting(const cv::Mat &im, const cv::Mat &background,
  48. const MattingResult &result,
  49. bool remove_small_connected_area = false);
  50. static cv::Mat SwapBackgroundSegmentation(const cv::Mat &im,
  51. const cv::Mat &background,
  52. int background_label,
  53. const SegmentationResult &result);
  54. static cv::Mat VisOcr(const cv::Mat &srcimg, const OCRResult &ocr_result);
  55. static cv::Mat VisCURVEOcr(const cv::Mat &srcimg,
  56. const OCRCURVEResult &ocr_result);
  57. };
  58. std::vector<int> GenerateColorMap(int num_classes = 1000);
  59. cv::Mat RemoveSmallConnectedArea(const cv::Mat &alpha_pred, float threshold);
  60. /** \brief Show the visualized results for detection models
  61. *
  62. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  63. * with layout HWC, BGR format \param[in] result the result produced by model
  64. * \param[in] score_threshold threshold for result scores, the bounding box will
  65. * not be shown if the score is less than score_threshold \param[in] line_size
  66. * line size for bounding boxes \param[in] font_size font size for text \return
  67. * cv::Mat type stores the visualized results
  68. */
  69. ULTRAINFER_DECL cv::Mat VisDetection(const cv::Mat &im,
  70. const DetectionResult &result,
  71. float score_threshold = 0.0,
  72. int line_size = 1, float font_size = 0.5f);
  73. /** \brief Show the visualized results with custom labels for detection models
  74. *
  75. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  76. * with layout HWC, BGR format \param[in] result the result produced by model
  77. * \param[in] labels the visualized result will show the bounding box contain
  78. * class label \param[in] score_threshold threshold for result scores, the
  79. * bounding box will not be shown if the score is less than score_threshold
  80. * \param[in] line_size line size for bounding boxes
  81. * \param[in] font_size font size for text
  82. * \param[in] font_color font color for bounding text
  83. * \param[in] font_thickness font thickness for text
  84. * \return cv::Mat type stores the visualized results
  85. */
  86. ULTRAINFER_DECL cv::Mat VisDetection(
  87. const cv::Mat &im, const DetectionResult &result,
  88. const std::vector<std::string> &labels, float score_threshold = 0.0,
  89. int line_size = 1, float font_size = 0.5f,
  90. std::vector<int> font_color = {255, 255, 255}, int font_thickness = 1);
  91. /** \brief Show the visualized results with custom labels for detection models
  92. *
  93. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  94. * with layout HWC, BGR format \param[in] result the result produced by model
  95. * \param[in] labels the visualized result will show the bounding box contain
  96. * class label \param[in] score_threshold threshold for result scores, the
  97. * bounding box will not be shown if the score is less than score_threshold
  98. * \param[in] line_size line size for bounding boxes
  99. * \param[in] font_size font size for text
  100. * \return cv::Mat type stores the visualized results
  101. */
  102. ULTRAINFER_DECL cv::Mat
  103. VisPerception(const cv::Mat &im, const PerceptionResult &result,
  104. const std::string &config_file, float score_threshold = 0.0,
  105. int line_size = 1, float font_size = 0.5f);
  106. /** \brief Show the visualized results for classification models
  107. *
  108. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  109. * with layout HWC, BGR format \param[in] result the result produced by model
  110. * \param[in] top_k the length of return values, e.g., if topk==2, the result
  111. * will include the 2 most possible class label for input image. \param[in]
  112. * score_threshold threshold for top_k scores, the class will not be shown if
  113. * the score is less than score_threshold \param[in] font_size font size \return
  114. * cv::Mat type stores the visualized results
  115. */
  116. ULTRAINFER_DECL cv::Mat VisClassification(const cv::Mat &im,
  117. const ClassifyResult &result,
  118. int top_k = 5,
  119. float score_threshold = 0.0f,
  120. float font_size = 0.5f);
  121. /** \brief Show the visualized results with custom labels for classification
  122. * models
  123. *
  124. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  125. * with layout HWC, BGR format \param[in] result the result produced by model
  126. * \param[in] labels custom labels for user, the visualized result will show the
  127. * corresponding custom labels \param[in] top_k the length of return values,
  128. * e.g., if topk==2, the result will include the 2 most possible class label for
  129. * input image. \param[in] score_threshold threshold for top_k scores, the class
  130. * will not be shown if the score is less than score_threshold \param[in]
  131. * font_size font size \return cv::Mat type stores the visualized results
  132. */
  133. ULTRAINFER_DECL cv::Mat
  134. VisClassification(const cv::Mat &im, const ClassifyResult &result,
  135. const std::vector<std::string> &labels, int top_k = 5,
  136. float score_threshold = 0.0f, float font_size = 0.5f);
  137. /** \brief Show the visualized results for face detection models
  138. *
  139. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  140. * with layout HWC, BGR format \param[in] result the result produced by model
  141. * \param[in] line_size line size for bounding boxes
  142. * \param[in] font_size font size for text
  143. * \return cv::Mat type stores the visualized results
  144. */
  145. ULTRAINFER_DECL cv::Mat VisFaceDetection(const cv::Mat &im,
  146. const FaceDetectionResult &result,
  147. int line_size = 1,
  148. float font_size = 0.5f);
  149. /** \brief Show the visualized results for face alignment models
  150. *
  151. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  152. * with layout HWC, BGR format \param[in] result the result produced by model
  153. * \param[in] line_size line size for circle point
  154. * \return cv::Mat type stores the visualized results
  155. */
  156. ULTRAINFER_DECL cv::Mat VisFaceAlignment(const cv::Mat &im,
  157. const FaceAlignmentResult &result,
  158. int line_size = 1);
  159. /** \brief Show the visualized results for segmentation models
  160. *
  161. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  162. * with layout HWC, BGR format \param[in] result the result produced by model
  163. * \param[in] weight transparent weight of visualized result image
  164. * \return cv::Mat type stores the visualized results
  165. */
  166. ULTRAINFER_DECL cv::Mat VisSegmentation(const cv::Mat &im,
  167. const SegmentationResult &result,
  168. float weight = 0.5);
  169. /** \brief Show the visualized results for matting models
  170. *
  171. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  172. * with layout HWC, BGR format \param[in] result the result produced by model
  173. * \param[in] transparent_background if transparent_background==true, the
  174. * background will with transparent color \param[in] transparent_threshold since
  175. * the alpha value in MattringResult is a float between [0, 1],
  176. * transparent_threshold is used to filter background pixel \param[in]
  177. * remove_small_connected_area if remove_small_connected_area==true, the
  178. * visualized result will not include the small connected areas \return cv::Mat
  179. * type stores the visualized results
  180. */
  181. ULTRAINFER_DECL cv::Mat VisMatting(const cv::Mat &im,
  182. const MattingResult &result,
  183. bool transparent_background = false,
  184. float transparent_threshold = 0.999,
  185. bool remove_small_connected_area = false);
  186. /** \brief Show the visualized results for Ocr models
  187. *
  188. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  189. * with layout HWC, BGR format \param[in] result the result produced by model
  190. * \return cv::Mat type stores the visualized results
  191. */
  192. ULTRAINFER_DECL cv::Mat VisOcr(const cv::Mat &im, const OCRResult &ocr_result,
  193. const float score_threshold = 0);
  194. ULTRAINFER_DECL cv::Mat VisCURVEOcr(const cv::Mat &im,
  195. const OCRCURVEResult &ocr_result,
  196. const float score_threshold = 0);
  197. ULTRAINFER_DECL cv::Mat VisMOT(const cv::Mat &img, const MOTResult &results,
  198. float score_threshold = 0.0f,
  199. tracking::TrailRecorder *recorder = nullptr);
  200. /** \brief Swap the image background with MattingResult
  201. *
  202. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  203. * with layout HWC, BGR format \param[in] background the background image data,
  204. * comes from cv::imread(), is a 3-D array with layout HWC, BGR format
  205. * \param[in] result the MattingResult produced by model
  206. * \param[in] remove_small_connected_area if remove_small_connected_area==true,
  207. * the visualized result will not include the small connected areas \return
  208. * cv::Mat type stores the visualized results
  209. */
  210. ULTRAINFER_DECL cv::Mat
  211. SwapBackground(const cv::Mat &im, const cv::Mat &background,
  212. const MattingResult &result,
  213. bool remove_small_connected_area = false);
  214. /** \brief Swap the image background with SegmentationResult
  215. *
  216. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  217. * with layout HWC, BGR format \param[in] background the background image data,
  218. * comes from cv::imread(), is a 3-D array with layout HWC, BGR format
  219. * \param[in] result the SegmentationResult produced by model
  220. * \param[in] background_label the background label number in SegmentationResult
  221. * \return cv::Mat type stores the visualized results
  222. */
  223. ULTRAINFER_DECL cv::Mat SwapBackground(const cv::Mat &im,
  224. const cv::Mat &background,
  225. const SegmentationResult &result,
  226. int background_label);
  227. /** \brief Show the visualized results for key point detection models
  228. *
  229. * \param[in] im the input image data, comes from cv::imread(), is a 3-D array
  230. * with layout HWC, BGR format \param[in] results the result produced by model
  231. * \param[in] conf_threshold threshold for result scores, the result will not be
  232. * shown if the score is less than conf_threshold \return cv::Mat type stores
  233. * the visualized results
  234. */
  235. ULTRAINFER_DECL cv::Mat
  236. VisKeypointDetection(const cv::Mat &im, const KeyPointDetectionResult &results,
  237. float conf_threshold = 0.5f);
  238. ULTRAINFER_DECL cv::Mat VisHeadPose(const cv::Mat &im,
  239. const HeadPoseResult &result, int size = 50,
  240. int line_size = 1);
  241. } // namespace vision
  242. } // namespace ultra_infer