table_recognition.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 numpy as np
  15. from ..base import BasePipeline
  16. from ...predictors import create_predictor
  17. from ..ocr import OCRPipeline
  18. from ...components import CropByBoxes
  19. from ...results import TableResult, StructureTableResult
  20. from .utils import *
  21. class TableRecPipeline(BasePipeline):
  22. """Table Recognition Pipeline"""
  23. def __init__(
  24. self,
  25. layout_model,
  26. text_det_model,
  27. text_rec_model,
  28. table_model,
  29. batch_size=1,
  30. device="gpu",
  31. chat_ocr=False,
  32. ):
  33. self.layout_predictor = create_predictor(
  34. model=layout_model, device=device, batch_size=batch_size
  35. )
  36. self.ocr_pipeline = OCRPipeline(
  37. text_det_model, text_rec_model, batch_size, device
  38. )
  39. self.table_predictor = create_predictor(
  40. model=table_model, device=device, batch_size=batch_size
  41. )
  42. self._crop_by_boxes = CropByBoxes()
  43. self._match = TableMatch(filter_ocr_result=False)
  44. self.chat_ocr = chat_ocr
  45. super().__init__()
  46. def predict(self, x):
  47. batch_structure_res = []
  48. for batch_layout_pred, batch_ocr_pred in zip(
  49. self.layout_predictor(x), self.ocr_pipeline(x)
  50. ):
  51. for layout_pred, ocr_pred in zip(batch_layout_pred, batch_ocr_pred):
  52. single_img_res = {
  53. "img_path": "",
  54. "layout_result": {},
  55. "ocr_result": {},
  56. "table_result": [],
  57. }
  58. layout_res = layout_pred["result"]
  59. # update layout result
  60. single_img_res["img_path"] = layout_res["img_path"]
  61. single_img_res["layout_result"] = layout_res
  62. ocr_res = ocr_pred["result"]
  63. single_img_res["ocr_result"] = ocr_res
  64. all_subs_of_img = list(self._crop_by_boxes(layout_res))
  65. # get cropped images with label 'table'
  66. table_subs = []
  67. for batch_subs in all_subs_of_img:
  68. table_sub_list = []
  69. for sub in batch_subs:
  70. if sub["label"].lower() == "table":
  71. table_sub_list.append(sub)
  72. table_subs.append(table_sub_list)
  73. single_img_res["table_result"] = self.get_table_result(table_subs)
  74. batch_structure_res.append({"result": TableResult(single_img_res)})
  75. yield batch_structure_res
  76. def get_ocr_result_by_bbox(self, box, ocr_res):
  77. dt_polys_list = []
  78. rec_text_list = []
  79. unmatched_ocr_res = {"dt_polys": [], "rec_text": []}
  80. for text_box, text_res in zip(ocr_res["dt_polys"], ocr_res["rec_text"]):
  81. text_box_area = convert_4point2rect(text_box)
  82. if is_inside(box, text_box_area):
  83. dt_polys_list.append(text_box)
  84. rec_text_list.append(text_res)
  85. else:
  86. unmatched_ocr_res["dt_polys"].append(text_box)
  87. unmatched_ocr_res["rec_text"].append(text_res)
  88. return (dt_polys_list, rec_text_list), unmatched_ocr_res
  89. def get_table_result(self, input_img):
  90. table_res_list = []
  91. table_index = 0
  92. for batch_input, batch_table_pred, batch_ocr_pred in zip(
  93. input_img, self.table_predictor(input_img), self.ocr_pipeline(input_img)
  94. ):
  95. batch_res_list = []
  96. for input, table_pred, ocr_pred in zip(
  97. batch_input, batch_table_pred, batch_ocr_pred
  98. ):
  99. single_table_res = table_pred["result"]
  100. ocr_res = ocr_pred["result"]
  101. single_table_box = single_table_res["bbox"]
  102. ori_x, ori_y, _, _ = input["box"]
  103. ori_bbox_list = np.array(
  104. get_ori_coordinate_for_table(ori_x, ori_y, single_table_box),
  105. dtype=np.float32,
  106. )
  107. html_res = self._match(single_table_res, ocr_res)
  108. batch_res_list.append(
  109. StructureTableResult(
  110. {
  111. "img_path": input["img_path"],
  112. "bbox": ori_bbox_list,
  113. "img_idx": table_index,
  114. "ocr_res": ocr_res,
  115. "html": html_res,
  116. }
  117. )
  118. )
  119. table_index += 1
  120. table_res_list.append(batch_res_list)
  121. return table_res_list