result.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 copy
  15. from pathlib import Path
  16. import numpy as np
  17. from ....utils.deps import class_requires_deps, is_dep_available
  18. from ...common.result import BaseCVResult, JsonMixin
  19. if is_dep_available("opencv-contrib-python"):
  20. import cv2
  21. @class_requires_deps("opencv-contrib-python")
  22. class TableRecResult(BaseCVResult):
  23. """SaveTableResults"""
  24. def _get_input_fn(self):
  25. fn = super()._get_input_fn()
  26. if (page_idx := self["page_index"]) is not None:
  27. fp = Path(fn)
  28. stem, suffix = fp.stem, fp.suffix
  29. return f"{stem}_{page_idx}{suffix}"
  30. else:
  31. return fn
  32. def _to_img(self):
  33. image = self["input_img"]
  34. bbox_res = self["bbox"]
  35. if len(bbox_res) > 0 and len(bbox_res[0]) == 4:
  36. vis_img = self.draw_rectangle(image, bbox_res)
  37. else:
  38. vis_img = self.draw_bbox(image, bbox_res)
  39. return {"res": vis_img}
  40. def draw_rectangle(self, image, boxes):
  41. """draw_rectangle"""
  42. boxes = np.array(boxes)
  43. img_show = image.copy()
  44. for box in boxes.astype(int):
  45. x1, y1, x2, y2 = box
  46. cv2.rectangle(img_show, (x1, y1), (x2, y2), (255, 0, 0), 2)
  47. return img_show
  48. def draw_bbox(self, image, boxes):
  49. """draw_bbox"""
  50. for box in boxes:
  51. box = np.reshape(np.array(box), [-1, 1, 2]).astype(np.int64)
  52. image = cv2.polylines(np.array(image), [box], True, (255, 0, 0), 2)
  53. return image
  54. def _to_str(self, *args, **kwargs):
  55. data = copy.deepcopy(self)
  56. data.pop("input_img")
  57. return JsonMixin._to_str(data, *args, **kwargs)
  58. def _to_json(self, *args, **kwargs):
  59. data = copy.deepcopy(self)
  60. data.pop("input_img")
  61. return JsonMixin._to_json(data, *args, **kwargs)