yolov7face.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 Yolov7FacePreprocessor:
  19. def __init__(self):
  20. """Create a preprocessor for Yolov7Face"""
  21. self._preprocessor = C.vision.facedet.Yolov7Preprocessor()
  22. def run(self, input_ims):
  23. """Preprocess input images for Yolov7Face
  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 size(self):
  30. """
  31. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [640, 640]
  32. """
  33. return self._preprocessor.size
  34. @property
  35. def padding_color_value(self):
  36. """
  37. padding value for preprocessing, default [114.0, 114.0, 114.0]
  38. """
  39. # padding value, size should be the same as channels
  40. return self._preprocessor.padding_color_value
  41. @property
  42. def is_scale_up(self):
  43. """
  44. is_scale_up for preprocessing, the input image only can be zoom out, the maximum resize scale cannot exceed 1.0, default true
  45. """
  46. return self._preprocessor.is_scale_up
  47. @size.setter
  48. def size(self, wh):
  49. assert isinstance(
  50. wh, (list, tuple)
  51. ), "The value to set `size` must be type of tuple or list."
  52. assert (
  53. len(wh) == 2
  54. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  55. len(wh)
  56. )
  57. self._preprocessor.size = wh
  58. @padding_color_value.setter
  59. def padding_color_value(self, value):
  60. assert isinstance(
  61. value, list
  62. ), "The value to set `padding_color_value` must be type of list."
  63. self._preprocessor.padding_color_value = value
  64. @is_scale_up.setter
  65. def is_scale_up(self, value):
  66. assert isinstance(
  67. value, bool
  68. ), "The value to set `is_scale_up` must be type of bool."
  69. self._preprocessor.is_scale_up = value
  70. class Yolov7FacePostprocessor:
  71. def __init__(self):
  72. """Create a postprocessor for Yolov7Face"""
  73. self._postprocessor = C.vision.facedet.Yolov7FacePostprocessor()
  74. def run(self, runtime_results, ims_info):
  75. """Postprocess the runtime results for Yolov7Face
  76. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  77. :param: ims_info: (list of dict)Record input_shape and output_shape
  78. :return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  79. """
  80. return self._postprocessor.run(runtime_results, ims_info)
  81. @property
  82. def conf_threshold(self):
  83. """
  84. confidence threshold for postprocessing, default is 0.5
  85. """
  86. return self._postprocessor.conf_threshold
  87. @property
  88. def nms_threshold(self):
  89. """
  90. nms threshold for postprocessing, default is 0.45
  91. """
  92. return self._postprocessor.nms_threshold
  93. @property
  94. def landmarks_per_face(self):
  95. """
  96. landmarks per face for postprocessing, default is 5
  97. """
  98. return self._postprocessor.landmarks_per_face
  99. @conf_threshold.setter
  100. def conf_threshold(self, conf_threshold):
  101. assert isinstance(
  102. conf_threshold, float
  103. ), "The value to set `conf_threshold` must be type of float."
  104. self._postprocessor.conf_threshold = conf_threshold
  105. @nms_threshold.setter
  106. def nms_threshold(self, nms_threshold):
  107. assert isinstance(
  108. nms_threshold, float
  109. ), "The value to set `nms_threshold` must be type of float."
  110. self._postprocessor.nms_threshold = nms_threshold
  111. @landmarks_per_face.setter
  112. def landmarks_per_face(self, landmarks_per_face):
  113. assert isinstance(
  114. landmarks_per_face, int
  115. ), "The value to set `landmarks_per_face` must be type of int."
  116. self._postprocessor.landmarks_per_face = landmarks_per_face
  117. class YOLOv7Face(UltraInferModel):
  118. def __init__(
  119. self,
  120. model_file,
  121. params_file="",
  122. runtime_option=None,
  123. model_format=ModelFormat.ONNX,
  124. ):
  125. """Load a YOLOv7Face model exported by YOLOv7Face.
  126. :param model_file: (str)Path of model file, e.g ./yolov7face.onnx
  127. :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
  128. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  129. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  130. """
  131. super(YOLOv7Face, self).__init__(runtime_option)
  132. self._model = C.vision.facedet.YOLOv7Face(
  133. model_file, params_file, self._runtime_option, model_format
  134. )
  135. assert self.initialized, "YOLOv7Face initialize failed."
  136. def predict(self, input_image):
  137. """Detect the location and key points of human faces from an input image
  138. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  139. :return: FaceDetectionResult
  140. """
  141. return self._model.predict(input_image)
  142. def batch_predict(self, images):
  143. """Classify a batch of input image
  144. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  145. :return list of DetectionResult
  146. """
  147. return self._model.batch_predict(images)
  148. @property
  149. def preprocessor(self):
  150. """Get YOLOv7Preprocessor object of the loaded model
  151. :return YOLOv7Preprocessor
  152. """
  153. return self._model.preprocessor
  154. @property
  155. def postprocessor(self):
  156. """Get YOLOv7Postprocessor object of the loaded model
  157. :return YOLOv7Postprocessor
  158. """
  159. return self._model.postprocessor