result.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 os
  15. from pathlib import Path
  16. class LayoutParsingResult(dict):
  17. """Layout Parsing Result"""
  18. def __init__(self, data) -> None:
  19. """Initializes a new instance of the class with the specified data."""
  20. super().__init__(data)
  21. def save_results(self, save_path: str) -> None:
  22. """Save the layout parsing results to the specified directory.
  23. Args:
  24. save_path (str): The directory path to save the results.
  25. """
  26. if not os.path.isdir(save_path):
  27. return
  28. img_id = self["img_id"]
  29. layout_det_res = self["layout_det_res"]
  30. save_img_path = Path(save_path) / f"layout_det_result_img{img_id}.jpg"
  31. layout_det_res.save_to_img(save_img_path)
  32. input_params = self["input_params"]
  33. if input_params["use_doc_preprocessor"]:
  34. save_img_path = Path(save_path) / f"doc_preprocessor_result_img{img_id}.jpg"
  35. self["doc_preprocessor_res"].save_to_img(save_img_path)
  36. if input_params["use_general_ocr"]:
  37. save_img_path = (
  38. Path(save_path) / f"text_paragraphs_ocr_result_img{img_id}.jpg"
  39. )
  40. self["text_paragraphs_ocr_res"].save_to_img(save_img_path)
  41. if input_params["use_general_ocr"] or input_params["use_table_recognition"]:
  42. save_img_path = Path(save_path) / f"overall_ocr_result_img{img_id}.jpg"
  43. self["overall_ocr_res"].save_to_img(save_img_path)
  44. if input_params["use_table_recognition"]:
  45. for tno in range(len(self["table_res_list"])):
  46. table_res = self["table_res_list"][tno]
  47. table_region_id = table_res["table_region_id"]
  48. save_img_path = (
  49. Path(save_path)
  50. / f"table_res_cell_img{img_id}_region{table_region_id}.jpg"
  51. )
  52. table_res.save_to_img(save_img_path)
  53. save_html_path = (
  54. Path(save_path)
  55. / f"table_res_img{img_id}_region{table_region_id}.html"
  56. )
  57. table_res.save_to_html(save_html_path)
  58. save_xlsx_path = (
  59. Path(save_path)
  60. / f"table_res_img{img_id}_region{table_region_id}.xlsx"
  61. )
  62. table_res.save_to_xlsx(save_xlsx_path)
  63. if input_params["use_seal_recognition"]:
  64. for sno in range(len(self["seal_res_list"])):
  65. seal_res = self["seal_res_list"][sno]
  66. seal_region_id = seal_res["seal_region_id"]
  67. save_img_path = (
  68. Path(save_path) / f"seal_res_img{img_id}_region{seal_region_id}.jpg"
  69. )
  70. seal_res.save_to_img(save_img_path)
  71. return