pfld.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 PFLD(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 PFLD.
  27. :param model_file: (str)Path of model file, e.g pfld/pfld-106-v3.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(PFLD, self).__init__(runtime_option)
  33. assert (
  34. model_format == ModelFormat.ONNX
  35. ), "PFLD only support model format of ModelFormat.ONNX now."
  36. self._model = C.vision.facealign.PFLD(
  37. model_file, params_file, self._runtime_option, model_format
  38. )
  39. assert self.initialized, "PFLD 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 (112, 112)
  50. """
  51. return self._model.size
  52. @size.setter
  53. def size(self, wh):
  54. """
  55. Set the preprocess image size, default (112, 112)
  56. """
  57. assert isinstance(
  58. wh, (list, tuple)
  59. ), "The value to set `size` must be type of tuple or list."
  60. assert (
  61. len(wh) == 2
  62. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  63. len(wh)
  64. )
  65. self._model.size = wh