yolov5cls.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/classification/contrib/yolov5cls/postprocessor.h"
  17. #include "ultra_infer/vision/classification/contrib/yolov5cls/preprocessor.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace classification {
  21. /*! @brief YOLOv5Cls model object used when to load a YOLOv5Cls model exported
  22. * by YOLOv5Cls.
  23. */
  24. class ULTRAINFER_DECL YOLOv5Cls : 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 ./yolov5cls.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. YOLOv5Cls(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 "yolov5cls"; }
  39. /** \brief Predict the classification result for an input image
  40. *
  41. * \param[in] img The input image data, comes from cv::imread(), is a 3-D
  42. * array with layout HWC, BGR format \param[in] result The output
  43. * classification result will be written to this structure \return true if the
  44. * prediction successed, otherwise false
  45. */
  46. virtual bool Predict(const cv::Mat &img, ClassifyResult *result);
  47. /** \brief Predict the classification results for a batch of input images
  48. *
  49. * \param[in] imgs, The input image list, each element comes from cv::imread()
  50. * \param[in] results The output classification result list
  51. * \return true if the prediction successed, otherwise false
  52. */
  53. virtual bool BatchPredict(const std::vector<cv::Mat> &imgs,
  54. std::vector<ClassifyResult> *results);
  55. /// Get preprocessor reference of YOLOv5Cls
  56. virtual YOLOv5ClsPreprocessor &GetPreprocessor() { return preprocessor_; }
  57. /// Get postprocessor reference of YOLOv5Cls
  58. virtual YOLOv5ClsPostprocessor &GetPostprocessor() { return postprocessor_; }
  59. protected:
  60. bool Initialize();
  61. YOLOv5ClsPreprocessor preprocessor_;
  62. YOLOv5ClsPostprocessor postprocessor_;
  63. };
  64. } // namespace classification
  65. } // namespace vision
  66. } // namespace ultra_infer