blazeface.py 5.1 KB

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