ultraface.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 facedet {
  21. /*! @brief UltraFace model object used when to load a UltraFace model exported
  22. * by UltraFace.
  23. */
  24. class ULTRAINFER_DECL UltraFace : 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 ./ultraface.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. UltraFace(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 {
  39. return "Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB";
  40. }
  41. /** \brief Predict the face detection result for an input image
  42. *
  43. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  44. * with layout HWC, BGR format \param[in] result The output face detection
  45. * result will be written to this structure \param[in] conf_threshold
  46. * confidence threshold for postprocessing, default is 0.7 \param[in]
  47. * nms_iou_threshold iou threshold for NMS, default is 0.3 \return true if
  48. * the prediction succeeded, otherwise false
  49. */
  50. virtual bool Predict(cv::Mat *im, FaceDetectionResult *result,
  51. float conf_threshold = 0.7f,
  52. float nms_iou_threshold = 0.3f);
  53. /*! @brief
  54. Argument for image preprocessing step, tuple of (width, height), decide the
  55. target size after resize, default (320, 240)
  56. */
  57. std::vector<int> size;
  58. private:
  59. bool Initialize();
  60. bool Preprocess(Mat *mat, FDTensor *outputs,
  61. std::map<std::string, std::array<float, 2>> *im_info);
  62. bool Postprocess(std::vector<FDTensor> &infer_result,
  63. FaceDetectionResult *result,
  64. const std::map<std::string, std::array<float, 2>> &im_info,
  65. float conf_threshold, float nms_iou_threshold);
  66. bool IsDynamicInput() const { return is_dynamic_input_; }
  67. bool is_dynamic_input_;
  68. };
  69. } // namespace facedet
  70. } // namespace vision
  71. } // namespace ultra_infer