visualizer.py 3.8 KB

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