chat_ocr.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. table_result_num = len(self["table_result"])
  56. for idx in range(table_result_num):
  57. self["table_result"][idx]["input_path"] = "{}_{:04d}".format(
  58. table_save_path, idx + 1
  59. )
  60. if not str(save_path).endswith(".json"):
  61. save_path = "{}.json".format(save_path)
  62. super().save_to_json(save_path)
  63. def save_to_html(self, save_path):
  64. if not save_path.lower().endswith(("html")):
  65. save_path = self.get_target_name(save_path)
  66. else:
  67. save_path = Path(save_path).stem
  68. table_save_path = f"{save_path}_table"
  69. for idx, table_result in enumerate(self["table_result"]):
  70. basename = (Path(table_result["input_path"]).name).split(".")[0]
  71. table_result["input_path"] = Path(
  72. str(table_result["input_path"]).replace(
  73. basename, "{}_{:04d}".format(table_save_path, idx + 1)
  74. )
  75. )
  76. table_result.save_to_html(save_path)
  77. def save_to_xlsx(self, save_path):
  78. if not save_path.lower().endswith(("xlsx")):
  79. save_path = self.get_target_name(save_path)
  80. else:
  81. save_path = Path(save_path).stem
  82. table_save_path = f"{save_path}_table"
  83. for idx, table_result in enumerate(self["table_result"]):
  84. basename = (Path(table_result["input_path"]).name).split(".")[0]
  85. table_result["input_path"] = Path(
  86. str(table_result["input_path"]).replace(
  87. basename, "{}_{:04d}".format(table_save_path, idx + 1)
  88. )
  89. )
  90. table_result.save_to_xlsx(save_path)
  91. def save_to_img(self, save_path):
  92. if not save_path.lower().endswith((".jpg", ".png")):
  93. save_path = self.get_target_name(save_path)
  94. else:
  95. save_path = Path(save_path).stem
  96. oricls_save_path = f"{save_path}_oricls.jpg"
  97. oricls_result = self["oricls_result"]
  98. if oricls_result:
  99. oricls_result.save_to_img(oricls_save_path)
  100. uvdoc_save_path = f"{save_path}_uvdoc.jpg"
  101. unwarp_result = self["unwarp_result"]
  102. if unwarp_result:
  103. unwarp_result.save_to_img(uvdoc_save_path)
  104. curve_save_path = f"{save_path}_curve"
  105. curve_results = self["curve_result"]
  106. # TODO(): support list of result
  107. if isinstance(curve_results, dict):
  108. curve_results = [curve_results]
  109. for idx, curve_result in enumerate(curve_results):
  110. curve_result.save_to_img(f"{curve_save_path}_{idx}.jpg")
  111. layout_save_path = f"{save_path}_layout.jpg"
  112. layout_result = self["layout_result"]
  113. if layout_result:
  114. layout_result.save_to_img(layout_save_path)
  115. ocr_save_path = f"{save_path}_ocr.jpg"
  116. table_save_path = f"{save_path}_table"
  117. ocr_result = self["ocr_result"]
  118. if ocr_result:
  119. ocr_result.save_to_img(ocr_save_path)
  120. for idx, table_result in enumerate(self["table_result"]):
  121. table_result.save_to_img(f"{table_save_path}_{idx}.jpg")
  122. class VectorResult(BaseResult, Base64Mixin):
  123. """VisualInfoResult"""
  124. def _to_base64(self):
  125. return self["vector"]
  126. class RetrievalResult(BaseResult):
  127. """VisualInfoResult"""
  128. pass
  129. class ChatResult(BaseResult):
  130. """VisualInfoResult"""
  131. pass