#include "JdydAlgorithnm.h" static ClassifyDectect classifyDectect; static DisconnectorDectect disconnectorDectect; static OilLevelDectect oilLevelDectect; static LightDectect lightDectect; static AirSwitchDectect airSwitchDectect; static ChangeoverSwitchDectect changeoverSwitchDectect; static IndoorDisconnectorDectect indoorDisconnectorDectect; static TriangleDisconnectorDectect triangleDisconnectorDectect; static MeterDectect meterDectect; static CharacterDectect characterDectect; static DisconnectorStateDectect disconnectorStateDectect; static ElectronPlateDectect electronPlateDectect; static NumberDectect numberDectect; static InstructionsDectect instructionsDectect; static ContactDectect contactDectect; static OperatingHandleStateDectect operatingHandleStateDectect; static PressplateDectect pressplateDectect; static PersonBehaviorDetect personBehaviorDetect; bool JdydAlgorithnm::Init() { try { bool isCuda = false; QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); classifyDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("分类模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); disconnectorDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("隔离开关模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); operatingHandleStateDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("操作手柄模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); personBehaviorDetect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("安全防范模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); pressplateDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("压板模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); oilLevelDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("油位模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); lightDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("信号灯模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); airSwitchDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("空开模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); changeoverSwitchDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("旋转开关模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); indoorDisconnectorDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("竖直隔离开关模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); triangleDisconnectorDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("三角隔离开关模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); meterDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("仪表模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); characterDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("字符型模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); disconnectorStateDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("隔离开关室内状态模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); electronPlateDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("电子数字表盘模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); numberDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("数字识别模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); instructionsDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("标识识别模型")); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); contactDectect.Init(isCuda); YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("连接点识别模型")); } catch (const std::exception& ex) { YunDaISASImageRecognitionService::ConsoleLog(ex.what()); } return true; } json JdydAlgorithnm::CallAlgorithnm(string pluginName, string filePath, string modelPath, int* roiVec, string pcArg) { cv::Mat img = cv::imread(filePath); int imgWidth = (roiVec[4] - roiVec[0])<1 || (roiVec[4] - roiVec[0]) > img.cols ? img.cols: (roiVec[4] - roiVec[0]); int imgheight =(roiVec[5] - roiVec[1])<1 || (roiVec[5] - roiVec[1]) > img.rows ? img.rows : (roiVec[5] - roiVec[1]); int topX = roiVec[0] <0 || roiVec[0]> img.cols ? 0: roiVec[0]; int topY = roiVec[1] < 0 || roiVec[1]> img.rows ? 0 : roiVec[1]; if (topX+ imgWidth> img.cols) { topX = 0; imgWidth = img.cols; } if (topY+ imgheight> img.rows) { topY = 0; imgheight = img.rows; } cv::Rect rect(topX, topY, imgWidth, imgheight); cv::Mat ROI = img(rect); string tempPath = "test.png"; vector result; if (classifyDectect.Detect(ROI, result)) { if (YunDaISASImageRecognitionService::GetIsShowDectect()) { cv::Mat drawROI; ROI.copyTo(drawROI); classifyDectect.drawPred(drawROI, result); imwrite("test.png", drawROI); YunDaISASImageRecognitionService::SetImage(QString::fromStdString(tempPath)); } } string rstStr = ""; string rstValue = ""; int flag = -1; vector rstStrArr; vector> rstRoiArr; if (1 <= result.size()) { float confidenceMax = 0; int confidenceMaxId = 0; int closestCenterPoint = ROI.cols + ROI.rows; int centerX = ROI.cols / 2; int centerY = ROI.rows / 2; int maxCenterBoxId = 0; float instructionsConfidenceMax = 0; int instructionsConfidenceMaxId = -1; float pluginNameConfidenceMax = 0; int pluginNameId = -1; for (size_t i = 0; i < result.size(); i++) { int absX = cv::abs((result[i].box.x + result[i].box.width / 2) - centerX); int absY = cv::abs((result[i].box.y + result[i].box.height / 2) - centerY); if (classifyDectect.className[result[i].id] == classifyDectect.instructionsName) { if (result[i].confidence> instructionsConfidenceMax) { instructionsConfidenceMax = result[i].confidence; instructionsConfidenceMaxId = i; } } else if (pluginName.size()>0 && classifyDectect.className[result[i].id] == pluginName) { if (result[i].confidence > pluginNameConfidenceMax) { pluginNameConfidenceMax = result[i].confidence; pluginNameId = i; } } else { if (result[i].confidence > confidenceMax) { confidenceMax = result[i].confidence; confidenceMaxId = i; } if (absX + absY < closestCenterPoint) { closestCenterPoint = absX + absY; maxCenterBoxId = i; } } vector vecRec; vecRec.push_back(result[i].box.x); vecRec.push_back(result[i].box.y); vecRec.push_back(result[i].box.width); vecRec.push_back(result[i].box.height); rstRoiArr.push_back(vecRec); rstStrArr.push_back(classifyDectect.className[result[i].id]); } /*if (!YunDaISASImageRecognitionService::GetReconginzeStratgy()) { maxCenterBoxId = confidenceMaxId; }*/ string instructionsDectectResultStr = ""; if (instructionsConfidenceMaxId!=-1) { IDetection::DectectResult dectectResult = instructionsDectect.GetStateResult(ROI, result[instructionsConfidenceMaxId].box); instructionsDectectResultStr = dectectResult.m_sValue; } /* string tempResult = ""; string tempResultValue = "";*/ GetMaxConfidenceRecoResult(rstStr, rstValue, instructionsDectectResultStr,result, maxCenterBoxId, pluginNameId,ROI); /* rstStr = tempResult; rstValue = tempResultValue;*/ } else { if (pluginName.size() > 0) { for (size_t i = 0; i < classifyDectect.className.size(); i++) { if (classifyDectect.className[i] == pluginName) { Output pluginNameOutput; pluginNameOutput.box.x = 0; pluginNameOutput.box.y = 0; pluginNameOutput.box.width = ROI.cols; pluginNameOutput.box.height = ROI.rows; pluginNameOutput.id = i; pluginNameOutput.confidence = 0.99; result.push_back(pluginNameOutput); int pluginNameId = 0; string instructionsDectectResultStr = ""; int maxCenterBoxId = 0; GetMaxConfidenceRecoResult(rstStr, rstValue, instructionsDectectResultStr, result, maxCenterBoxId, pluginNameId, ROI); } } } } if (rstStr.size()>300) { rstStr = ""; } json resultJson = { {"flag", flag}, {"result" , rstStr}, {"resultValue" , rstValue},{"results" , rstStrArr} ,{"resultRois",rstRoiArr}}; return resultJson; } /// /// 获取 /// /// /// /// /// /// /// /// void JdydAlgorithnm::GetMaxConfidenceRecoResult(string &tempResult, string &tempResultValue, const string instructionsDectectResultStr,vector result, int maxCenterBoxId,const int pluginNameId,const cv::Mat ROI) { if (pluginNameId > -1) { maxCenterBoxId = pluginNameId; } cv::Rect boxTemp = result[maxCenterBoxId].box; if (boxTemp.x<0 || boxTemp.y < 0 || boxTemp.x + boxTemp.width>ROI.cols || boxTemp.y + boxTemp.height>ROI.rows) { boxTemp.x = 0; boxTemp.y = 0; boxTemp.width = ROI.cols; boxTemp.height = ROI.rows; } result[maxCenterBoxId].box = boxTemp; if (classifyDectect.className[result[maxCenterBoxId].id] == "disconnector")//隔离开关 { IDetection::DectectResult dectectResultVec = disconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "oil_level")//油位 { IDetection::DectectResult dectectResultVec = oilLevelDectect.GetDigitResult(ROI, result[maxCenterBoxId].box); if (dectectResultVec.m_sValue != "") { tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else { tempResult = classifyDectect.className[result[maxCenterBoxId].id]; } tempResultValue = std::to_string(dectectResultVec.m_dValue); } else if (classifyDectect.className[result[maxCenterBoxId].id] == "meter")//指针表盘 { auto dectectResults = meterDectect.GetDigitResults(ROI, result[maxCenterBoxId].box); if (dectectResults.size() > 0) { for (size_t i = 0; i < dectectResults.size(); i++) { string tempResultItem = ""; string tempResultValueItem = ""; if (dectectResults[i].m_sValue != "") { tempResultItem = "meter_" + dectectResults[i].m_sValue; } else { tempResultItem = classifyDectect.className[result[maxCenterBoxId].id]; } tempResultValueItem = std::to_string(dectectResults[i].m_dValue); if (tempResultValueItem != "" && tempResultItem != "") { if (tempResult == "") { tempResult += tempResultItem; tempResultValue += tempResultValueItem; } else { tempResult += ";"; tempResult += tempResultItem; tempResultValue += ";"; tempResultValue += tempResultValueItem; } } } } else { tempResult = "meter_" + meterDectect.GetDigitResult(ROI, result[maxCenterBoxId].box).m_sValue; } } else if (classifyDectect.className[result[maxCenterBoxId].id] == "changeover_switch") { IDetection::DectectResult dectectResultVec = changeoverSwitchDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "pressplate") { IDetection::DectectResult dectectResultVec = pressplateDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "disconnector_state") { IDetection::DectectResult dectectResultVec = disconnectorStateDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "light") { IDetection::DectectResult dectectResultVec = lightDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "air_switch") { IDetection::DectectResult dectectResultVec = airSwitchDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "character") { IDetection::DectectResult dectectResultVec = characterDectect.GetStateResult(ROI, result[maxCenterBoxId].box); if (characterDectect.GetStateResults().size() > 0) { string tempResultItem = ""; for (size_t i = 0; i < characterDectect.GetStateResults().size(); i++) { if (tempResultItem == "") { tempResultItem += "character_" + characterDectect.GetStateResults()[i].m_sValue; } else { tempResultItem += ";"; tempResultItem += "character_" + characterDectect.GetStateResults()[i].m_sValue; } } tempResult = tempResultItem; } else { tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } } else if (classifyDectect.className[result[maxCenterBoxId].id] == "indoor_disconnector") { IDetection::DectectResult dectectResultVec = indoorDisconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "triangle_disconnector") { IDetection::DectectResult dectectResultVec = triangleDisconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "electron_plate")//电子数字盘 { auto dectectResults = electronPlateDectect.GetDigitResults(ROI, result[maxCenterBoxId].box); if (dectectResults.size() > 0) { for (size_t i = 0; i < dectectResults.size(); i++) { string tempResultItem = ""; string tempResultValueItem = ""; if (dectectResults[i].m_sValue != "digit") { tempResultItem = "electron_plate_" + dectectResults[i].m_sValue; //tempResult = tempResultItem; } else { tempResultItem = "electron_plate_" + dectectResults[i].m_sValue; tempResultValueItem = std::to_string(dectectResults[i].m_dValue); } if (tempResultItem != "") { if (tempResult == "") { tempResult += tempResultItem; tempResultValue += tempResultValueItem; } else { tempResult += ";"; tempResult += tempResultItem; tempResultValue += ";"; tempResultValue += tempResultValueItem; } } } } else { auto resultStateValue = electronPlateDectect.GetDigitResult(ROI, result[maxCenterBoxId].box).m_sValue; if (resultStateValue == "") { tempResult = "electron_plate"; } else { tempResult = "electron_plate_" + resultStateValue; } } } else if (classifyDectect.className[result[maxCenterBoxId].id] == "contact") { IDetection::DectectResult dectectResultVec = contactDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "operating_handle") { IDetection::DectectResult dectectResultVec = operatingHandleStateDectect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else if (classifyDectect.className[result[maxCenterBoxId].id] == "person") { IDetection::DectectResult dectectResultVec = personBehaviorDetect.GetStateResult(ROI, result[maxCenterBoxId].box); if (dectectResultVec.m_sValue != "") { tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue; } else { tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_"; } /*IDetection::DectectResult dectectResultVec = personBehaviorDetect.GetStateResult(ROI, result[maxCenterBoxId].box); tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;*/ } } ClassifyDectect& JdydAlgorithnm::GetClassifyDectectInstance() { return classifyDectect; } DisconnectorDectect& JdydAlgorithnm::GetdisconnectorDectectInstance() { return disconnectorDectect; } OilLevelDectect& JdydAlgorithnm::GetoilLevelDectectInstance() { return oilLevelDectect; } LightDectect& JdydAlgorithnm::GetlightDectectInstance(){ return lightDectect; } AirSwitchDectect& JdydAlgorithnm::GetairSwitchDectectInstance() { return airSwitchDectect; } ChangeoverSwitchDectect& JdydAlgorithnm::GetchangeoverSwitchDectectInstance() { return changeoverSwitchDectect; } IndoorDisconnectorDectect& JdydAlgorithnm::GetindoorDisconnectorDectectInstance() { return indoorDisconnectorDectect; } MeterDectect& JdydAlgorithnm::GetmeterDectectInstance() { return meterDectect; } CharacterDectect& JdydAlgorithnm::GetcharacterDectectInstance() { return characterDectect; } DisconnectorStateDectect& JdydAlgorithnm::GetdisconnectorStateDectectInstance() { return disconnectorStateDectect; } ElectronPlateDectect& JdydAlgorithnm::GetelectronPlateDectectInstance() { return electronPlateDectect; } NumberDectect& JdydAlgorithnm::GeNumberDectectInstance() { return numberDectect; } OperatingHandleStateDectect& JdydAlgorithnm::GetoperatingHandleStateDectectInstance() { return operatingHandleStateDectect; } PressplateDectect& JdydAlgorithnm::GetpressplateDectectInstance() { return pressplateDectect; } PersonBehaviorDetect& JdydAlgorithnm::GetpersonBehaviorDetectInstance() { return personBehaviorDetect; }