visualizer.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. from PIL import Image, ImageDraw, ImageFont
  19. from ......utils.fonts import PINGFANG_FONT_FILE_PATH
  20. def colormap(rgb=False):
  21. """
  22. Get colormap
  23. """
  24. color_list = np.array([
  25. 0xFF, 0x00, 0x00, 0xCC, 0xFF, 0x00, 0x00, 0xFF, 0x66, 0x00, 0x66, 0xFF,
  26. 0xCC, 0x00, 0xFF, 0xFF, 0x4D, 0x00, 0x80, 0xff, 0x00, 0x00, 0xFF, 0xB2,
  27. 0x00, 0x1A, 0xFF, 0xFF, 0x00, 0xE5, 0xFF, 0x99, 0x00, 0x33, 0xFF, 0x00,
  28. 0x00, 0xFF, 0xFF, 0x33, 0x00, 0xFF, 0xff, 0x00, 0x99, 0xFF, 0xE5, 0x00,
  29. 0x00, 0xFF, 0x1A, 0x00, 0xB2, 0xFF, 0x80, 0x00, 0xFF, 0xFF, 0x00, 0x4D
  30. ]).astype(np.float32)
  31. color_list = (color_list.reshape((-1, 3)))
  32. if not rgb:
  33. color_list = color_list[:, ::-1]
  34. return color_list.astype('int32')
  35. def font_colormap(color_index):
  36. """
  37. Get font colormap
  38. """
  39. dark = np.array([0x14, 0x0E, 0x35])
  40. light = np.array([0xFF, 0xFF, 0xFF])
  41. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  42. if color_index in light_indexs:
  43. return light.astype('int32')
  44. else:
  45. return dark.astype('int32')
  46. def draw_label(image, label, label_map_dict):
  47. """ Draw label on image """
  48. image = image.convert('RGB')
  49. image_size = image.size
  50. draw = ImageDraw.Draw(image)
  51. min_font_size = int(image_size[0] * 0.02)
  52. max_font_size = int(image_size[0] * 0.05)
  53. for font_size in range(max_font_size, min_font_size - 1, -1):
  54. font = ImageFont.truetype(
  55. PINGFANG_FONT_FILE_PATH, font_size, encoding="utf-8")
  56. text_width_tmp, text_height_tmp = draw.textsize(
  57. label_map_dict[int(label)], font)
  58. if text_width_tmp <= image_size[0]:
  59. break
  60. else:
  61. font = ImageFont.truetype(PINGFANG_FONT_FILE_PATH, min_font_size)
  62. color_list = colormap(rgb=True)
  63. color = tuple(color_list[0])
  64. font_color = tuple(font_colormap(3))
  65. text_width, text_height = draw.textsize(label_map_dict[int(label)], font)
  66. rect_left = 3
  67. rect_top = 3
  68. rect_right = rect_left + text_width + 3
  69. rect_bottom = rect_top + text_height + 6
  70. draw.rectangle(
  71. [(rect_left, rect_top), (rect_right, rect_bottom)], fill=color)
  72. text_x = rect_left + 3
  73. text_y = rect_top
  74. draw.text(
  75. (text_x, text_y),
  76. label_map_dict[int(label)],
  77. fill=font_color,
  78. font=font)
  79. return image