result.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 copy
  17. import numpy as np
  18. import PIL
  19. from PIL import Image, ImageDraw, ImageFont
  20. from ....utils.fonts import PINGFANG_FONT_FILE_PATH
  21. from ...utils.io import ImageReader
  22. from ...common.result import BaseCVResult, StrMixin, JsonMixin
  23. from ...utils.color_map import get_colormap, font_colormap
  24. def draw_attribute_result(img, boxes):
  25. """
  26. Args:
  27. img (PIL.Image.Image): PIL image
  28. boxes (list): a list of dictionaries representing detection box information.
  29. Returns:
  30. img (PIL.Image.Image): visualized image
  31. """
  32. font_size = int((0.024 * int(img.width) + 2) * 0.7)
  33. font = ImageFont.truetype(PINGFANG_FONT_FILE_PATH, font_size, encoding="utf-8")
  34. draw_thickness = int(max(img.size) * 0.005)
  35. draw = ImageDraw.Draw(img)
  36. label2color = {}
  37. catid2fontcolor = {}
  38. color_list = get_colormap(rgb=True)
  39. for i, dt in enumerate(boxes):
  40. text_lines, bbox, score = dt["label"], dt["coordinate"], dt["score"]
  41. if i not in label2color:
  42. color_index = i % len(color_list)
  43. label2color[i] = color_list[color_index]
  44. catid2fontcolor[i] = font_colormap(color_index)
  45. color = tuple(label2color[i]) + (255,)
  46. font_color = tuple(catid2fontcolor[i])
  47. xmin, ymin, xmax, ymax = bbox
  48. # draw box
  49. draw.line(
  50. [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)],
  51. width=draw_thickness,
  52. fill=color,
  53. )
  54. # draw label
  55. current_y = ymin
  56. for line in text_lines:
  57. if tuple(map(int, PIL.__version__.split("."))) <= (10, 0, 0):
  58. tw, th = draw.textsize(line, font=font)
  59. else:
  60. left, top, right, bottom = draw.textbbox((0, 0), line, font)
  61. tw, th = right - left, bottom - top + 4
  62. draw.text((5 + xmin + 1, current_y + 1), line, fill=(0, 0, 0), font=font)
  63. draw.text((5 + xmin, current_y), line, fill=color, font=font)
  64. current_y += th
  65. return img
  66. class AttributeRecResult(BaseCVResult):
  67. def _to_str(self, *args, **kwargs):
  68. data = copy.deepcopy(self)
  69. data.pop("input_img")
  70. return StrMixin._to_str(data, *args, **kwargs)
  71. def _to_json(self, *args, **kwargs):
  72. data = copy.deepcopy(self)
  73. data.pop("input_img")
  74. return JsonMixin._to_json(data, *args, **kwargs)
  75. def _to_img(self):
  76. """apply"""
  77. img_reader = ImageReader(backend="pillow")
  78. image = img_reader.read(self["input_path"])
  79. boxes = [
  80. {
  81. "coordinate": box["coordinate"],
  82. "label": box["labels"],
  83. "score": box["det_score"],
  84. }
  85. for box in self["boxes"]
  86. ]
  87. image = draw_attribute_result(image, boxes)
  88. return {"res": image}