YoloFeatureManager.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 
  2. #pragma once
  3. #include <opencv2/opencv.hpp>
  4. #include <opencv2/dnn.hpp>
  5. #include <vector>
  6. #include <string>
  7. class YoloFeatureManager
  8. {
  9. private:
  10. cv::dnn::Net net;
  11. int inputWidth;
  12. int inputHeight;
  13. float CONF_THRESHOLD;
  14. float NMS_THRESHOLD;
  15. int FRUIT_VEGETABLE_COUNT;
  16. public:
  17. YoloFeatureManager();
  18. ~YoloFeatureManager() = default;
  19. // 重新加载模型
  20. void loadModel(const std::string & modelPath);
  21. // 提取特征
  22. std::vector<float> extractFeatures(const std::string & imagePath);
  23. // 执行探测
  24. void Detection(const std::string& imagePath);
  25. // 绘制探测结果
  26. void YoloFeatureManager::drawDetection(cv::Mat& img, const std::vector<cv::Rect>& boxes, const std::vector<int>& classIds,
  27. const std::vector<float>& confidences);
  28. //根据摄像头读取的帧,识别出对应类别的name(英文的)
  29. std::string Class(cv::Mat& image);
  30. std::string ClassFromVideoCapture();
  31. private:
  32. // 寻找置信度最高的类别
  33. int getTopClass(const cv::Mat & output)
  34. {
  35. // 将输出展平为一维数组
  36. cv::Mat flatOutput = output.reshape(1, 1);
  37. double maxVal;
  38. cv::Point maxLoc;
  39. // 找到最大值的位置(即最高置信度类别索引)
  40. cv::minMaxLoc(flatOutput, nullptr, &maxVal, nullptr, &maxLoc);
  41. return maxLoc.x;
  42. }
  43. // 获取类别名称
  44. std::string getClassName(std::size_t classId) const;
  45. };