yolov8_onnx.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <iostream>
  3. #include<memory>
  4. #include <opencv2/opencv.hpp>
  5. #include "yolov8_utils.h"
  6. #include<onnxruntime_cxx_api.h>
  7. //#include <tensorrt_provider_factory.h> //if use OrtTensorRTProviderOptionsV2
  8. //#include <onnxruntime_c_api.h>
  9. class Yolov8Onnx {
  10. public:
  11. Yolov8Onnx() :_OrtMemoryInfo(Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtDeviceAllocator, OrtMemType::OrtMemTypeCPUOutput)) {};
  12. ~Yolov8Onnx() {
  13. if (_OrtSession != nullptr)
  14. delete _OrtSession;
  15. };// delete _OrtMemoryInfo;
  16. public:
  17. /** \brief Read onnx-model
  18. * \param[in] modelPath:onnx-model path
  19. * \param[in] isCuda:if true,use Ort-GPU,else run it on cpu.
  20. * \param[in] cudaID:if isCuda==true,run Ort-GPU on cudaID.
  21. * \param[in] warmUp:if isCuda==true,warm up GPU-model.
  22. */
  23. bool ReadModel(const std::string& modelPath, bool isCuda = false, int cudaID = 0, bool warmUp = true);
  24. /** \brief detect.
  25. * \param[in] srcImg:a 3-channels image.
  26. * \param[out] output:detection results of input image.
  27. */
  28. bool OnnxDetect(cv::Mat& srcImg, std::vector<OutputParams>& output);
  29. /** \brief detect,batch size= _batchSize
  30. * \param[in] srcImg:A batch of images.
  31. * \param[out] output:detection results of input images.
  32. */
  33. bool OnnxBatchDetect(std::vector<cv::Mat>& srcImg, std::vector<std::vector<OutputParams>>& output);
  34. private:
  35. template <typename T>
  36. T VectorProduct(const std::vector<T>& v)
  37. {
  38. return std::accumulate(v.begin(), v.end(), 1, std::multiplies<T>());
  39. };
  40. int Preprocessing(const std::vector<cv::Mat>& srcImgs, std::vector<cv::Mat>& outSrcImgs, std::vector<cv::Vec4d>& params);
  41. const int _netWidth = 640; //ONNX-net-input-width
  42. const int _netHeight = 640; //ONNX-net-input-height
  43. int _batchSize = 1; //if multi-batch,set this
  44. bool _isDynamicShape = false;//onnx support dynamic shape
  45. float _classThreshold = 0.55;
  46. float _nmsThreshold = 0.45;
  47. float _maskThreshold = 0.5;
  48. //ONNXRUNTIME
  49. Ort::Env _OrtEnv = Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_ERROR, "Yolov8");
  50. Ort::SessionOptions _OrtSessionOptions = Ort::SessionOptions();
  51. Ort::Session* _OrtSession = nullptr;
  52. Ort::MemoryInfo _OrtMemoryInfo;
  53. #if ORT_API_VERSION < ORT_OLD_VISON
  54. char* _inputName, * _output_name0;
  55. #else
  56. std::shared_ptr<char> _inputName, _output_name0;
  57. #endif
  58. std::vector<char*> _inputNodeNames; //输入节点名
  59. std::vector<char*> _outputNodeNames;//输出节点名
  60. size_t _inputNodesNum = 0; //输入节点数
  61. size_t _outputNodesNum = 0; //输出节点数
  62. ONNXTensorElementDataType _inputNodeDataType; //数据类型
  63. ONNXTensorElementDataType _outputNodeDataType;
  64. std::vector<int64_t> _inputTensorShape; //输入张量shape
  65. std::vector<int64_t> _outputTensorShape;
  66. public:
  67. std::vector<std::string> _className = {
  68. "earAxis", "auxHook", "mainHook"
  69. };
  70. };