result.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 TextDetResult(BaseCVResult):
  23. def _get_input_fn(self):
  24. fn = super()._get_input_fn()
  25. if (page_idx := self["page_index"]) is not None:
  26. fp = Path(fn)
  27. stem, suffix = fp.stem, fp.suffix
  28. return f"{stem}_{page_idx}{suffix}"
  29. else:
  30. return fn
  31. def _to_img(self):
  32. """draw rectangle"""
  33. boxes = self["dt_polys"]
  34. image = self["input_img"]
  35. for box in boxes:
  36. box = np.reshape(np.array(box).astype(int), [-1, 1, 2]).astype(np.int64)
  37. cv2.polylines(image, [box], True, (0, 0, 255), 2)
  38. return {"res": image[:, :, ::-1]}
  39. def _to_str(self, *args, **kwargs):
  40. data = copy.deepcopy(self)
  41. data.pop("input_img")
  42. return JsonMixin._to_str(data, *args, **kwargs)
  43. def _to_json(self, *args, **kwargs):
  44. data = copy.deepcopy(self)
  45. data.pop("input_img")
  46. return JsonMixin._to_json(data, *args, **kwargs)