result.py 3.5 KB

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