chat_ocr.py 3.8 KB

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