result.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. from typing import Dict
  15. from PIL import Image, ImageDraw
  16. from ....utils.fonts import PINGFANG_FONT, create_font
  17. from ...common.result import BaseCVResult, JsonMixin
  18. class DocPreprocessorResult(BaseCVResult):
  19. """doc preprocessor result"""
  20. def _to_img(self) -> Dict[str, Image.Image]:
  21. """
  22. Generate an image combining the original, rotated, and unwarping images.
  23. Returns:
  24. Dict[Image.Image]: A new image combining the original, rotated, and unwarping images
  25. """
  26. image = self["input_img"][:, :, ::-1]
  27. rot_img = self["rot_img"][:, :, ::-1]
  28. angle = self["angle"]
  29. output_img = self["output_img"][:, :, ::-1]
  30. use_doc_orientation_classify = self["model_settings"][
  31. "use_doc_orientation_classify"
  32. ]
  33. use_doc_unwarping = self["model_settings"]["use_doc_unwarping"]
  34. h1, w1 = image.shape[0:2]
  35. h2, w2 = rot_img.shape[0:2]
  36. h3, w3 = output_img.shape[0:2]
  37. h = max(max(h1, h2), h3)
  38. img_show = Image.new("RGB", (w1 + w2 + w3, h + 25), (255, 255, 255))
  39. img_show.paste(Image.fromarray(image), (0, 0, w1, h1))
  40. img_show.paste(Image.fromarray(rot_img), (w1, 0, w1 + w2, h2))
  41. img_show.paste(Image.fromarray(output_img), (w1 + w2, 0, w1 + w2 + w3, h3))
  42. draw_text = ImageDraw.Draw(img_show)
  43. txt_list = ["Original Image", "Rotated Image", "Unwarping Image"]
  44. txt_list[1] = f"Rotated Image ({use_doc_orientation_classify}, {angle})"
  45. txt_list[2] = f"Unwarping Image ({use_doc_unwarping})"
  46. region_w_list = [w1, w2, w3]
  47. beg_w_list = [0, w1, w1 + w2]
  48. for tno in range(len(txt_list)):
  49. txt = txt_list[tno]
  50. font = create_font(txt, (region_w_list[tno], 20), PINGFANG_FONT.path)
  51. draw_text.text(
  52. [10 + beg_w_list[tno], h + 2], txt, fill=(0, 0, 0), font=font
  53. )
  54. imgs = {"preprocessed_img": img_show}
  55. return imgs
  56. def _to_str(self, *args, **kwargs) -> Dict[str, str]:
  57. """Converts the instance's attributes to a dictionary and then to a string.
  58. Args:
  59. *args: Additional positional arguments passed to the base class method.
  60. **kwargs: Additional keyword arguments passed to the base class method.
  61. Returns:
  62. Dict[str, str]: A dictionary with the instance's attributes converted to strings.
  63. """
  64. data = {}
  65. data["input_path"] = self["input_path"]
  66. data["page_index"] = self["page_index"]
  67. data["model_settings"] = self["model_settings"]
  68. data["angle"] = self["angle"]
  69. return JsonMixin._to_str(data, *args, **kwargs)
  70. def _to_json(self, *args, **kwargs) -> Dict[str, str]:
  71. """
  72. Converts the object's data to a JSON dictionary.
  73. Args:
  74. *args: Positional arguments passed to the JsonMixin._to_json method.
  75. **kwargs: Keyword arguments passed to the JsonMixin._to_json method.
  76. Returns:
  77. Dict[str, str]: A dictionary containing the object's data in JSON format.
  78. """
  79. data = {}
  80. data["input_path"] = self["input_path"]
  81. data["page_index"] = self["page_index"]
  82. data["model_settings"] = self["model_settings"]
  83. data["angle"] = self["angle"]
  84. return JsonMixin._to_json(data, *args, **kwargs)