result.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.fonts import SIMFANG_FONT
  18. from ...common.result import BaseCVResult, JsonMixin
  19. class TextRecResult(BaseCVResult):
  20. def _to_str(self, *args, **kwargs):
  21. data = copy.deepcopy(self)
  22. data.pop("input_img")
  23. data.pop("vis_font")
  24. return JsonMixin._to_str(data, *args, **kwargs)
  25. def _to_json(self, *args, **kwargs):
  26. data = copy.deepcopy(self)
  27. data.pop("input_img")
  28. data.pop("vis_font")
  29. return JsonMixin._to_json(data, *args, **kwargs)
  30. def _to_img(self):
  31. """Draw label on image"""
  32. image = Image.fromarray(self["input_img"][:, :, ::-1])
  33. rec_text = self["rec_text"]
  34. rec_score = self["rec_score"]
  35. vis_font = self["vis_font"] if self["vis_font"] is not None else SIMFANG_FONT
  36. image = image.convert("RGB")
  37. image_width, image_height = image.size
  38. text = f"{rec_text} ({rec_score})"
  39. font = self.adjust_font_size(image_width, text, vis_font.path)
  40. row_height = font.getbbox(text)[3]
  41. new_image_height = image_height + int(row_height * 1.2)
  42. new_image = Image.new("RGB", (image_width, new_image_height), (255, 255, 255))
  43. new_image.paste(image, (0, 0))
  44. draw = ImageDraw.Draw(new_image)
  45. draw.text(
  46. (0, image_height),
  47. text,
  48. fill=(0, 0, 0),
  49. font=font,
  50. )
  51. return {"res": new_image}
  52. def adjust_font_size(self, image_width, text, font_path):
  53. font_size = int(image_width * 0.06)
  54. font = ImageFont.truetype(font_path, font_size)
  55. if int(PIL.__version__.split(".")[0]) < 10:
  56. text_width, _ = font.getsize(text)
  57. else:
  58. text_width, _ = font.getbbox(text)[2:]
  59. while text_width > image_width:
  60. font_size -= 1
  61. font = ImageFont.truetype(font_path, font_size)
  62. if int(PIL.__version__.split(".")[0]) < 10:
  63. text_width, _ = font.getsize(text)
  64. else:
  65. text_width, _ = font.getbbox(text)[2:]
  66. return font