base.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. class BaseResult(dict, StrMixin, JsonMixin):
  30. def __init__(self, data):
  31. super().__init__(data)
  32. self._show_funcs = []
  33. StrMixin.__init__(self)
  34. JsonMixin.__init__(self)
  35. def save_all(self, save_path):
  36. for func in self._show_funcs:
  37. signature = inspect.signature(func)
  38. if "save_path" in signature.parameters:
  39. func(save_path=save_path)
  40. else:
  41. func()
  42. class CVResult(BaseResult, ImgMixin):
  43. def __init__(self, data):
  44. super().__init__(data)
  45. ImgMixin.__init__(self, "pillow")
  46. self._img_reader = ImageReader(backend="pillow")
  47. self._img_writer = ImageWriter(backend="pillow")