result.py 2.5 KB

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