visualizer.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 ImageDraw, ImageFont
  17. from ......utils.fonts import PINGFANG_FONT
  18. def colormap(rgb=False):
  19. """
  20. Get colormap
  21. """
  22. color_list = np.array(
  23. [
  24. 0xFF,
  25. 0x00,
  26. 0x00,
  27. 0xCC,
  28. 0xFF,
  29. 0x00,
  30. 0x00,
  31. 0xFF,
  32. 0x66,
  33. 0x00,
  34. 0x66,
  35. 0xFF,
  36. 0xCC,
  37. 0x00,
  38. 0xFF,
  39. 0xFF,
  40. 0x4D,
  41. 0x00,
  42. 0x80,
  43. 0xFF,
  44. 0x00,
  45. 0x00,
  46. 0xFF,
  47. 0xB2,
  48. 0x00,
  49. 0x1A,
  50. 0xFF,
  51. 0xFF,
  52. 0x00,
  53. 0xE5,
  54. 0xFF,
  55. 0x99,
  56. 0x00,
  57. 0x33,
  58. 0xFF,
  59. 0x00,
  60. 0x00,
  61. 0xFF,
  62. 0xFF,
  63. 0x33,
  64. 0x00,
  65. 0xFF,
  66. 0xFF,
  67. 0x00,
  68. 0x99,
  69. 0xFF,
  70. 0xE5,
  71. 0x00,
  72. 0x00,
  73. 0xFF,
  74. 0x1A,
  75. 0x00,
  76. 0xB2,
  77. 0xFF,
  78. 0x80,
  79. 0x00,
  80. 0xFF,
  81. 0xFF,
  82. 0x00,
  83. 0x4D,
  84. ]
  85. ).astype(np.float32)
  86. color_list = color_list.reshape((-1, 3))
  87. if not rgb:
  88. color_list = color_list[:, ::-1]
  89. return color_list.astype("int32")
  90. def font_colormap(color_index):
  91. """
  92. Get font colormap
  93. """
  94. dark = np.array([0x14, 0x0E, 0x35])
  95. light = np.array([0xFF, 0xFF, 0xFF])
  96. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  97. if color_index in light_indexs:
  98. return light.astype("int32")
  99. else:
  100. return dark.astype("int32")
  101. def draw_label(image, label, label_map_dict):
  102. """Draw label on image"""
  103. image = image.convert("RGB")
  104. image_size = image.size
  105. draw = ImageDraw.Draw(image)
  106. min_font_size = int(image_size[0] * 0.02)
  107. max_font_size = int(image_size[0] * 0.05)
  108. for font_size in range(max_font_size, min_font_size - 1, -1):
  109. font = ImageFont.truetype(PINGFANG_FONT.path, font_size, encoding="utf-8")
  110. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  111. text_width_tmp, text_height_tmp = draw.textsize(
  112. label_map_dict[int(label)], font
  113. )
  114. else:
  115. left, top, right, bottom = draw.textbbox(
  116. (0, 0), label_map_dict[int(label)], font
  117. )
  118. text_width_tmp, text_height_tmp = right - left, bottom - top
  119. if text_width_tmp <= image_size[0]:
  120. break
  121. else:
  122. font = ImageFont.truetype(PINGFANG_FONT.path, min_font_size)
  123. color_list = colormap(rgb=True)
  124. color = tuple(color_list[0])
  125. font_color = tuple(font_colormap(3))
  126. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  127. text_width, text_height = draw.textsize(label_map_dict[int(label)], font)
  128. else:
  129. left, top, right, bottom = draw.textbbox(
  130. (0, 0), label_map_dict[int(label)], font
  131. )
  132. text_width, text_height = right - left, bottom - top
  133. rect_left = 3
  134. rect_top = 3
  135. rect_right = rect_left + text_width + 3
  136. rect_bottom = rect_top + text_height + 6
  137. draw.rectangle([(rect_left, rect_top), (rect_right, rect_bottom)], fill=color)
  138. text_x = rect_left + 3
  139. text_y = rect_top
  140. draw.text((text_x, text_y), label_map_dict[int(label)], fill=font_color, font=font)
  141. return image