det.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 cv2
  16. import PIL
  17. from PIL import Image, ImageDraw, ImageFont
  18. from ...utils.fonts import PINGFANG_FONT_FILE_PATH
  19. from ..utils.color_map import get_colormap, font_colormap
  20. from .base import CVResult
  21. def draw_box(img, boxes):
  22. """
  23. Args:
  24. img (PIL.Image.Image): PIL image
  25. boxes (list): a list of dictionaries representing detection box information.
  26. Returns:
  27. img (PIL.Image.Image): visualized image
  28. """
  29. font_size = int(0.024 * int(img.width)) + 2
  30. font = ImageFont.truetype(PINGFANG_FONT_FILE_PATH, font_size, encoding="utf-8")
  31. draw_thickness = int(max(img.size) * 0.005)
  32. draw = ImageDraw.Draw(img)
  33. clsid2color = {}
  34. catid2fontcolor = {}
  35. color_list = get_colormap(rgb=True)
  36. for i, dt in enumerate(boxes):
  37. clsid, bbox, score = dt["cls_id"], dt["coordinate"], dt["score"]
  38. if clsid not in clsid2color:
  39. color_index = i % len(color_list)
  40. clsid2color[clsid] = color_list[color_index]
  41. catid2fontcolor[clsid] = font_colormap(color_index)
  42. color = tuple(clsid2color[clsid])
  43. font_color = tuple(catid2fontcolor[clsid])
  44. xmin, ymin, xmax, ymax = bbox
  45. # draw bbox
  46. draw.line(
  47. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)],
  48. width=draw_thickness,
  49. fill=color,
  50. )
  51. # draw label
  52. text = "{} {:.2f}".format(dt["label"], score)
  53. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  54. tw, th = draw.textsize(text, font=font)
  55. else:
  56. left, top, right, bottom = draw.textbbox((0, 0), text, font)
  57. tw, th = right - left, bottom - top
  58. if ymin < th:
  59. draw.rectangle([(xmin, ymin), (xmin + tw + 4, ymin + th + 1)], fill=color)
  60. draw.text((xmin + 2, ymin - 2), text, fill=font_color, font=font)
  61. else:
  62. draw.rectangle([(xmin, ymin - th), (xmin + tw + 4, ymin + 1)], fill=color)
  63. draw.text((xmin + 2, ymin - th - 2), text, fill=font_color, font=font)
  64. return img
  65. class DetResult(CVResult):
  66. """Save Result Transform"""
  67. def _to_img(self):
  68. """apply"""
  69. boxes = self["boxes"]
  70. image = self._img_reader.read(self["input_path"])
  71. image = draw_box(image, boxes)
  72. return image