chat_ocr.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. from pathlib import Path
  16. from .base import BaseResult
  17. from .utils.mixin import Base64Mixin
  18. class LayoutParsingResult(BaseResult):
  19. """LayoutParsingResult"""
  20. pass
  21. class VisualInfoResult(BaseResult):
  22. """VisualInfoResult"""
  23. pass
  24. class VisualResult(BaseResult):
  25. """VisualInfoResult"""
  26. def __init__(self, data, page_id=None, src_input_name=None):
  27. super().__init__(data)
  28. self.page_id = page_id
  29. if isinstance(src_input_name, list):
  30. self.src_input_name = src_input_name[page_id]
  31. else:
  32. self.src_input_name = src_input_name
  33. def _to_str(self, _, *args, **kwargs):
  34. return super()._to_str(
  35. {"layout_parsing_result": self["layout_parsing_result"]}, *args, **kwargs
  36. )
  37. def get_target_name(self, save_path):
  38. if self.src_input_name.endswith(".pdf"):
  39. save_path = (
  40. Path(save_path)
  41. / f"{Path(self.src_input_name).stem}_pdf"
  42. / Path("page_{:04d}".format(self.page_id + 1))
  43. )
  44. else:
  45. save_path = Path(save_path) / f"{Path(self.src_input_name).stem}"
  46. return save_path
  47. def save_to_json(self, save_path):
  48. if not save_path.lower().endswith(("json")):
  49. save_path = self.get_target_name(save_path)
  50. else:
  51. save_path = Path(save_path).stem
  52. layout_save_path = f"{save_path}_layout.jpg"
  53. ocr_save_path = f"{save_path}_ocr.jpg"
  54. table_save_path = f"{save_path}_table"
  55. self["input_path"] = layout_save_path
  56. self["layout_result"]["input_path"] = layout_save_path
  57. self["layout_parsing_result"]["input_path"] = layout_save_path
  58. self["ocr_result"]["input_path"] = ocr_save_path
  59. table_result_num = len(self["table_result"])
  60. for idx in range(table_result_num):
  61. self["table_result"][idx]["input_path"] = "{}_{:04d}".format(
  62. table_save_path, idx + 1
  63. )
  64. if not str(save_path).endswith(".json"):
  65. save_path = "{}.json".format(save_path)
  66. super().save_to_json(save_path)
  67. def save_to_html(self, save_path):
  68. if not save_path.lower().endswith(("html")):
  69. save_path = self.get_target_name(save_path)
  70. else:
  71. save_path = Path(save_path).stem
  72. table_save_path = f"{save_path}_table"
  73. for idx, table_result in enumerate(self["table_result"]):
  74. basename = (Path(table_result["input_path"]).name).split(".")[0]
  75. table_result["input_path"] = Path(
  76. str(table_result["input_path"]).replace(
  77. basename, "{}_{:04d}".format(table_save_path, idx + 1)
  78. )
  79. )
  80. table_result.save_to_html(save_path)
  81. def save_to_xlsx(self, save_path):
  82. if not save_path.lower().endswith(("xlsx")):
  83. save_path = self.get_target_name(save_path)
  84. else:
  85. save_path = Path(save_path).stem
  86. table_save_path = f"{save_path}_table"
  87. for idx, table_result in enumerate(self["table_result"]):
  88. basename = (Path(table_result["input_path"]).name).split(".")[0]
  89. table_result["input_path"] = Path(
  90. str(table_result["input_path"]).replace(
  91. basename, "{}_{:04d}".format(table_save_path, idx + 1)
  92. )
  93. )
  94. table_result.save_to_xlsx(save_path)
  95. def save_to_img(self, save_path):
  96. if not save_path.lower().endswith((".jpg", ".png")):
  97. save_path = self.get_target_name(save_path)
  98. else:
  99. save_path = Path(save_path).stem
  100. oricls_save_path = f"{save_path}_oricls.jpg"
  101. oricls_result = self["oricls_result"]
  102. if oricls_result:
  103. oricls_result.save_to_img(oricls_save_path)
  104. uvdoc_save_path = f"{save_path}_uvdoc.jpg"
  105. unwarp_result = self["unwarp_result"]
  106. if unwarp_result:
  107. unwarp_result.save_to_img(uvdoc_save_path)
  108. curve_save_path = f"{save_path}_curve"
  109. curve_results = self["curve_result"]
  110. # TODO(): support list of result
  111. if isinstance(curve_results, dict):
  112. curve_results = [curve_results]
  113. for idx, curve_result in enumerate(curve_results):
  114. curve_result.save_to_img(f"{curve_save_path}_{idx}.jpg")
  115. layout_save_path = f"{save_path}_layout.jpg"
  116. layout_result = self["layout_result"]
  117. if layout_result:
  118. layout_result.save_to_img(layout_save_path)
  119. ocr_save_path = f"{save_path}_ocr.jpg"
  120. table_save_path = f"{save_path}_table"
  121. ocr_result = self["ocr_result"]
  122. if ocr_result:
  123. ocr_result.save_to_img(ocr_save_path)
  124. for idx, table_result in enumerate(self["table_result"]):
  125. table_result.save_to_img(f"{table_save_path}_{idx}.jpg")
  126. class VectorResult(BaseResult, Base64Mixin):
  127. """VisualInfoResult"""
  128. def _to_base64(self):
  129. return self["vector"]
  130. class RetrievalResult(BaseResult):
  131. """VisualInfoResult"""
  132. pass
  133. class ChatResult(BaseResult):
  134. """VisualInfoResult"""
  135. pass