123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #pragma once
- #include<iostream>
- #include <vector>
- #include<opencv2/opencv.hpp>
- #include "YunDaISASImageRecognitionService.h"
- using namespace std;
- class IDetection {
- public:
- struct Output {
- Output(int id, float confidence, cv::Rect box)
- {
- m_id = id;
- m_confidence = confidence;
- m_box = box;
- }
- /*Output()
- {
- }*/
- public:
- int m_id; //结果类别id
- float m_confidence; //结果置信度
- cv::Rect m_box; //矩形框
- };
- public:
- struct DectectResult {
- // DectectResult(float confidence, float dValue, string sValue/*, string sUnit,string sName*/ )
- // {
- // m_confidence = confidence;
- // m_dValue = dValue;
- // m_sValue = sValue;
- ///* m_sUnit = sUnit;
- // m_sName = sName;*/
- // }
- DectectResult(float confidence, float dValue, string sValue)
- {
- m_confidence = confidence;
- m_dValue = dValue;
- m_sValue = sValue;
- }
- DectectResult()
- {
- }
- float m_confidence =0; //结果置信度
- float m_dValue =0;
- string m_sValue="";
- //string m_sUnit=""; //单位 例如℃
- //string m_sName=""; //名称
- };
- //public:
- // virtual vector<DectectResult> GetStateResult(cv::Mat img,std::vector<cv::Rect> rects) = 0;
- // virtual vector<DectectResult> GetDigitResult(cv::Mat img,std::vector<cv::Rect> rects) = 0;
- public:
- #if(defined YOLO_P6 && YOLO_P6==true)
- const float netAnchors[4][6] = { { 19,27, 44,40, 38,94 },{ 96,68, 86,152, 180,137 },{ 140,301, 303,264, 238,542 },{ 436,615, 739,380, 925,792 } };
- const int netWidth = 1280; //ONNX图片输入宽度
- const int netHeight = 1280; //ONNX图片输入高度
- const int strideSize = 4; //stride size
- #else
- const float netAnchors[3][6] = { { 10,13, 16,30, 33,23 },{ 30,61, 62,45, 59,119 },{ 116,90, 156,198, 373,326 } };
- const int netWidth = 640; //ONNX图片输入宽度
- const int netHeight = 640; //ONNX图片输入高度
- const int strideSize = 3; //stride size
- #endif // YOLO_P6
- const float netStride[4] = { 8, 16.0,32,64 };
- float boxThreshold = 0.25;
- float classThreshold = 0.25;
- float nmsThreshold = 0.45;
- float nmsScoreThreshold = boxThreshold * classThreshold;
- public:
- static void DrawPred(cv::Mat& img, vector<Output> result,vector<std::string> className)
- {
- for (int i = 0; i < result.size(); i++) {
- try
- {
- int left = result[i].m_box.x;
- int top = result[i].m_box.y;
- int width = result[i].m_box.width;
- int height = result[i].m_box.height;
- int baseLine;
- //2022.10.17
- cv::Rect box = result[i].m_box;
- cv::Mat input_image_copy = img.clone();
- cv::Mat cutMat = input_image_copy(box);
- string label = className[result[i].m_id] + ": " + to_string(result[i].m_confidence);
- cv::Size labelSize = getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
- top = max(top, labelSize.height);
- cv::rectangle(img, cv::Point(left, top - int(2 * labelSize.height)), cv::Point(left + int(2 * labelSize.width), top + baseLine), cv::Scalar(0, 0, 255), cv::FILLED);
- cv::rectangle(img, cv::Point(left, top), cv::Point(left + width, top + height), cv::Scalar(255, 255, 0), 1);
- cv::putText(img, label, cv::Point(left, top), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 0), 1);
-
- }
- catch (const std::exception& ex)
- {
- YunDaISASImageRecognitionService::ConsoleLog(ex.what());
- }
- }
- }
-
- };
-
|