pipnet.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 PIPNet(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 face alignment model exported by PIPNet.
  26. :param model_file: (str)Path of model file, e.g ./PIPNet.onnx
  27. :param params_file: (str)Path of parameters file, 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, default is ONNX
  30. """
  31. super(PIPNet, self).__init__(runtime_option)
  32. assert (
  33. model_format == ModelFormat.ONNX
  34. ), "PIPNet only support model format of ModelFormat.ONNX now."
  35. self._model = C.vision.facealign.PIPNet(
  36. model_file, params_file, self._runtime_option, model_format
  37. )
  38. assert self.initialized, "PIPNet initialize failed."
  39. def predict(self, input_image):
  40. """Detect an input image landmarks
  41. :param im: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  42. :return: FaceAlignmentResult
  43. """
  44. return self._model.predict(input_image)
  45. @property
  46. def size(self):
  47. """
  48. Returns the preprocess image size, default (256, 256)
  49. """
  50. return self._model.size
  51. @property
  52. def mean_vals(self):
  53. """
  54. Returns the mean value of normalization, default mean_vals = [0.485f, 0.456f, 0.406f];
  55. """
  56. return self._model.mean_vals
  57. @property
  58. def std_vals(self):
  59. """
  60. Returns the std value of normalization, default std_vals = [0.229f, 0.224f, 0.225f];
  61. """
  62. return self._model.std_vals
  63. @property
  64. def num_landmarks(self):
  65. """
  66. Returns the number of landmarks
  67. """
  68. return self._model.num_landmarks
  69. @size.setter
  70. def size(self, wh):
  71. """
  72. Set the preprocess image size, default (256, 256)
  73. """
  74. assert isinstance(
  75. wh, (list, tuple)
  76. ), "The value to set `size` must be type of tuple or list."
  77. assert (
  78. len(wh) == 2
  79. ), "The value to set `size` must contains 2 elements means [width, height], but now it contains {} elements.".format(
  80. len(wh)
  81. )
  82. self._model.size = wh
  83. @mean_vals.setter
  84. def mean_vals(self, value):
  85. assert isinstance(
  86. value, list
  87. ), "The value to set `mean_vals` must be type of list."
  88. self._model.mean_vals = value
  89. @std_vals.setter
  90. def std_vals(self, value):
  91. assert isinstance(
  92. value, list
  93. ), "The value to set `std_vals` must be type of list."
  94. self._model.std_vals = value
  95. @num_landmarks.setter
  96. def num_landmarks(self, value):
  97. assert isinstance(
  98. value, int
  99. ), "The value to set `std_vals` must be type of int."
  100. self._model.num_landmarks = value