result.py 3.7 KB

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