centerface.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 .... import UltraInferModel, ModelFormat
  16. from .... import c_lib_wrap as C
  17. class CenterFacePreprocessor:
  18. def __init__(self):
  19. """Create a preprocessor for CenterFace"""
  20. self._preprocessor = C.vision.facedet.CenterFacePreprocessor()
  21. def run(self, input_ims):
  22. """Preprocess input images for CenterFace
  23. :param: input_ims: (list of numpy.ndarray)The input image
  24. :return: list of FDTensor
  25. """
  26. return self._preprocessor.run(input_ims)
  27. @property
  28. def size(self):
  29. """
  30. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [640, 640]
  31. """
  32. return self._preprocessor.size
  33. @size.setter
  34. def size(self, wh):
  35. assert isinstance(
  36. wh, (list, tuple)
  37. ), "The value to set `size` must be type of tuple or list."
  38. assert (
  39. len(wh) == 2
  40. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  41. len(wh)
  42. )
  43. self._preprocessor.size = wh
  44. class CenterFacePostprocessor:
  45. def __init__(self):
  46. """Create a postprocessor for CenterFace"""
  47. self._postprocessor = C.vision.facedet.CenterFacePostprocessor()
  48. def run(self, runtime_results, ims_info):
  49. """Postprocess the runtime results for CenterFace
  50. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  51. :param: ims_info: (list of dict)Record input_shape and output_shape
  52. :return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  53. """
  54. return self._postprocessor.run(runtime_results, ims_info)
  55. @property
  56. def conf_threshold(self):
  57. """
  58. confidence threshold for postprocessing, default is 0.5
  59. """
  60. return self._postprocessor.conf_threshold
  61. @property
  62. def nms_threshold(self):
  63. """
  64. nms threshold for postprocessing, default is 0.3
  65. """
  66. return self._postprocessor.nms_threshold
  67. @conf_threshold.setter
  68. def conf_threshold(self, conf_threshold):
  69. assert isinstance(
  70. conf_threshold, float
  71. ), "The value to set `conf_threshold` must be type of float."
  72. self._postprocessor.conf_threshold = conf_threshold
  73. @nms_threshold.setter
  74. def nms_threshold(self, nms_threshold):
  75. assert isinstance(
  76. nms_threshold, float
  77. ), "The value to set `nms_threshold` must be type of float."
  78. self._postprocessor.nms_threshold = nms_threshold
  79. class CenterFace(UltraInferModel):
  80. def __init__(
  81. self,
  82. model_file,
  83. params_file="",
  84. runtime_option=None,
  85. model_format=ModelFormat.ONNX,
  86. ):
  87. """Load a CenterFace model exported by CenterFace.
  88. :param model_file: (str)Path of model file, e.g ./CenterFace.onnx
  89. :param params_file: (str)Path of parameters file, e.g yolox/model.pdiparams, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  90. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  91. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  92. """
  93. super(CenterFace, self).__init__(runtime_option)
  94. self._model = C.vision.facedet.CenterFace(
  95. model_file, params_file, self._runtime_option, model_format
  96. )
  97. assert self.initialized, "CenterFace initialize failed."
  98. def predict(self, input_image):
  99. """Detect the location and key points of human faces from an input image
  100. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  101. :return: FaceDetectionResult
  102. """
  103. return self._model.predict(input_image)
  104. def batch_predict(self, images):
  105. """Classify a batch of input image
  106. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  107. :return list of DetectionResult
  108. """
  109. return self._model.batch_predict(images)
  110. @property
  111. def preprocessor(self):
  112. """Get CenterFacePreprocessor object of the loaded model
  113. :return CenterFacePreprocessor
  114. """
  115. return self._model.preprocessor
  116. @property
  117. def postprocessor(self):
  118. """Get CenterFacePostprocessor object of the loaded model
  119. :return CenterFacePostprocessor
  120. """
  121. return self._model.postprocessor