result.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 math
  15. import random
  16. import numpy as np
  17. import cv2
  18. import PIL
  19. from PIL import Image, ImageDraw, ImageFont
  20. from ....utils.fonts import PINGFANG_FONT_FILE_PATH, create_font
  21. from ..components import CVResult
  22. class DocPreprocessorResult(CVResult):
  23. def save_to_img(self, save_path, *args, **kwargs):
  24. if not str(save_path).lower().endswith((".jpg", ".png")):
  25. img_id = self["img_id"]
  26. save_path = save_path + "/res_doc_preprocess_%d.jpg" % img_id
  27. super().save_to_img(save_path, *args, **kwargs)
  28. def _to_img(self):
  29. """draw doc preprocess result"""
  30. image = self["input_image"]
  31. angle = self["angle"]
  32. rot_img = self["rot_img"]
  33. output_img = self["output_img"]
  34. h, w = image.shape[0:2]
  35. img_show = Image.new("RGB", (w * 3, h + 25), (255, 255, 255))
  36. img_show.paste(Image.fromarray(image), (0, 0, w, h))
  37. img_show.paste(Image.fromarray(rot_img), (w, 0, w * 2, h))
  38. img_show.paste(Image.fromarray(output_img), (w * 2, 0, w * 3, h))
  39. draw_text = ImageDraw.Draw(img_show)
  40. txt_list = ["Original Image", "Rotated Image", "Unwarping Image"]
  41. for tno in range(len(txt_list)):
  42. txt = txt_list[tno]
  43. font = create_font(txt, (w, 20), PINGFANG_FONT_FILE_PATH)
  44. draw_text.text([10 + w * tno, h + 2], txt, fill=(0, 0, 0), font=font)
  45. return img_show