result.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 copy
  15. import PIL
  16. from PIL import Image, ImageDraw, ImageFont
  17. import numpy as np
  18. from ....utils.fonts import PINGFANG_FONT_FILE_PATH
  19. from ...utils.color_map import get_colormap
  20. from ...common.result import BaseCVResult, StrMixin, JsonMixin
  21. class MLClassResult(BaseCVResult):
  22. def _to_str(self, *args, **kwargs):
  23. data = copy.deepcopy(self)
  24. return StrMixin._to_str(data, *args, **kwargs)
  25. def _to_json(self, *args, **kwargs):
  26. data = copy.deepcopy(self)
  27. data.pop("input_img")
  28. return JsonMixin._to_json(data, *args, **kwargs)
  29. def _to_img(self):
  30. """Draw label on image"""
  31. image = Image.fromarray(self["input_img"])
  32. label_names = self["label_names"]
  33. scores = self["scores"]
  34. image = image.convert("RGB")
  35. image_width, image_height = image.size
  36. font_size = int(image_width * 0.06)
  37. font = ImageFont.truetype(PINGFANG_FONT_FILE_PATH, font_size)
  38. text_lines = []
  39. row_width = 0
  40. row_height = 0
  41. row_text = "\t"
  42. for label_name, score in zip(label_names, scores):
  43. text = f"{label_name}({score})\t"
  44. if int(PIL.__version__.split(".")[0]) < 10:
  45. text_width, row_height = font.getsize(text)
  46. else:
  47. text_width, row_height = font.getbbox(text)[2:]
  48. if row_width + text_width <= image_width:
  49. row_text += text
  50. row_width += text_width
  51. else:
  52. text_lines.append(row_text)
  53. row_text = "\t" + text
  54. row_width = text_width
  55. text_lines.append(row_text)
  56. color_list = get_colormap(rgb=True)
  57. color = tuple(color_list[0])
  58. new_image_height = image_height + len(text_lines) * int(row_height * 1.2)
  59. new_image = Image.new("RGB", (image_width, new_image_height), color)
  60. new_image.paste(image, (0, 0))
  61. draw = ImageDraw.Draw(new_image)
  62. font_color = tuple(self._get_font_colormap(3))
  63. for i, text in enumerate(text_lines):
  64. if int(PIL.__version__.split(".")[0]) < 10:
  65. text_width, _ = font.getsize(text)
  66. else:
  67. text_width, _ = font.getbbox(text)[2:]
  68. draw.text(
  69. (0, image_height + i * int(row_height * 1.2)),
  70. text,
  71. fill=font_color,
  72. font=font,
  73. )
  74. return {"res": new_image}
  75. def _get_font_colormap(self, color_index):
  76. """
  77. Get font colormap
  78. """
  79. dark = np.array([0x14, 0x0E, 0x35])
  80. light = np.array([0xFF, 0xFF, 0xFF])
  81. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  82. if color_index in light_indexs:
  83. return light.astype("int32")
  84. else:
  85. return dark.astype("int32")