retinaface.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /** \brief All object face detection model APIs are defined inside this
  21. * namespace
  22. *
  23. */
  24. namespace facedet {
  25. /*! @brief RetinaFace model object used when to load a RetinaFace model exported
  26. * by RetinaFace.
  27. */
  28. class ULTRAINFER_DECL RetinaFace : public UltraInferModel {
  29. public:
  30. /** \brief Set path of model file and the configuration of runtime.
  31. *
  32. * \param[in] model_file Path of model file, e.g ./retinaface.onnx
  33. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  34. * if the model format is ONNX, this parameter will be ignored \param[in]
  35. * custom_option RuntimeOption for inference, the default will use cpu, and
  36. * choose the backend defined in "valid_cpu_backends" \param[in] model_format
  37. * Model format of the loaded model, default is ONNX format
  38. */
  39. RetinaFace(const std::string &model_file, const std::string &params_file = "",
  40. const RuntimeOption &custom_option = RuntimeOption(),
  41. const ModelFormat &model_format = ModelFormat::ONNX);
  42. std::string ModelName() const { return "Pytorch_Retinaface"; }
  43. /** \brief Predict the face detection result for an input image
  44. *
  45. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  46. * with layout HWC, BGR format \param[in] result The output face detection
  47. * result will be written to this structure \param[in] conf_threshold
  48. * confidence threshold for postprocessing, default is 0.25 \param[in]
  49. * nms_iou_threshold iou threshold for NMS, default is 0.4 \return true if
  50. * the prediction succeeded, otherwise false
  51. */
  52. virtual bool Predict(cv::Mat *im, FaceDetectionResult *result,
  53. float conf_threshold = 0.25f,
  54. float nms_iou_threshold = 0.4f);
  55. /*! @brief
  56. Argument for image preprocessing step, tuple of (width, height), decide the
  57. target size after resize, default (640, 640)
  58. */
  59. std::vector<int> size;
  60. /*! @brief
  61. Argument for image postprocessing step, variance in RetinaFace's
  62. prior-box(anchor) generate process, default (0.1, 0.2)
  63. */
  64. std::vector<float> variance;
  65. /*! @brief
  66. Argument for image postprocessing step, downsample strides (namely, steps) for
  67. RetinaFace to generate anchors, will take (8,16,32) as default values
  68. */
  69. std::vector<int> downsample_strides;
  70. /*! @brief
  71. Argument for image postprocessing step, min sizes, width and height for each
  72. anchor, default min_sizes = {{16, 32}, {64, 128}, {256, 512}}
  73. */
  74. std::vector<std::vector<int>> min_sizes;
  75. /*! @brief
  76. Argument for image postprocessing step, landmarks_per_face, default 5 in
  77. RetinaFace
  78. */
  79. int landmarks_per_face;
  80. private:
  81. bool Initialize();
  82. bool Preprocess(Mat *mat, FDTensor *output,
  83. std::map<std::string, std::array<float, 2>> *im_info);
  84. bool Postprocess(std::vector<FDTensor> &infer_result,
  85. FaceDetectionResult *result,
  86. const std::map<std::string, std::array<float, 2>> &im_info,
  87. float conf_threshold, float nms_iou_threshold);
  88. bool IsDynamicInput() const { return is_dynamic_input_; }
  89. bool is_dynamic_input_;
  90. };
  91. } // namespace facedet
  92. } // namespace vision
  93. } // namespace ultra_infer