result.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 {"res": 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