result.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  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(PINGFANG_FONT.path, font_size, encoding="utf-8")
  42. draw_thickness = int(max(image.size) * 0.002)
  43. draw = ImageDraw.Draw(image)
  44. results = self["result"][i]
  45. for result in results:
  46. bbox, score, class_name = result
  47. if class_name not in label2color:
  48. random_index = random.randint(0, len(color_list) - 1)
  49. label2color[class_name] = color_list[random_index]
  50. catid2fontcolor[class_name] = self._get_font_colormap(random_index)
  51. color = tuple(label2color[class_name])
  52. font_color = tuple(catid2fontcolor[class_name])
  53. xmin, ymin, xmax, ymax = bbox
  54. rectangle = [
  55. (xmin, ymin),
  56. (xmin, ymax),
  57. (xmax, ymax),
  58. (xmax, ymin),
  59. (xmin, ymin),
  60. ]
  61. draw.line(
  62. rectangle,
  63. width=draw_thickness,
  64. fill=color,
  65. )
  66. text = "{} {:.2f}".format(class_name, score)
  67. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  68. tw, th = draw.textsize(text, font=font)
  69. else:
  70. left, top, right, bottom = draw.textbbox((0, 0), text, font)
  71. tw, th = right - left, bottom - top + 4
  72. if ymin < th:
  73. draw.rectangle(
  74. [(xmin, ymin), (xmin + tw + 4, ymin + th + 1)], fill=color
  75. )
  76. draw.text((xmin + 2, ymin - 2), text, fill=font_color, font=font)
  77. else:
  78. draw.rectangle(
  79. [(xmin, ymin - th), (xmin + tw + 4, ymin + 1)], fill=color
  80. )
  81. draw.text(
  82. (xmin + 2, ymin - th - 2), text, fill=font_color, font=font
  83. )
  84. image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
  85. video_list.append(image)
  86. return {"res": (np.array(video_list), write_fps)}
  87. def _get_font_colormap(self, color_index):
  88. """
  89. Get font colormap
  90. """
  91. dark = np.array([0x14, 0x0E, 0x35])
  92. light = np.array([0xFF, 0xFF, 0xFF])
  93. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  94. if color_index in light_indexs:
  95. return light.astype("int32")
  96. else:
  97. return dark.astype("int32")