base_result.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. import inspect
  15. from ....utils.io import ImageReader, ImageWriter
  16. from ..utils.mixin import JsonMixin, ImgMixin, StrMixin
  17. from typing import Dict
  18. class BaseResult(dict, StrMixin, JsonMixin):
  19. """Base Result"""
  20. def __init__(self, data: Dict) -> None:
  21. """Initializes the instance with the provided data.
  22. Args:
  23. data (Dict): The data to initialize the instance with.
  24. """
  25. super().__init__(data)
  26. self._show_funcs = []
  27. StrMixin.__init__(self)
  28. JsonMixin.__init__(self)
  29. def save_all(self, save_path: str) -> None:
  30. """
  31. Save all show functions to the specified path if they accept a save_path argument.
  32. Args:
  33. save_path (str): The path to save the functions' output.
  34. Returns:
  35. None
  36. """
  37. for func in self._show_funcs:
  38. signature = inspect.signature(func)
  39. if "save_path" in signature.parameters:
  40. func(save_path=save_path)
  41. else:
  42. func()
  43. class CVResult(BaseResult, ImgMixin):
  44. """Result For Computer Vision Tasks"""
  45. def __init__(self, data: Dict) -> None:
  46. """Initializes the instance with the given data and sets up image processing with the 'pillow' backend.
  47. Args:
  48. data (Dict): The data to initialize the instance with.
  49. """
  50. super().__init__(data)
  51. ImgMixin.__init__(self, "pillow")
  52. self._img_reader = ImageReader(backend="pillow")
  53. self._img_writer = ImageWriter(backend="pillow")