FastDeploy  latest
Fast & Easy to Deploy!
scrfd.h
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 
15 #pragma once
16 #include <unordered_map>
17 #include "fastdeploy/fastdeploy_model.h"
18 #include "fastdeploy/vision/common/processors/transform.h"
19 #include "fastdeploy/vision/common/result.h"
20 
21 namespace fastdeploy {
22 
23 namespace vision {
24 
25 namespace facedet {
28 class FASTDEPLOY_DECL SCRFD : public FastDeployModel {
29  public:
37  SCRFD(const std::string& model_file, const std::string& params_file = "",
38  const RuntimeOption& custom_option = RuntimeOption(),
39  const ModelFormat& model_format = ModelFormat::ONNX);
40 
41  std::string ModelName() const { return "scrfd"; }
50  virtual bool Predict(cv::Mat* im, FaceDetectionResult* result,
51  float conf_threshold = 0.25f,
52  float nms_iou_threshold = 0.4f);
53 
57  std::vector<int> size;
58  // padding value, size should be the same as channels
59 
60  std::vector<float> padding_value;
61  // only pad to the minimum rectange which height and width is times of stride
62  bool is_mini_pad;
63  // while is_mini_pad = false and is_no_pad = true,
64  // will resize the image to the set size
65  bool is_no_pad;
66  // if is_scale_up is false, the input image only can be zoom out,
67  // the maximum resize scale cannot exceed 1.0
68  bool is_scale_up;
69  // padding stride, for is_mini_pad
70  int stride;
74  std::vector<int> downsample_strides;
82  bool use_kps;
86  int max_nms;
90  unsigned int num_anchors;
91 
93  void DisableNormalize();
94 
96  void DisablePermute();
97  private:
98  bool Initialize();
99 
100  bool Preprocess(Mat* mat, FDTensor* output,
101  std::map<std::string, std::array<float, 2>>* im_info);
102 
103  bool Postprocess(std::vector<FDTensor>& infer_result,
104  FaceDetectionResult* result,
105  const std::map<std::string, std::array<float, 2>>& im_info,
106  float conf_threshold, float nms_iou_threshold);
107 
108  void GeneratePoints();
109 
110  void LetterBox(Mat* mat, const std::vector<int>& size,
111  const std::vector<float>& color, bool _auto,
112  bool scale_fill = false, bool scale_up = true,
113  int stride = 32);
114 
115  bool is_dynamic_input_;
116 
117  bool center_points_is_update_;
118 
119  typedef struct {
120  float cx;
121  float cy;
122  } SCRFDPoint;
123 
124  std::unordered_map<int, std::vector<SCRFDPoint>> center_points_;
125 
126  // for recording the switch of normalize
127  bool disable_normalize_ = false;
128  // for recording the switch of hwc2chw
129  bool disable_permute_ = false;
130 };
131 } // namespace facedet
132 } // namespace vision
133 } // namespace fastdeploy
Option object used when create a new Runtime object.
Definition: runtime_option.h:40
Base model object for all the vision models.
Definition: fastdeploy_model.h:21
std::vector< int > size
Argument for image preprocessing step, tuple of (width, height), decide the target size after resize...
Definition: scrfd.h:57
bool use_kps
Argument for image postprocessing step, the outputs of onnx file with key points features or not...
Definition: scrfd.h:82
ModelFormat
Definition: enum_variables.h:67
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
int landmarks_per_face
Argument for image postprocessing step, landmarks_per_face, default 5 in SCRFD.
Definition: scrfd.h:78
int max_nms
Argument for image postprocessing step, the upperbond number of boxes processed by nms...
Definition: scrfd.h:86
SCRFD model object used when to load a SCRFD model exported by SCRFD.
Definition: scrfd.h:28
Face detection result structure for all the face detection models.
Definition: result.h:212
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
std::vector< int > downsample_strides
Argument for image postprocessing step, downsample strides (namely, steps) for SCRFD to generate anch...
Definition: scrfd.h:74
Model with ONNX format.
Definition: enum_variables.h:70
std::string ModelName() const
Get model&#39;s name.
Definition: scrfd.h:41
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16
unsigned int num_anchors
Argument for image postprocessing step, anchor number of each stride, default 2.
Definition: scrfd.h:90