inference.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. // Cpp native
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <random>
  7. // OpenCV / DNN / Inference
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/opencv.hpp>
  10. #include <opencv2/dnn.hpp>
  11. struct Detection
  12. {
  13. int class_id{0};
  14. std::string className{};
  15. float confidence{0.0};
  16. cv::Scalar color{};
  17. cv::Rect box{};
  18. };
  19. class Inference
  20. {
  21. public:
  22. Inference(const std::string &onnxModelPath, const cv::Size &modelInputShape = {640, 640}, const std::string &classesTxtFile = "", const bool &runWithCuda = false);
  23. std::vector<Detection> runInference(const cv::Mat &input);
  24. private:
  25. void loadClassesFromFile();
  26. void loadOnnxNetwork();
  27. cv::Mat formatToSquare(const cv::Mat &source);
  28. std::string modelPath{};
  29. std::string classesPath{};
  30. bool cudaEnabled{};
  31. std::vector<std::string> classes{"car", "wheel"};
  32. cv::Size2f modelShape{};
  33. float modelConfidenceThreshold {0.25};
  34. float modelScoreThreshold {0.45};
  35. float modelNMSThreshold {0.50};
  36. bool letterBoxForSquare = true;
  37. cv::dnn::Net net;
  38. };