result.py 4.1 KB

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