scrfd.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <unordered_map>
  19. namespace ultra_infer {
  20. namespace vision {
  21. namespace facedet {
  22. /*! @brief SCRFD model object used when to load a SCRFD model exported by SCRFD.
  23. */
  24. class ULTRAINFER_DECL SCRFD : 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 ./scrfd.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. SCRFD(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 "scrfd"; }
  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 \param[in] conf_threshold
  44. * confidence threshold for postprocessing, default is 0.25 \param[in]
  45. * nms_iou_threshold iou threshold for NMS, default is 0.4 \return true if
  46. * the prediction succeeded, otherwise false
  47. */
  48. virtual bool Predict(cv::Mat *im, FaceDetectionResult *result,
  49. float conf_threshold = 0.25f,
  50. float nms_iou_threshold = 0.4f);
  51. /*! @brief
  52. Argument for image preprocessing step, tuple of (width, height), decide the
  53. target size after resize, default (640, 640)
  54. */
  55. std::vector<int> size;
  56. // padding value, size should be the same as channels
  57. std::vector<float> padding_value;
  58. // only pad to the minimum rectangle which height and width is times of stride
  59. bool is_mini_pad;
  60. // while is_mini_pad = false and is_no_pad = true,
  61. // will resize the image to the set size
  62. bool is_no_pad;
  63. // if is_scale_up is false, the input image only can be zoom out,
  64. // the maximum resize scale cannot exceed 1.0
  65. bool is_scale_up;
  66. // padding stride, for is_mini_pad
  67. int stride;
  68. /*! @brief
  69. Argument for image postprocessing step, downsample strides (namely, steps) for
  70. SCRFD to generate anchors, will take (8,16,32) as default values
  71. */
  72. std::vector<int> downsample_strides;
  73. /*! @brief
  74. Argument for image postprocessing step, landmarks_per_face, default 5 in SCRFD
  75. */
  76. int landmarks_per_face;
  77. /*! @brief
  78. Argument for image postprocessing step, the outputs of onnx file with key
  79. points features or not, default true
  80. */
  81. bool use_kps;
  82. /*! @brief
  83. Argument for image postprocessing step, the upperbond number of boxes
  84. processed by nms, default 30000
  85. */
  86. int max_nms;
  87. /*! @brief
  88. Argument for image postprocessing step, anchor number of each stride, default
  89. 2
  90. */
  91. unsigned int num_anchors;
  92. /// This function will disable normalize and hwc2chw in preprocessing step.
  93. void DisableNormalize();
  94. /// This function will disable hwc2chw in preprocessing step.
  95. void DisablePermute();
  96. private:
  97. bool Initialize();
  98. bool Preprocess(Mat *mat, FDTensor *output,
  99. std::map<std::string, std::array<float, 2>> *im_info);
  100. bool Postprocess(std::vector<FDTensor> &infer_result,
  101. FaceDetectionResult *result,
  102. const std::map<std::string, std::array<float, 2>> &im_info,
  103. float conf_threshold, float nms_iou_threshold);
  104. void GeneratePoints();
  105. void LetterBox(Mat *mat, const std::vector<int> &size,
  106. const std::vector<float> &color, bool _auto,
  107. bool scale_fill = false, bool scale_up = true,
  108. int stride = 32);
  109. bool is_dynamic_input_;
  110. bool center_points_is_update_;
  111. typedef struct {
  112. float cx;
  113. float cy;
  114. } SCRFDPoint;
  115. std::unordered_map<int, std::vector<SCRFDPoint>> center_points_;
  116. // for recording the switch of normalize
  117. bool disable_normalize_ = false;
  118. // for recording the switch of hwc2chw
  119. bool disable_permute_ = false;
  120. };
  121. } // namespace facedet
  122. } // namespace vision
  123. } // namespace ultra_infer