table_rec.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 cv2
  15. import numpy as np
  16. from pathlib import Path
  17. import PIL
  18. from PIL import Image, ImageDraw, ImageFont
  19. from .utils.mixin import HtmlMixin, XlsxMixin
  20. from .base import BaseResult, CVResult
  21. class TableRecResult(CVResult):
  22. """SaveTableResults"""
  23. _HARD_FLAG = False
  24. def __init__(self, data):
  25. super().__init__(data)
  26. def _to_img(self):
  27. image = self._img_reader.read(self["input_path"])
  28. if self._HARD_FLAG:
  29. image_np = np.array(image)
  30. image = Image.fromarray(image_np[:, :, ::-1])
  31. bbox_res = self["bbox"]
  32. if len(bbox_res) > 0 and len(bbox_res[0]) == 4:
  33. vis_img = self.draw_rectangle(image, bbox_res)
  34. else:
  35. vis_img = self.draw_bbox(image, bbox_res)
  36. return vis_img
  37. def draw_rectangle(self, image, boxes):
  38. """draw_rectangle"""
  39. boxes = np.array(boxes)
  40. img_show = image.copy()
  41. for box in boxes.astype(int):
  42. x1, y1, x2, y2 = box
  43. cv2.rectangle(img_show, (x1, y1), (x2, y2), (255, 0, 0), 2)
  44. return img_show
  45. def draw_bbox(self, image, boxes):
  46. """draw_bbox"""
  47. for box in boxes:
  48. box = np.reshape(np.array(box), [-1, 1, 2]).astype(np.int64)
  49. image = cv2.polylines(np.array(image), [box], True, (255, 0, 0), 2)
  50. return image
  51. class StructureTableResult(TableRecResult, HtmlMixin, XlsxMixin):
  52. """StructureTableResult"""
  53. def __init__(self, data):
  54. super().__init__(data)
  55. HtmlMixin.__init__(self)
  56. XlsxMixin.__init__(self)
  57. def _to_html(self):
  58. return self["html"]
  59. class TableResult(BaseResult):
  60. """TableResult"""
  61. def save_to_html(self, save_path):
  62. if not save_path.lower().endswith(("html")):
  63. input_path = self["input_path"]
  64. save_path = Path(save_path) / f"{Path(input_path).stem}"
  65. else:
  66. save_path = Path(save_path).stem
  67. for table_result in self["table_result"]:
  68. table_result.save_to_html(save_path)
  69. def save_to_xlsx(self, save_path):
  70. if not save_path.lower().endswith(("xlsx")):
  71. input_path = self["input_path"]
  72. save_path = Path(save_path) / f"{Path(input_path).stem}"
  73. else:
  74. save_path = Path(save_path).stem
  75. for table_result in self["table_result"]:
  76. table_result.save_to_xlsx(save_path)
  77. def save_to_img(self, save_path):
  78. if not save_path.lower().endswith((".jpg", ".png")):
  79. input_path = self["input_path"]
  80. save_path = Path(save_path) / f"{Path(input_path).stem}"
  81. else:
  82. save_path = Path(save_path).stem
  83. layout_save_path = f"{save_path}_layout.jpg"
  84. ocr_save_path = f"{save_path}_ocr.jpg"
  85. table_save_path = f"{save_path}_table.jpg"
  86. layout_result = self["layout_result"]
  87. layout_result.save_to_img(layout_save_path)
  88. ocr_result = self["ocr_result"]
  89. ocr_result.save_to_img(ocr_save_path)
  90. for table_result in self["table_result"]:
  91. table_result.save_to_img(table_save_path)