manager.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. from __future__ import absolute_import
  15. from abc import ABC, abstractmethod
  16. from ... import c_lib_wrap as C
  17. class ProcessorManager:
  18. def __init__(self):
  19. self._manager = None
  20. def run(self, input_ims):
  21. """Process input image
  22. :param: input_ims: (list of numpy.ndarray) The input images
  23. :return: list of FDTensor
  24. """
  25. return self._manager.run(input_ims)
  26. def use_cuda(self, enable_cv_cuda=False, gpu_id=-1):
  27. """Use CUDA processors
  28. :param: enable_cv_cuda: Ture: use CV-CUDA, False: use CUDA only
  29. :param: gpu_id: GPU device id
  30. """
  31. return self._manager.use_cuda(enable_cv_cuda, gpu_id)
  32. class PyProcessorManager(ABC):
  33. """
  34. PyProcessorManager is used to define a customized processor in python
  35. """
  36. def __init__(self):
  37. self._manager = C.vision.processors.ProcessorManager()
  38. def use_cuda(self, enable_cv_cuda=False, gpu_id=-1):
  39. """Use CUDA processors
  40. :param: enable_cv_cuda: Ture: use CV-CUDA, False: use CUDA only
  41. :param: gpu_id: GPU device id
  42. """
  43. return self._manager.use_cuda(enable_cv_cuda, gpu_id)
  44. def __call__(self, images):
  45. image_batch = C.vision.FDMatBatch()
  46. image_batch.from_mats(images)
  47. self._manager.pre_apply(image_batch)
  48. outputs = self.apply(image_batch)
  49. self._manager.post_apply()
  50. return outputs
  51. @abstractmethod
  52. def apply(self, image_batch):
  53. print("This function has to be implemented.")
  54. return []