result.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 typing import Dict
  16. from pathlib import Path
  17. import numpy as np
  18. import cv2
  19. from ...common.result import BaseCVResult, HtmlMixin, XlsxMixin
  20. class SingleTableRecognitionResult(BaseCVResult, HtmlMixin, XlsxMixin):
  21. """table recognition result"""
  22. def __init__(self, data: Dict) -> None:
  23. """Initializes the object with given data and sets up mixins for HTML and XLSX processing."""
  24. super().__init__(data)
  25. HtmlMixin.__init__(self) # Initializes the HTML mixin functionality
  26. XlsxMixin.__init__(self) # Initializes the XLSX mixin functionality
  27. def _to_html(self) -> str:
  28. """Converts the prediction to its corresponding HTML representation.
  29. Returns:
  30. str: The HTML string representation of the prediction.
  31. """
  32. return self["pred_html"]
  33. def _to_xlsx(self) -> str:
  34. """Converts the prediction HTML to an XLSX file path.
  35. Returns:
  36. str: The path to the XLSX file containing the prediction data.
  37. """
  38. return self["pred_html"]
  39. def _to_img(self) -> np.ndarray:
  40. """
  41. Convert the input image with table OCR predictions to an image with cell boundaries highlighted.
  42. Returns:
  43. np.ndarray: The input image with cell boundaries highlighted in red.
  44. """
  45. input_img = self["table_ocr_pred"]["input_img"].copy()
  46. cell_box_list = self["cell_box_list"]
  47. for box in cell_box_list:
  48. x1, y1, x2, y2 = [int(pos) for pos in box]
  49. cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
  50. return input_img
  51. class TableRecognitionResult(dict):
  52. """Layout Parsing Result"""
  53. def __init__(self, data) -> None:
  54. """Initializes a new instance of the class with the specified data."""
  55. super().__init__(data)
  56. def save_results(self, save_path: str) -> None:
  57. """Save the table recognition results to the specified directory.
  58. Args:
  59. save_path (str): The directory path to save the results.
  60. """
  61. if not os.path.isdir(save_path):
  62. return
  63. img_id = self["img_id"]
  64. layout_det_res = self["layout_det_res"]
  65. if len(layout_det_res) > 0:
  66. save_img_path = Path(save_path) / f"layout_det_result_img{img_id}.jpg"
  67. layout_det_res.save_to_img(save_img_path)
  68. input_params = self["input_params"]
  69. if input_params["use_doc_preprocessor"]:
  70. save_img_path = Path(save_path) / f"doc_preprocessor_result_img{img_id}.jpg"
  71. self["doc_preprocessor_res"].save_to_img(save_img_path)
  72. save_img_path = Path(save_path) / f"overall_ocr_result_img{img_id}.jpg"
  73. self["overall_ocr_res"].save_to_img(save_img_path)
  74. for tno in range(len(self["table_res_list"])):
  75. table_res = self["table_res_list"][tno]
  76. table_region_id = table_res["table_region_id"]
  77. save_img_path = (
  78. Path(save_path)
  79. / f"table_res_cell_img{img_id}_region{table_region_id}.jpg"
  80. )
  81. table_res.save_to_img(save_img_path)
  82. save_html_path = (
  83. Path(save_path) / f"table_res_img{img_id}_region{table_region_id}.html"
  84. )
  85. table_res.save_to_html(save_html_path)
  86. save_xlsx_path = (
  87. Path(save_path) / f"table_res_img{img_id}_region{table_region_id}.xlsx"
  88. )
  89. table_res.save_to_xlsx(save_xlsx_path)
  90. return