#pragma once #include #include #include #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 GetStateResult(cv::Mat img,std::vector rects) = 0; // virtual vector GetDigitResult(cv::Mat img,std::vector 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 result,vector 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()); } } } };