result.py 3.1 KB

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