table_rec.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. from pathlib import Path
  15. import json
  16. import numpy as np
  17. import cv2
  18. from ...utils import logging
  19. from ..utils.io import JsonWriter, ImageWriter, ImageReader
  20. from .base import BaseResult
  21. class TableRecResult(BaseResult):
  22. """SaveTableResults"""
  23. def __init__(self, data):
  24. super().__init__(data)
  25. self._img_writer.set_backend("pillow")
  26. def _get_res_img(self):
  27. image = self._img_reader.read(self["img_path"])
  28. bbox_res = self["bbox"]
  29. if len(bbox_res) > 0 and len(bbox_res[0]) == 4:
  30. vis_img = self.draw_rectangle(image, bbox_res)
  31. else:
  32. vis_img = self.draw_bbox(image, bbox_res)
  33. return vis_img
  34. def draw_rectangle(self, image, boxes):
  35. """draw_rectangle"""
  36. boxes = np.array(boxes)
  37. img_show = image.copy()
  38. for box in boxes.astype(int):
  39. x1, y1, x2, y2 = box
  40. cv2.rectangle(img_show, (x1, y1), (x2, y2), (255, 0, 0), 2)
  41. return img_show
  42. def draw_bbox(self, image, boxes):
  43. """draw_bbox"""
  44. for box in boxes:
  45. box = np.reshape(np.array(box), [-1, 1, 2]).astype(np.int64)
  46. image = cv2.polylines(np.array(image), [box], True, (255, 0, 0), 2)
  47. return image
  48. class StructureResult(BaseResult):
  49. """StructureResult"""
  50. def __init__(self, data):
  51. super().__init__(data)
  52. self._img_writer.set_backend("pillow")
  53. def _get_res_img(self):
  54. return self._img_reader.read(self["img_path"])