attribute_rec.py 3.0 KB

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