base.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 abc import ABC, abstractmethod
  15. from ....utils.subclass_register import AutoRegisterABCMetaClass
  16. import inspect
  17. from ....utils.func_register import FuncRegister
  18. from ...utils.io import ImageReader, ImageWriter
  19. from .utils.mixin import JsonMixin, ImgMixin, StrMixin
  20. class BaseComponent(ABC, metaclass=AutoRegisterABCMetaClass):
  21. """Base Component"""
  22. __is_base = True
  23. def __init__(self):
  24. super().__init__()
  25. @abstractmethod
  26. def __call__(self):
  27. raise NotImplementedError(
  28. "The component method `__call__` has not been implemented yet."
  29. )
  30. class BaseResult(dict, StrMixin, JsonMixin):
  31. def __init__(self, data):
  32. super().__init__(data)
  33. self._show_funcs = []
  34. StrMixin.__init__(self)
  35. JsonMixin.__init__(self)
  36. def save_all(self, save_path):
  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. def __init__(self, data):
  45. super().__init__(data)
  46. ImgMixin.__init__(self, "pillow")
  47. self._img_reader = ImageReader(backend="pillow")
  48. self._img_writer = ImageWriter(backend="pillow")