visualizer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. from PIL import Image, ImageDraw, ImageFont
  16. from ......utils.fonts import PINGFANG_FONT
  17. def colormap(rgb=False):
  18. """
  19. Get colormap
  20. """
  21. color_list = np.array(
  22. [
  23. 0xFF,
  24. 0x00,
  25. 0x00,
  26. 0xCC,
  27. 0xFF,
  28. 0x00,
  29. 0x00,
  30. 0xFF,
  31. 0x66,
  32. 0x00,
  33. 0x66,
  34. 0xFF,
  35. 0xCC,
  36. 0x00,
  37. 0xFF,
  38. 0xFF,
  39. 0x4D,
  40. 0x00,
  41. 0x80,
  42. 0xFF,
  43. 0x00,
  44. 0x00,
  45. 0xFF,
  46. 0xB2,
  47. 0x00,
  48. 0x1A,
  49. 0xFF,
  50. 0xFF,
  51. 0x00,
  52. 0xE5,
  53. 0xFF,
  54. 0x99,
  55. 0x00,
  56. 0x33,
  57. 0xFF,
  58. 0x00,
  59. 0x00,
  60. 0xFF,
  61. 0xFF,
  62. 0x33,
  63. 0x00,
  64. 0xFF,
  65. 0xFF,
  66. 0x00,
  67. 0x99,
  68. 0xFF,
  69. 0xE5,
  70. 0x00,
  71. 0x00,
  72. 0xFF,
  73. 0x1A,
  74. 0x00,
  75. 0xB2,
  76. 0xFF,
  77. 0x80,
  78. 0x00,
  79. 0xFF,
  80. 0xFF,
  81. 0x00,
  82. 0x4D,
  83. ]
  84. ).astype(np.float32)
  85. color_list = color_list.reshape((-1, 3))
  86. if not rgb:
  87. color_list = color_list[:, ::-1]
  88. return color_list.astype("int32")
  89. def font_colormap(color_index):
  90. """
  91. Get font colormap
  92. """
  93. dark = np.array([0x14, 0x0E, 0x35])
  94. light = np.array([0xFF, 0xFF, 0xFF])
  95. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  96. if color_index in light_indexs:
  97. return light.astype("int32")
  98. else:
  99. return dark.astype("int32")
  100. def draw_multi_label(image, label, label_map_dict):
  101. labels = label.split(",")
  102. label_names = [
  103. label_map_dict[i] for i, label in enumerate(labels) if int(label) == 1
  104. ]
  105. image = image.convert("RGB")
  106. image_width, image_height = image.size
  107. font_size = int(image_width * 0.06)
  108. font = ImageFont.truetype(PINGFANG_FONT.path, font_size)
  109. text_lines = []
  110. row_width = 0
  111. row_height = 0
  112. row_text = "\t"
  113. for label_name in label_names:
  114. text = f"{label_name}\t"
  115. x1, y1, x2, y2 = font.getbbox(text)
  116. text_width, row_height = x2 - x1, y2 - y1
  117. if row_width + text_width <= image_width:
  118. row_text += text
  119. row_width += text_width
  120. else:
  121. text_lines.append(row_text)
  122. row_text = "\t" + text
  123. row_width = text_width
  124. text_lines.append(row_text)
  125. color_list = colormap(rgb=True)
  126. color = tuple(color_list[0])
  127. new_image_height = image_height + len(text_lines) * int(row_height * 1.8)
  128. new_image = Image.new("RGB", (image_width, new_image_height), color)
  129. new_image.paste(image, (0, 0))
  130. draw = ImageDraw.Draw(new_image)
  131. font_color = tuple(font_colormap(3))
  132. for i, text in enumerate(text_lines):
  133. draw.text(
  134. (0, image_height + i * int(row_height * 1.2)),
  135. text,
  136. fill=font_color,
  137. font=font,
  138. )
  139. return new_image