chat_ocr.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 _to_str(self, _, *args, **kwargs):
  27. return super()._to_str(
  28. {"layout_parsing_result": self["layout_parsing_result"]}, *args, **kwargs
  29. )
  30. def save_to_html(self, save_path):
  31. if not save_path.lower().endswith(("html")):
  32. input_path = self["input_path"]
  33. save_path = Path(save_path) / f"{Path(input_path).stem}"
  34. else:
  35. save_path = Path(save_path).stem
  36. for table_result in self["table_result"]:
  37. table_result.save_to_html(save_path)
  38. def save_to_xlsx(self, save_path):
  39. if not save_path.lower().endswith(("xlsx")):
  40. input_path = self["input_path"]
  41. save_path = Path(save_path) / f"{Path(input_path).stem}"
  42. else:
  43. save_path = Path(save_path).stem
  44. for table_result in self["table_result"]:
  45. table_result.save_to_xlsx(save_path)
  46. def save_to_img(self, save_path):
  47. if not save_path.lower().endswith((".jpg", ".png")):
  48. input_path = self["input_path"]
  49. save_path = Path(save_path) / f"{Path(input_path).stem}"
  50. else:
  51. save_path = Path(save_path).stem
  52. oricls_save_path = f"{save_path}_oricls.jpg"
  53. oricls_result = self["oricls_result"]
  54. if oricls_result:
  55. oricls_result._HARD_FLAG = True
  56. oricls_result.save_to_img(oricls_save_path)
  57. uvdoc_save_path = f"{save_path}_uvdoc.jpg"
  58. unwarp_result = self["unwarp_result"]
  59. if unwarp_result:
  60. # unwarp_result._HARD_FLAG = True
  61. unwarp_result.save_to_img(uvdoc_save_path)
  62. curve_save_path = f"{save_path}_curve"
  63. curve_results = self["curve_result"]
  64. # TODO(): support list of result
  65. if isinstance(curve_results, dict):
  66. curve_results = [curve_results]
  67. for idx, curve_result in enumerate(curve_results):
  68. curve_result._HARD_FLAG = True if not unwarp_result else False
  69. curve_result.save_to_img(f"{curve_save_path}_{idx}.jpg")
  70. layout_save_path = f"{save_path}_layout.jpg"
  71. layout_result = self["layout_result"]
  72. if layout_result:
  73. layout_result._HARD_FLAG = True if not unwarp_result else False
  74. layout_result.save_to_img(layout_save_path)
  75. ocr_save_path = f"{save_path}_ocr.jpg"
  76. table_save_path = f"{save_path}_table"
  77. ocr_result = self["ocr_result"]
  78. if ocr_result:
  79. ocr_result._HARD_FLAG = True if not unwarp_result else False
  80. ocr_result.save_to_img(ocr_save_path)
  81. for idx, table_result in enumerate(self["table_result"]):
  82. table_result._HARD_FLAG = True if not unwarp_result else False
  83. table_result.save_to_img(f"{table_save_path}_{idx}.jpg")
  84. class VectorResult(BaseResult, Base64Mixin):
  85. """VisualInfoResult"""
  86. def _to_base64(self):
  87. return self["vector"]
  88. class RetrievalResult(BaseResult):
  89. """VisualInfoResult"""
  90. pass
  91. class ChatResult(BaseResult):
  92. """VisualInfoResult"""
  93. pass