result.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 cv2
  15. import numpy as np
  16. import random
  17. import PIL
  18. from PIL import Image, ImageDraw, ImageFont
  19. from ....utils.fonts import PINGFANG_FONT_FILE_PATH
  20. from ...utils.color_map import get_colormap
  21. from ...utils.io import VideoReader
  22. from ...common.result import BaseVideoResult
  23. class DetVideoResult(BaseVideoResult):
  24. def _to_video(self):
  25. """Draw label on image"""
  26. video_reader = VideoReader(backend="decord")
  27. video = video_reader.read(self["input_path"])
  28. video = list(video)
  29. write_fps = video_reader.get_fps()
  30. label2color = {}
  31. catid2fontcolor = {}
  32. color_list = get_colormap(rgb=True)
  33. video_list = []
  34. for i in range(len(video)):
  35. image = Image.fromarray(video[i].asnumpy())
  36. image_size = image.size
  37. font_size = int(0.018 * int(image.width)) + 2
  38. font = ImageFont.truetype(
  39. PINGFANG_FONT_FILE_PATH, font_size, encoding="utf-8"
  40. )
  41. draw_thickness = int(max(image.size) * 0.002)
  42. draw = ImageDraw.Draw(image)
  43. results = self["result"][i]
  44. for result in results:
  45. bbox, score, class_name = result
  46. if class_name not in label2color:
  47. random_index = random.randint(0, len(color_list) - 1)
  48. label2color[class_name] = color_list[random_index]
  49. catid2fontcolor[class_name] = self._get_font_colormap(random_index)
  50. color = tuple(label2color[class_name])
  51. font_color = tuple(catid2fontcolor[class_name])
  52. xmin, ymin, xmax, ymax = bbox
  53. rectangle = [
  54. (xmin, ymin),
  55. (xmin, ymax),
  56. (xmax, ymax),
  57. (xmax, ymin),
  58. (xmin, ymin),
  59. ]
  60. draw.line(
  61. rectangle,
  62. width=draw_thickness,
  63. fill=color,
  64. )
  65. text = "{} {:.2f}".format(class_name, score)
  66. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  67. tw, th = draw.textsize(text, font=font)
  68. else:
  69. left, top, right, bottom = draw.textbbox((0, 0), text, font)
  70. tw, th = right - left, bottom - top + 4
  71. if ymin < th:
  72. draw.rectangle(
  73. [(xmin, ymin), (xmin + tw + 4, ymin + th + 1)], fill=color
  74. )
  75. draw.text((xmin + 2, ymin - 2), text, fill=font_color, font=font)
  76. else:
  77. draw.rectangle(
  78. [(xmin, ymin - th), (xmin + tw + 4, ymin + 1)], fill=color
  79. )
  80. draw.text(
  81. (xmin + 2, ymin - th - 2), text, fill=font_color, font=font
  82. )
  83. image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
  84. video_list.append(image)
  85. return {"res": (np.array(video_list), write_fps)}
  86. def _get_font_colormap(self, color_index):
  87. """
  88. Get font colormap
  89. """
  90. dark = np.array([0x14, 0x0E, 0x35])
  91. light = np.array([0xFF, 0xFF, 0xFF])
  92. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  93. if color_index in light_indexs:
  94. return light.astype("int32")
  95. else:
  96. return dark.astype("int32")