__init__.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Copyright (c) 2024 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. from __future__ import absolute_import
  15. import logging
  16. from ... import c_lib_wrap as C
  17. import cv2
  18. def vis_detection(
  19. im_data,
  20. det_result,
  21. labels=[],
  22. score_threshold=0.0,
  23. line_size=1,
  24. font_size=0.5,
  25. font_color=[255, 255, 255],
  26. font_thickness=1,
  27. ):
  28. """Show the visualized results for detection models
  29. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  30. :param det_result: the result produced by model
  31. :param labels: (list of str) the visualized result will show the bounding box contain class label
  32. :param score_threshold: (float) score_threshold threshold for result scores, the bounding box will not be shown if the score is less than score_threshold
  33. :param line_size: (float) line_size line size for bounding boxes
  34. :param font_size: (float) font_size font size for text
  35. :param font_color: (list of int) font_color for text
  36. :param font_thickness: (int) font_thickness for text
  37. :return: (numpy.ndarray) image with visualized results
  38. """
  39. return C.vision.vis_detection(
  40. im_data,
  41. det_result,
  42. labels,
  43. score_threshold,
  44. line_size,
  45. font_size,
  46. font_color,
  47. font_thickness,
  48. )
  49. def vis_perception(
  50. im_data, det_result, config_file, score_threshold=0.0, line_size=1, font_size=0.5
  51. ):
  52. """Show the visualized results for 3d detection models
  53. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  54. :param det_result: the result produced by model
  55. :param config_file: the config file for detection and visualization
  56. :param score_threshold: (float) score_threshold threshold for result scores, the bounding box will not be shown if the score is less than score_threshold
  57. :param line_size: (float) line_size line size for bounding boxes
  58. :param font_size: (float) font_size font size for text
  59. :return: (numpy.ndarray) image with visualized results
  60. """
  61. return C.vision.vis_perception(
  62. im_data, det_result, config_file, score_threshold, line_size, font_size
  63. )
  64. def vis_keypoint_detection(im_data, keypoint_det_result, conf_threshold=0.5):
  65. """Show the visualized results for keypoint detection models
  66. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  67. :param keypoint_det_result: the result produced by model
  68. :param conf_threshold: (float) conf_threshold threshold for result scores, the bounding box will not be shown if the score is less than conf_threshold
  69. :return: (numpy.ndarray) image with visualized results
  70. """
  71. return C.vision.Visualize.vis_keypoint_detection(
  72. im_data, keypoint_det_result, conf_threshold
  73. )
  74. def vis_face_detection(im_data, face_det_result, line_size=1, font_size=0.5):
  75. """Show the visualized results for face detection models
  76. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  77. :param face_det_result: the result produced by model
  78. :param line_size: (float) line_size line size for bounding boxes
  79. :param font_size: (float) font_size font size for text
  80. :return: (numpy.ndarray) image with visualized results
  81. """
  82. return C.vision.vis_face_detection(im_data, face_det_result, line_size, font_size)
  83. def vis_face_alignment(im_data, face_align_result, line_size=1):
  84. """Show the visualized results for face alignment models
  85. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  86. :param face_align_result: the result produced by model
  87. :param line_size: (float)line_size line size for circle point
  88. :return: (numpy.ndarray) image with visualized results
  89. """
  90. return C.vision.vis_face_alignment(im_data, face_align_result, line_size)
  91. def vis_segmentation(im_data, seg_result, weight=0.5):
  92. """Show the visualized results for segmentation models
  93. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  94. :param seg_result: the result produced by model
  95. :param weight: (float)transparent weight of visualized result image
  96. :return: (numpy.ndarray) image with visualized results
  97. """
  98. return C.vision.vis_segmentation(im_data, seg_result, weight)
  99. def vis_matting_alpha(im_data, matting_result, remove_small_connected_area=False):
  100. logging.warning(
  101. "DEPRECATED: ultra_infer.vision.vis_matting_alpha is deprecated, please use ultra_infer.vision.vis_matting function instead."
  102. )
  103. return C.vision.vis_matting(im_data, matting_result, remove_small_connected_area)
  104. def vis_matting(
  105. im_data,
  106. matting_result,
  107. transparent_background=False,
  108. transparent_threshold=0.99,
  109. remove_small_connected_area=False,
  110. ):
  111. """Show the visualized results for matting models
  112. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  113. :param matting_result: the result produced by model
  114. :param transparent_background: whether visualizing matting result with transparent background
  115. :param transparent_threshold: since the alpha value in MattringResult is a float between [0, 1], transparent_threshold is used to filter background pixel
  116. :param remove_small_connected_area: (bool) if remove_small_connected_area==True, the visualized result will not include the small connected areas
  117. :return: (numpy.ndarray) image with visualized results
  118. """
  119. return C.vision.vis_matting(
  120. im_data,
  121. matting_result,
  122. transparent_background,
  123. transparent_threshold,
  124. remove_small_connected_area,
  125. )
  126. def swap_background_matting(
  127. im_data, background, result, remove_small_connected_area=False
  128. ):
  129. logging.warning(
  130. "DEPRECATED: ultra_infer.vision.swap_background_matting is deprecated, please use ultra_infer.vision.swap_background function instead."
  131. )
  132. assert isinstance(
  133. result, C.vision.MattingResult
  134. ), "The result must be MattingResult type"
  135. return C.vision.Visualize.swap_background_matting(
  136. im_data, background, result, remove_small_connected_area
  137. )
  138. def swap_background_segmentation(im_data, background, background_label, result):
  139. logging.warning(
  140. "DEPRECATED: ultra_infer.vision.swap_background_segmentation is deprecated, please use ultra_infer.vision.swap_background function instead."
  141. )
  142. assert isinstance(
  143. result, C.vision.SegmentationResult
  144. ), "The result must be SegmentaitonResult type"
  145. return C.vision.Visualize.swap_background_segmentation(
  146. im_data, background, background_label, result
  147. )
  148. def swap_background(
  149. im_data, background, result, remove_small_connected_area=False, background_label=0
  150. ):
  151. """Swap the image background with MattingResult or SegmentationResult
  152. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  153. :param background: (numpy.ndarray)The background image data, 3-D array with layout HWC, BGR format
  154. :param result: The result produced by model, MattingResult or SegmentationResult
  155. :param remove_small_connected_area: (bool) If remove_small_connected_area==True, the visualized result will not include the small connected areas
  156. :param background_label: (int)The background label number in SegmentationResult
  157. :return: (numpy.ndarray) image with visualized results
  158. """
  159. if isinstance(result, C.vision.MattingResult):
  160. return C.vision.swap_background(
  161. im_data, background, result, remove_small_connected_area
  162. )
  163. elif isinstance(result, C.vision.SegmentationResult):
  164. return C.vision.swap_background(im_data, background, result, background_label)
  165. else:
  166. raise Exception(
  167. "Only support result type of MattingResult or SegmentationResult, but now the data type is {}.".format(
  168. type(result)
  169. )
  170. )
  171. def vis_ppocr(im_data, det_result):
  172. """Show the visualized results for ocr models
  173. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  174. :param det_result: the result produced by model
  175. :return: (numpy.ndarray) image with visualized results
  176. """
  177. return C.vision.vis_ppocr(im_data, det_result)
  178. def vis_ppocr_curve(im_data, det_result):
  179. """Show the visualized results for ocr models
  180. :param im_data: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  181. :param det_result: the result produced by model
  182. :return: (numpy.ndarray) image with visualized results
  183. """
  184. return C.vision.vis_ppocr_curve(im_data, det_result)
  185. def vis_mot(im_data, mot_result, score_threshold=0.0, records=None):
  186. return C.vision.vis_mot(im_data, mot_result, score_threshold, records)
  187. def vis_headpose(im_data, headpose_result, size=50, line_size=1):
  188. return C.vision.vis_headpose(im_data, headpose_result, size, line_size)