pipnet.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2022 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. #pragma once
  15. #include "ultra_infer/ultra_infer_model.h"
  16. #include "ultra_infer/vision/common/processors/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace facealign {
  21. /*! @brief PIPNet model object used when to load a PIPNet model exported by
  22. * PIPNet.
  23. */
  24. class ULTRAINFER_DECL PIPNet : public UltraInferModel {
  25. public:
  26. /** \brief Set path of model file and the configuration of runtime.
  27. *
  28. * \param[in] model_file Path of model file, e.g ./pipnet.onnx
  29. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  30. * if the model format is ONNX, this parameter will be ignored \param[in]
  31. * custom_option RuntimeOption for inference, the default will use cpu, and
  32. * choose the backend defined in "valid_cpu_backends" \param[in] model_format
  33. * Model format of the loaded model, default is ONNX format
  34. */
  35. PIPNet(const std::string &model_file, const std::string &params_file = "",
  36. const RuntimeOption &custom_option = RuntimeOption(),
  37. const ModelFormat &model_format = ModelFormat::ONNX);
  38. std::string ModelName() const { return "PIPNet"; }
  39. /** \brief Predict the face detection result for an input image
  40. *
  41. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  42. * with layout HWC, BGR format \param[in] result The output face detection
  43. * result will be written to this structure \return true if the prediction
  44. * succeeded, otherwise false
  45. */
  46. virtual bool Predict(cv::Mat *im, FaceAlignmentResult *result);
  47. /** \brief Get the number of landmakrs
  48. *
  49. * \return Integer type, default num_landmarks = 19
  50. */
  51. int GetNumLandmarks() { return num_landmarks_; }
  52. /** \brief Get the mean values for normalization
  53. *
  54. * \return Vector of float values, default mean_vals = {0.485f, 0.456f,
  55. * 0.406f}
  56. */
  57. std::vector<float> GetMeanVals() { return mean_vals_; }
  58. /** \brief Get the std values for normalization
  59. *
  60. * \return Vector of float values, default std_vals = {0.229f, 0.224f, 0.225f}
  61. */
  62. std::vector<float> GetStdVals() { return std_vals_; }
  63. /** \brief Get the input size of image
  64. *
  65. * \return Vector of int values, default {256, 256}
  66. */
  67. std::vector<int> GetSize() { return size_; }
  68. /** \brief Set the number of landmarks
  69. *
  70. * \param[in] num_landmarks Integer value which represents number of landmarks
  71. */
  72. void SetNumLandmarks(const int &num_landmarks);
  73. /** \brief Set the mean values for normalization
  74. *
  75. * \param[in] mean_vals Vector of float values whose length is equal to 3
  76. */
  77. void SetMeanVals(const std::vector<float> &mean_vals) {
  78. mean_vals_ = mean_vals;
  79. }
  80. /** \brief Set the std values for normalization
  81. *
  82. * \param[in] std_vals Vector of float values whose length is equal to 3
  83. */
  84. void SetStdVals(const std::vector<float> &std_vals) { std_vals_ = std_vals; }
  85. /** \brief Set the input size of image
  86. *
  87. * \param[in] size Vector of int values which represents {width, height} of
  88. * image
  89. */
  90. void SetSize(const std::vector<int> &size) { size_ = size; }
  91. private:
  92. bool Initialize();
  93. bool Preprocess(Mat *mat, FDTensor *outputs,
  94. std::map<std::string, std::array<int, 2>> *im_info);
  95. bool Postprocess(std::vector<FDTensor> &infer_result,
  96. FaceAlignmentResult *result,
  97. const std::map<std::string, std::array<int, 2>> &im_info);
  98. void GenerateLandmarks(std::vector<FDTensor> &infer_result,
  99. FaceAlignmentResult *result, float img_height,
  100. float img_width);
  101. std::map<int, int> num_lms_map_;
  102. std::map<int, int> max_len_map_;
  103. std::map<int, std::vector<int>> reverse_index1_map_;
  104. std::map<int, std::vector<int>> reverse_index2_map_;
  105. int num_nb_;
  106. int net_stride_;
  107. // Now PIPNet support num_landmarks in {19, 29, 68, 98}
  108. std::vector<int> supported_num_landmarks_;
  109. // tuple of (width, height), default (256, 256)
  110. std::vector<int> size_;
  111. // Mean parameters for normalize, size should be the the same as channels,
  112. // default mean_vals = {0.485f, 0.456f, 0.406f}
  113. std::vector<float> mean_vals_;
  114. // Std parameters for normalize, size should be the the same as channels,
  115. // default std_vals = {0.229f, 0.224f, 0.225f}
  116. std::vector<float> std_vals_;
  117. // number of landmarks
  118. int num_landmarks_;
  119. };
  120. } // namespace facealign
  121. } // namespace vision
  122. } // namespace ultra_infer