chat_ocr.py 3.7 KB

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