yolov5.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
  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/detection/contrib/yolov5/postprocessor.h"
  17. #include "ultra_infer/vision/detection/contrib/yolov5/preprocessor.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace detection {
  21. /*! @brief YOLOv5 model object used when to load a YOLOv5 model exported by
  22. * YOLOv5.
  23. */
  24. class ULTRAINFER_DECL YOLOv5 : 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 ./yolov5.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. YOLOv5(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 "yolov5"; }
  39. /** \brief DEPRECATED Predict the detection result for an input image, remove
  40. * at 1.0 version
  41. *
  42. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  43. * with layout HWC, BGR format \param[in] result The output detection result
  44. * will be written to this structure \param[in] conf_threshold confidence
  45. * threshold for postprocessing, default is 0.25 \param[in] nms_threshold iou
  46. * threshold for NMS, default is 0.5 \return true if the prediction
  47. * successed, otherwise false
  48. */
  49. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  50. float conf_threshold = 0.25, float nms_threshold = 0.5);
  51. /** \brief Predict the detection result for an input image
  52. *
  53. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  54. * array with layout HWC, BGR format \param[in] result The output detection
  55. * result will be written to this structure \return true if the prediction
  56. * successed, otherwise false
  57. */
  58. virtual bool Predict(const cv::Mat &img, DetectionResult *result);
  59. /** \brief Predict the detection results for a batch of input images
  60. *
  61. * \param[in] imgs, The input image list, each element comes from cv::imread()
  62. * \param[in] results The output detection result list
  63. * \return true if the prediction successed, otherwise false
  64. */
  65. virtual bool BatchPredict(const std::vector<cv::Mat> &imgs,
  66. std::vector<DetectionResult> *results);
  67. /// Get preprocessor reference of YOLOv5
  68. virtual YOLOv5Preprocessor &GetPreprocessor() { return preprocessor_; }
  69. /// Get postprocessor reference of YOLOv5
  70. virtual YOLOv5Postprocessor &GetPostprocessor() { return postprocessor_; }
  71. protected:
  72. bool Initialize();
  73. YOLOv5Preprocessor preprocessor_;
  74. YOLOv5Postprocessor postprocessor_;
  75. };
  76. } // namespace detection
  77. } // namespace vision
  78. } // namespace ultra_infer