| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
-
- #pragma once
- #include <opencv2/opencv.hpp>
- #include <opencv2/dnn.hpp>
- #include <vector>
- #include <string>
- class YoloFeatureManager
- {
- private:
- cv::dnn::Net net;
- int inputWidth;
- int inputHeight;
- float CONF_THRESHOLD;
- float NMS_THRESHOLD;
- int FRUIT_VEGETABLE_COUNT;
- public:
- YoloFeatureManager();
- ~YoloFeatureManager() = default;
- // 重新加载模型
- void loadModel(const std::string & modelPath);
- // 提取特征
- std::vector<float> extractFeatures(const std::string & imagePath);
- // 执行探测
- void Detection(const std::string& imagePath);
- // 绘制探测结果
- void YoloFeatureManager::drawDetection(cv::Mat& img, const std::vector<cv::Rect>& boxes, const std::vector<int>& classIds,
- const std::vector<float>& confidences);
- //根据摄像头读取的帧,识别出对应类别的name(英文的)
- std::string Class(cv::Mat& image);
- std::string ClassFromVideoCapture();
- private:
- // 寻找置信度最高的类别
- int getTopClass(const cv::Mat & output)
- {
- // 将输出展平为一维数组
- cv::Mat flatOutput = output.reshape(1, 1);
- double maxVal;
- cv::Point maxLoc;
- // 找到最大值的位置(即最高置信度类别索引)
- cv::minMaxLoc(flatOutput, nullptr, &maxVal, nullptr, &maxLoc);
- return maxLoc.x;
- }
- // 获取类别名称
- std::string getClassName(std::size_t classId) const;
- };
|