123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #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;
- 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);
- 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<Output> 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 <string> rstStrArr;
- vector<vector<int>> 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<int> 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;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="tempResult"></param>
- /// <param name="tempResultValue"></param>
- /// <param name="instructionsDectectResultStr"></param>
- /// <param name="result"></param>
- /// <param name="maxCenterBoxId"></param>
- /// <param name="pluginNameId"></param>
- /// <param name="ROI"></param>
- void JdydAlgorithnm::GetMaxConfidenceRecoResult(string &tempResult, string &tempResultValue, const string instructionsDectectResultStr,vector<Output> 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] == "person")//人员入侵
- {
- tempResult = classifyDectect.className[result[maxCenterBoxId].id];
- }
- else 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")
- {
- tempResult = "pressplate_on" ;
- }
- 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;
- }
-
- }
- 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;
- }
|