scrfd.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 SCRFD(UltraInferModel):
  18. def __init__(
  19. self,
  20. model_file,
  21. params_file="",
  22. runtime_option=None,
  23. model_format=ModelFormat.ONNX,
  24. ):
  25. """Load a SCRFD model exported by SCRFD.
  26. :param model_file: (str)Path of model file, e.g ./scrfd.onnx
  27. :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
  28. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  29. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  30. """
  31. # 调用基函数进行backend_option的初始化
  32. # 初始化后的option保存在self._runtime_option
  33. super(SCRFD, self).__init__(runtime_option)
  34. self._model = C.vision.facedet.SCRFD(
  35. model_file, params_file, self._runtime_option, model_format
  36. )
  37. # 通过self.initialized判断整个模型的初始化是否成功
  38. assert self.initialized, "SCRFD initialize failed."
  39. def predict(self, input_image, conf_threshold=0.7, nms_iou_threshold=0.3):
  40. """Detect the location and key points of human faces from an input image
  41. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  42. :param conf_threshold: confidence threshold for postprocessing, default is 0.7
  43. :param nms_iou_threshold: iou threshold for NMS, default is 0.3
  44. :return: FaceDetectionResult
  45. """
  46. return self._model.predict(input_image, conf_threshold, nms_iou_threshold)
  47. def disable_normalize(self):
  48. """
  49. This function will disable normalize in preprocessing step.
  50. """
  51. self._model.disable_normalize()
  52. def disable_permute(self):
  53. """
  54. This function will disable hwc2chw in preprocessing step.
  55. """
  56. self._model.disable_permute()
  57. # 一些跟SCRFD模型有关的属性封装
  58. # 多数是预处理相关,可通过修改如model.size = [640, 640]改变预处理时resize的大小(前提是模型支持)
  59. @property
  60. def size(self):
  61. """
  62. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default (640, 640)
  63. """
  64. return self._model.size
  65. @property
  66. def padding_value(self):
  67. # padding value, size should be the same as channels
  68. return self._model.padding_value
  69. @property
  70. def is_no_pad(self):
  71. # while is_mini_pad = false and is_no_pad = true, will resize the image to the set size
  72. return self._model.is_no_pad
  73. @property
  74. def is_mini_pad(self):
  75. # only pad to the minimum rectangle which height and width is times of stride
  76. return self._model.is_mini_pad
  77. @property
  78. def is_scale_up(self):
  79. # if is_scale_up is false, the input image only can be zoom out, the maximum resize scale cannot exceed 1.0
  80. return self._model.is_scale_up
  81. @property
  82. def stride(self):
  83. # padding stride, for is_mini_pad
  84. return self._model.stride
  85. @property
  86. def downsample_strides(self):
  87. """
  88. Argument for image postprocessing step,
  89. downsample strides (namely, steps) for SCRFD to generate anchors,
  90. will take (8,16,32) as default values
  91. """
  92. return self._model.downsample_strides
  93. @property
  94. def landmarks_per_face(self):
  95. """
  96. Argument for image postprocessing step, landmarks_per_face, default 5 in SCRFD
  97. """
  98. return self._model.landmarks_per_face
  99. @property
  100. def use_kps(self):
  101. """
  102. Argument for image postprocessing step,
  103. the outputs of onnx file with key points features or not, default true
  104. """
  105. return self._model.use_kps
  106. @property
  107. def max_nms(self):
  108. """
  109. Argument for image postprocessing step, the upperbond number of boxes processed by nms, default 30000
  110. """
  111. return self._model.max_nms
  112. @property
  113. def num_anchors(self):
  114. """
  115. Argument for image postprocessing step, anchor number of each stride, default 2
  116. """
  117. return self._model.num_anchors
  118. @size.setter
  119. def size(self, wh):
  120. assert isinstance(
  121. wh, (list, tuple)
  122. ), "The value to set `size` must be type of tuple or list."
  123. assert (
  124. len(wh) == 2
  125. ), "The value to set `size` must contains 2 elements means [width, height], but now it contains {} elements.".format(
  126. len(wh)
  127. )
  128. self._model.size = wh
  129. @padding_value.setter
  130. def padding_value(self, value):
  131. assert isinstance(
  132. value, list
  133. ), "The value to set `padding_value` must be type of list."
  134. self._model.padding_value = value
  135. @is_no_pad.setter
  136. def is_no_pad(self, value):
  137. assert isinstance(
  138. value, bool
  139. ), "The value to set `is_no_pad` must be type of bool."
  140. self._model.is_no_pad = value
  141. @is_mini_pad.setter
  142. def is_mini_pad(self, value):
  143. assert isinstance(
  144. value, bool
  145. ), "The value to set `is_mini_pad` must be type of bool."
  146. self._model.is_mini_pad = value
  147. @is_scale_up.setter
  148. def is_scale_up(self, value):
  149. assert isinstance(
  150. value, bool
  151. ), "The value to set `is_scale_up` must be type of bool."
  152. self._model.is_scale_up = value
  153. @stride.setter
  154. def stride(self, value):
  155. assert isinstance(value, int), "The value to set `stride` must be type of int."
  156. self._model.stride = value
  157. @downsample_strides.setter
  158. def downsample_strides(self, value):
  159. assert isinstance(
  160. value, list
  161. ), "The value to set `downsample_strides` must be type of list."
  162. self._model.downsample_strides = value
  163. @landmarks_per_face.setter
  164. def landmarks_per_face(self, value):
  165. assert isinstance(
  166. value, int
  167. ), "The value to set `landmarks_per_face` must be type of int."
  168. self._model.landmarks_per_face = value
  169. @use_kps.setter
  170. def use_kps(self, value):
  171. assert isinstance(
  172. value, bool
  173. ), "The value to set `use_kps` must be type of bool."
  174. self._model.use_kps = value
  175. @max_nms.setter
  176. def max_nms(self, value):
  177. assert isinstance(value, int), "The value to set `max_nms` must be type of int."
  178. self._model.max_nms = value
  179. @num_anchors.setter
  180. def num_anchors(self, value):
  181. assert isinstance(
  182. value, int
  183. ), "The value to set `num_anchors` must be type of int."
  184. self._model.num_anchors = value