pipnet.py 3.8 KB

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