result.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. from pathlib import Path
  17. import numpy as np
  18. import cv2
  19. import PIL
  20. from PIL import Image, ImageDraw, ImageFont
  21. from ....utils.fonts import PINGFANG_FONT_FILE_PATH, create_font
  22. from ...common.result import BaseCVResult
  23. class DocPreprocessorResult(BaseCVResult):
  24. """doc preprocessor result"""
  25. def save_to_img(self, save_path: str, *args, **kwargs) -> None:
  26. """
  27. Save the image to the specified path.
  28. Args:
  29. save_path (str): The path to save the image.
  30. If the path does not end with '.jpg' or '.png', it appends '_res_doc_preprocess_<img_id>.jpg'
  31. to the path where <img_id> is retrieved from the object's 'img_id' attribute.
  32. *args: Variable length argument list.
  33. **kwargs: Arbitrary keyword arguments.
  34. Returns:
  35. None
  36. """
  37. if not str(save_path).lower().endswith((".jpg", ".png")):
  38. img_id = self["img_id"]
  39. save_path = Path(save_path) / f"res_doc_preprocess_{img_id}.jpg"
  40. super().save_to_img(save_path, *args, **kwargs)
  41. def _to_img(self) -> PIL.Image:
  42. """
  43. Generate an image combining the original, rotated, and unwarping images.
  44. Returns:
  45. PIL.Image: A new image that displays the original, rotated, and unwarping images side by side.
  46. """
  47. image = self["input_image"][:, :, ::-1]
  48. angle = self["angle"]
  49. rot_img = self["rot_img"][:, :, ::-1]
  50. output_img = self["output_img"][:, :, ::-1]
  51. h1, w1 = image.shape[0:2]
  52. h2, w2 = rot_img.shape[0:2]
  53. h3, w3 = output_img.shape[0:2]
  54. h = max(max(h1, h2), h3)
  55. img_show = Image.new("RGB", (w1 + w2 + w3, h + 25), (255, 255, 255))
  56. img_show.paste(Image.fromarray(image), (0, 0, w1, h1))
  57. img_show.paste(Image.fromarray(rot_img), (w1, 0, w1 + w2, h2))
  58. img_show.paste(Image.fromarray(output_img), (w1 + w2, 0, w1 + w2 + w3, h3))
  59. draw_text = ImageDraw.Draw(img_show)
  60. txt_list = ["Original Image", "Rotated Image", "Unwarping Image"]
  61. region_w_list = [w1, w2, w3]
  62. beg_w_list = [0, w1, w1 + w2]
  63. for tno in range(len(txt_list)):
  64. txt = txt_list[tno]
  65. font = create_font(txt, (region_w_list[tno], 20), PINGFANG_FONT_FILE_PATH)
  66. draw_text.text(
  67. [10 + beg_w_list[tno], h + 2], txt, fill=(0, 0, 0), font=font
  68. )
  69. return img_show