result.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. from typing import Dict
  15. import math
  16. import random
  17. from pathlib import Path
  18. import numpy as np
  19. import cv2
  20. import PIL
  21. from PIL import Image, ImageDraw, ImageFont
  22. from ....utils.fonts import PINGFANG_FONT_FILE_PATH, create_font
  23. from ...common.result import BaseCVResult
  24. class DocPreprocessorResult(BaseCVResult):
  25. """doc preprocessor result"""
  26. def save_to_img(self, save_path: str, *args, **kwargs) -> None:
  27. """
  28. Save the image to the specified path.
  29. Args:
  30. save_path (str): The path to save the image.
  31. If the path does not end with '.jpg' or '.png', it appends '_res_doc_preprocess_<img_id>.jpg'
  32. to the path where <img_id> is retrieved from the object's 'img_id' attribute.
  33. *args: Variable length argument list.
  34. **kwargs: Arbitrary keyword arguments.
  35. Returns:
  36. None
  37. """
  38. if not str(save_path).lower().endswith((".jpg", ".png")):
  39. img_id = self["img_id"]
  40. save_path = Path(save_path) / f"res_doc_preprocess_{img_id}.jpg"
  41. super().save_to_img(save_path, *args, **kwargs)
  42. def _to_img(self) -> Dict[str, Image.Image]:
  43. """
  44. Generate an image combining the original, rotated, and unwarping images.
  45. Returns:
  46. Dict[Image.Image]: A new image that displays the rotated, and unwarping images.
  47. """
  48. imgs = {"preprocessed_img": Image.fromarray(self["output_img"][:, :, ::-1])}
  49. if self["rot_img"] is not None:
  50. imgs["rotated_img"] = Image.fromarray(self["rot_img"][:, :, ::-1])
  51. return imgs