visualizer.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. import numpy as np
  15. import PIL
  16. from PIL import Image, ImageDraw, ImageFont
  17. from pycocotools.coco import COCO
  18. from ......utils import logging
  19. from ......utils.fonts import PINGFANG_FONT_FILE_PATH
  20. def colormap(rgb=False):
  21. """
  22. Get colormap
  23. The code of this function is copied from https://github.com/facebookresearch/Detectron/blob/main/detectron/\
  24. utils/colormap.py
  25. """
  26. color_list = np.array(
  27. [
  28. 0xFF,
  29. 0x00,
  30. 0x00,
  31. 0xCC,
  32. 0xFF,
  33. 0x00,
  34. 0x00,
  35. 0xFF,
  36. 0x66,
  37. 0x00,
  38. 0x66,
  39. 0xFF,
  40. 0xCC,
  41. 0x00,
  42. 0xFF,
  43. 0xFF,
  44. 0x4D,
  45. 0x00,
  46. 0x80,
  47. 0xFF,
  48. 0x00,
  49. 0x00,
  50. 0xFF,
  51. 0xB2,
  52. 0x00,
  53. 0x1A,
  54. 0xFF,
  55. 0xFF,
  56. 0x00,
  57. 0xE5,
  58. 0xFF,
  59. 0x99,
  60. 0x00,
  61. 0x33,
  62. 0xFF,
  63. 0x00,
  64. 0x00,
  65. 0xFF,
  66. 0xFF,
  67. 0x33,
  68. 0x00,
  69. 0xFF,
  70. 0xFF,
  71. 0x00,
  72. 0x99,
  73. 0xFF,
  74. 0xE5,
  75. 0x00,
  76. 0x00,
  77. 0xFF,
  78. 0x1A,
  79. 0x00,
  80. 0xB2,
  81. 0xFF,
  82. 0x80,
  83. 0x00,
  84. 0xFF,
  85. 0xFF,
  86. 0x00,
  87. 0x4D,
  88. ]
  89. ).astype(np.float32)
  90. color_list = color_list.reshape((-1, 3))
  91. if not rgb:
  92. color_list = color_list[:, ::-1]
  93. return color_list.astype("int32")
  94. def font_colormap(color_index):
  95. """
  96. Get font color according to the index of colormap
  97. """
  98. dark = np.array([0x14, 0x0E, 0x35])
  99. light = np.array([0xFF, 0xFF, 0xFF])
  100. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  101. if color_index in light_indexs:
  102. return light.astype("int32")
  103. else:
  104. return dark.astype("int32")
  105. def draw_bbox(image, coco_info: COCO, img_id):
  106. """
  107. Draw bbox on image
  108. """
  109. try:
  110. image_info = coco_info.loadImgs(img_id)[0]
  111. font_size = int(0.024 * int(image_info["width"])) + 2
  112. except:
  113. font_size = 12
  114. font = ImageFont.truetype(PINGFANG_FONT_FILE_PATH, font_size, encoding="utf-8")
  115. image = image.convert("RGB")
  116. draw = ImageDraw.Draw(image)
  117. image_size = image.size
  118. width = int(max(image_size) * 0.005)
  119. catid2color = {}
  120. catid2fontcolor = {}
  121. catid_num_dict = {}
  122. color_list = colormap(rgb=True)
  123. annotations = coco_info.loadAnns(coco_info.getAnnIds(imgIds=img_id))
  124. for ann in annotations:
  125. catid = ann["category_id"]
  126. catid_num_dict[catid] = catid_num_dict.get(catid, 0) + 1
  127. for i, (catid, _) in enumerate(
  128. sorted(catid_num_dict.items(), key=lambda x: x[1], reverse=True)
  129. ):
  130. if catid not in catid2color:
  131. color_index = i % len(color_list)
  132. catid2color[catid] = color_list[color_index]
  133. catid2fontcolor[catid] = font_colormap(color_index)
  134. for ann in annotations:
  135. catid, bbox = ann["category_id"], ann["bbox"]
  136. color = tuple(catid2color[catid])
  137. font_color = tuple(catid2fontcolor[catid])
  138. if len(bbox) == 4:
  139. # draw bbox
  140. xmin, ymin, w, h = bbox
  141. xmax = xmin + w
  142. ymax = ymin + h
  143. draw.line(
  144. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)],
  145. width=width,
  146. fill=color,
  147. )
  148. elif len(bbox) == 8:
  149. x1, y1, x2, y2, x3, y3, x4, y4 = bbox
  150. draw.line(
  151. [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x1, y1)],
  152. width=width,
  153. fill=color,
  154. )
  155. xmin = min(x1, x2, x3, x4)
  156. ymin = min(y1, y2, y3, y4)
  157. else:
  158. logging.info("Error: The shape of bbox must be [M, 4] or [M, 8]!")
  159. # draw label
  160. label = coco_info.loadCats(catid)[0]["name"]
  161. text = "{}".format(label)
  162. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  163. tw, th = draw.textsize(text, font=font)
  164. else:
  165. left, top, right, bottom = draw.textbbox((0, 0), text, font)
  166. tw, th = right - left, bottom - top
  167. if ymin < th:
  168. draw.rectangle([(xmin, ymin), (xmin + tw + 4, ymin + th + 1)], fill=color)
  169. draw.text((xmin + 2, ymin - 2), text, fill=font_color, font=font)
  170. else:
  171. draw.rectangle([(xmin, ymin - th), (xmin + tw + 4, ymin + 1)], fill=color)
  172. draw.text((xmin + 2, ymin - th - 2), text, fill=font_color, font=font)
  173. return image
  174. def draw_mask(image, coco_info: COCO, img_id):
  175. """
  176. Draw mask on image
  177. """
  178. mask_color_id = 0
  179. w_ratio = 0.4
  180. alpha = 0.6
  181. color_list = colormap(rgb=True)
  182. img_array = np.array(image).astype("float32")
  183. h, w = img_array.shape[:2]
  184. annotations = coco_info.loadAnns(coco_info.getAnnIds(imgIds=img_id))
  185. for ann in annotations:
  186. segm = ann["segmentation"]
  187. if not segm:
  188. continue
  189. import pycocotools.mask as mask_util
  190. rles = mask_util.frPyObjects(segm, h, w)
  191. rle = mask_util.merge(rles)
  192. mask = mask_util.decode(rle) * 255
  193. color_mask = color_list[mask_color_id % len(color_list), 0:3]
  194. mask_color_id += 1
  195. for c in range(3):
  196. color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255
  197. idx = np.nonzero(mask)
  198. img_array[idx[0], idx[1], :] *= 1.0 - alpha
  199. img_array[idx[0], idx[1], :] += alpha * color_mask
  200. return Image.fromarray(img_array.astype("uint8"))