JdydAlgorithnm.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "JdydAlgorithnm.h"
  2. static ClassifyDectect classifyDectect;
  3. static DisconnectorDectect disconnectorDectect;
  4. static OilLevelDectect oilLevelDectect;
  5. static LightDectect lightDectect;
  6. static AirSwitchDectect airSwitchDectect;
  7. static ChangeoverSwitchDectect changeoverSwitchDectect;
  8. static IndoorDisconnectorDectect indoorDisconnectorDectect;
  9. static TriangleDisconnectorDectect triangleDisconnectorDectect;
  10. static MeterDectect meterDectect;
  11. static CharacterDectect characterDectect;
  12. static DisconnectorStateDectect disconnectorStateDectect;
  13. static ElectronPlateDectect electronPlateDectect;
  14. static NumberDectect numberDectect;
  15. static InstructionsDectect instructionsDectect;
  16. static ContactDectect contactDectect;
  17. static OperatingHandleStateDectect operatingHandleStateDectect;
  18. bool JdydAlgorithnm::Init()
  19. {
  20. try
  21. {
  22. bool isCuda = false;
  23. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  24. classifyDectect.Init(isCuda);
  25. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("分类模型"));
  26. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  27. disconnectorDectect.Init(isCuda);
  28. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("隔离开关模型"));
  29. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  30. operatingHandleStateDectect.Init(isCuda);
  31. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("操作手柄模型"));
  32. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  33. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("安全防范模型"));
  34. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  35. oilLevelDectect.Init(isCuda);
  36. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("油位模型"));
  37. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  38. lightDectect.Init(isCuda);
  39. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("信号灯模型"));
  40. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  41. airSwitchDectect.Init(isCuda);
  42. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("空开模型"));
  43. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  44. changeoverSwitchDectect.Init(isCuda);
  45. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("旋转开关模型"));
  46. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  47. indoorDisconnectorDectect.Init(isCuda);
  48. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("竖直隔离开关模型"));
  49. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  50. triangleDisconnectorDectect.Init(isCuda);
  51. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("三角隔离开关模型"));
  52. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  53. meterDectect.Init(isCuda);
  54. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("仪表模型"));
  55. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  56. characterDectect.Init(isCuda);
  57. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("字符型模型"));
  58. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  59. disconnectorStateDectect.Init(isCuda);
  60. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("隔离开关室内状态模型"));
  61. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  62. electronPlateDectect.Init(isCuda);
  63. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("电子数字表盘模型"));
  64. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  65. numberDectect.Init(isCuda);
  66. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("数字识别模型"));
  67. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  68. instructionsDectect.Init(isCuda);
  69. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("标识识别模型"));
  70. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  71. contactDectect.Init(isCuda);
  72. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("连接点识别模型"));
  73. }
  74. catch (const std::exception& ex)
  75. {
  76. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  77. }
  78. return true;
  79. }
  80. json JdydAlgorithnm::CallAlgorithnm(string pluginName, string filePath, string modelPath, int* roiVec, string pcArg)
  81. {
  82. cv::Mat img = cv::imread(filePath);
  83. int imgWidth = (roiVec[4] - roiVec[0])<1 || (roiVec[4] - roiVec[0]) > img.cols ? img.cols: (roiVec[4] - roiVec[0]);
  84. int imgheight =(roiVec[5] - roiVec[1])<1 || (roiVec[5] - roiVec[1]) > img.rows ? img.rows : (roiVec[5] - roiVec[1]);
  85. int topX = roiVec[0] <0 || roiVec[0]> img.cols ? 0: roiVec[0];
  86. int topY = roiVec[1] < 0 || roiVec[1]> img.rows ? 0 : roiVec[1];
  87. if (topX+ imgWidth> img.cols)
  88. {
  89. topX = 0;
  90. imgWidth = img.cols;
  91. }
  92. if (topY+ imgheight> img.rows)
  93. {
  94. topY = 0;
  95. imgheight = img.rows;
  96. }
  97. cv::Rect rect(topX, topY, imgWidth, imgheight);
  98. cv::Mat ROI = img(rect);
  99. string tempPath = "test.png";
  100. vector<Output> result;
  101. if (classifyDectect.Detect(ROI, result))
  102. {
  103. if (YunDaISASImageRecognitionService::GetIsShowDectect())
  104. {
  105. cv::Mat drawROI;
  106. ROI.copyTo(drawROI);
  107. classifyDectect.drawPred(drawROI, result);
  108. imwrite("test.png", drawROI);
  109. YunDaISASImageRecognitionService::SetImage(QString::fromStdString(tempPath));
  110. }
  111. }
  112. string rstStr = "";
  113. string rstValue = "";
  114. int flag = -1;
  115. vector <string> rstStrArr;
  116. vector<vector<int>> rstRoiArr;
  117. if (1 <= result.size())
  118. {
  119. float confidenceMax = 0;
  120. int confidenceMaxId = 0;
  121. int closestCenterPoint = ROI.cols + ROI.rows;
  122. int centerX = ROI.cols / 2;
  123. int centerY = ROI.rows / 2;
  124. int maxCenterBoxId = 0;
  125. float instructionsConfidenceMax = 0;
  126. int instructionsConfidenceMaxId = -1;
  127. float pluginNameConfidenceMax = 0;
  128. int pluginNameId = -1;
  129. for (size_t i = 0; i < result.size(); i++)
  130. {
  131. int absX = cv::abs((result[i].box.x + result[i].box.width / 2) - centerX);
  132. int absY = cv::abs((result[i].box.y + result[i].box.height / 2) - centerY);
  133. if (classifyDectect.className[result[i].id] == classifyDectect.instructionsName)
  134. {
  135. if (result[i].confidence> instructionsConfidenceMax)
  136. {
  137. instructionsConfidenceMax = result[i].confidence;
  138. instructionsConfidenceMaxId = i;
  139. }
  140. }
  141. else if (pluginName.size()>0 && classifyDectect.className[result[i].id] == pluginName)
  142. {
  143. if (result[i].confidence > pluginNameConfidenceMax)
  144. {
  145. pluginNameConfidenceMax = result[i].confidence;
  146. pluginNameId = i;
  147. }
  148. }
  149. else
  150. {
  151. if (result[i].confidence > confidenceMax)
  152. {
  153. confidenceMax = result[i].confidence;
  154. confidenceMaxId = i;
  155. }
  156. if (absX + absY < closestCenterPoint)
  157. {
  158. closestCenterPoint = absX + absY;
  159. maxCenterBoxId = i;
  160. }
  161. }
  162. vector<int> vecRec;
  163. vecRec.push_back(result[i].box.x);
  164. vecRec.push_back(result[i].box.y);
  165. vecRec.push_back(result[i].box.width);
  166. vecRec.push_back(result[i].box.height);
  167. rstRoiArr.push_back(vecRec);
  168. rstStrArr.push_back(classifyDectect.className[result[i].id]);
  169. }
  170. /*if (!YunDaISASImageRecognitionService::GetReconginzeStratgy())
  171. {
  172. maxCenterBoxId = confidenceMaxId;
  173. }*/
  174. string instructionsDectectResultStr = "";
  175. if (instructionsConfidenceMaxId!=-1)
  176. {
  177. IDetection::DectectResult dectectResult = instructionsDectect.GetStateResult(ROI, result[instructionsConfidenceMaxId].box);
  178. instructionsDectectResultStr = dectectResult.m_sValue;
  179. }
  180. /* string tempResult = "";
  181. string tempResultValue = "";*/
  182. GetMaxConfidenceRecoResult(rstStr, rstValue, instructionsDectectResultStr,result, maxCenterBoxId, pluginNameId,ROI);
  183. /* rstStr = tempResult;
  184. rstValue = tempResultValue;*/
  185. }
  186. else
  187. {
  188. if (pluginName.size() > 0)
  189. {
  190. for (size_t i = 0; i < classifyDectect.className.size(); i++)
  191. {
  192. if (classifyDectect.className[i] == pluginName)
  193. {
  194. Output pluginNameOutput;
  195. pluginNameOutput.box.x = 0;
  196. pluginNameOutput.box.y = 0;
  197. pluginNameOutput.box.width = ROI.cols;
  198. pluginNameOutput.box.height = ROI.rows;
  199. pluginNameOutput.id = i;
  200. pluginNameOutput.confidence = 0.99;
  201. result.push_back(pluginNameOutput);
  202. int pluginNameId = 0;
  203. string instructionsDectectResultStr = "";
  204. int maxCenterBoxId = 0;
  205. GetMaxConfidenceRecoResult(rstStr, rstValue, instructionsDectectResultStr, result, maxCenterBoxId, pluginNameId, ROI);
  206. }
  207. }
  208. }
  209. }
  210. if (rstStr.size()>300)
  211. {
  212. rstStr = "";
  213. }
  214. json resultJson = { {"flag", flag}, {"result" , rstStr}, {"resultValue" , rstValue},{"results" , rstStrArr} ,{"resultRois",rstRoiArr}};
  215. return resultJson;
  216. }
  217. /// <summary>
  218. /// 获取
  219. /// </summary>
  220. /// <param name="tempResult"></param>
  221. /// <param name="tempResultValue"></param>
  222. /// <param name="instructionsDectectResultStr"></param>
  223. /// <param name="result"></param>
  224. /// <param name="maxCenterBoxId"></param>
  225. /// <param name="pluginNameId"></param>
  226. /// <param name="ROI"></param>
  227. void JdydAlgorithnm::GetMaxConfidenceRecoResult(string &tempResult, string &tempResultValue, const string instructionsDectectResultStr,vector<Output> result, int maxCenterBoxId,const int pluginNameId,const cv::Mat ROI)
  228. {
  229. if (pluginNameId > -1)
  230. {
  231. maxCenterBoxId = pluginNameId;
  232. }
  233. cv::Rect boxTemp = result[maxCenterBoxId].box;
  234. if (boxTemp.x<0 ||
  235. boxTemp.y < 0 ||
  236. boxTemp.x + boxTemp.width>ROI.cols ||
  237. boxTemp.y + boxTemp.height>ROI.rows)
  238. {
  239. boxTemp.x = 0;
  240. boxTemp.y = 0;
  241. boxTemp.width = ROI.cols;
  242. boxTemp.height = ROI.rows;
  243. }
  244. result[maxCenterBoxId].box = boxTemp;
  245. if (classifyDectect.className[result[maxCenterBoxId].id] == "person")//人员入侵
  246. {
  247. tempResult = classifyDectect.className[result[maxCenterBoxId].id];
  248. }
  249. else if (classifyDectect.className[result[maxCenterBoxId].id] == "disconnector")//隔离开关
  250. {
  251. IDetection::DectectResult dectectResultVec = disconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  252. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  253. }
  254. else if (classifyDectect.className[result[maxCenterBoxId].id] == "oil_level")//油位
  255. {
  256. IDetection::DectectResult dectectResultVec = oilLevelDectect.GetDigitResult(ROI, result[maxCenterBoxId].box);
  257. if (dectectResultVec.m_sValue != "")
  258. {
  259. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  260. }
  261. else {
  262. tempResult = classifyDectect.className[result[maxCenterBoxId].id];
  263. }
  264. tempResultValue = std::to_string(dectectResultVec.m_dValue);
  265. }
  266. else if (classifyDectect.className[result[maxCenterBoxId].id] == "meter")//指针表盘
  267. {
  268. auto dectectResults = meterDectect.GetDigitResults(ROI, result[maxCenterBoxId].box);
  269. if (dectectResults.size() > 0)
  270. {
  271. for (size_t i = 0; i < dectectResults.size(); i++)
  272. {
  273. string tempResultItem = "";
  274. string tempResultValueItem = "";
  275. if (dectectResults[i].m_sValue != "")
  276. {
  277. tempResultItem = "meter_" + dectectResults[i].m_sValue;
  278. }
  279. else {
  280. tempResultItem = classifyDectect.className[result[maxCenterBoxId].id];
  281. }
  282. tempResultValueItem = std::to_string(dectectResults[i].m_dValue);
  283. if (tempResultValueItem != "" && tempResultItem != "")
  284. {
  285. if (tempResult == "")
  286. {
  287. tempResult += tempResultItem;
  288. tempResultValue += tempResultValueItem;
  289. }
  290. else
  291. {
  292. tempResult += ";";
  293. tempResult += tempResultItem;
  294. tempResultValue += ";";
  295. tempResultValue += tempResultValueItem;
  296. }
  297. }
  298. }
  299. }
  300. else {
  301. tempResult = "meter_" + meterDectect.GetDigitResult(ROI, result[maxCenterBoxId].box).m_sValue;
  302. }
  303. }
  304. else if (classifyDectect.className[result[maxCenterBoxId].id] == "changeover_switch")
  305. {
  306. IDetection::DectectResult dectectResultVec = changeoverSwitchDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  307. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  308. }
  309. else if (classifyDectect.className[result[maxCenterBoxId].id] == "pressplate")
  310. {
  311. tempResult = "pressplate_on" ;
  312. }
  313. else if (classifyDectect.className[result[maxCenterBoxId].id] == "disconnector_state")
  314. {
  315. IDetection::DectectResult dectectResultVec = disconnectorStateDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  316. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  317. }
  318. else if (classifyDectect.className[result[maxCenterBoxId].id] == "light")
  319. {
  320. IDetection::DectectResult dectectResultVec = lightDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  321. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  322. }
  323. else if (classifyDectect.className[result[maxCenterBoxId].id] == "air_switch")
  324. {
  325. IDetection::DectectResult dectectResultVec = airSwitchDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  326. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  327. }
  328. else if (classifyDectect.className[result[maxCenterBoxId].id] == "character")
  329. {
  330. IDetection::DectectResult dectectResultVec = characterDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  331. if (characterDectect.GetStateResults().size() > 0)
  332. {
  333. string tempResultItem = "";
  334. for (size_t i = 0; i < characterDectect.GetStateResults().size(); i++)
  335. {
  336. if (tempResultItem == "")
  337. {
  338. tempResultItem += "character_" + characterDectect.GetStateResults()[i].m_sValue;
  339. }
  340. else
  341. {
  342. tempResultItem += ";";
  343. tempResultItem += "character_" + characterDectect.GetStateResults()[i].m_sValue;
  344. }
  345. }
  346. tempResult = tempResultItem;
  347. }
  348. else
  349. {
  350. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  351. }
  352. }
  353. else if (classifyDectect.className[result[maxCenterBoxId].id] == "indoor_disconnector")
  354. {
  355. IDetection::DectectResult dectectResultVec = indoorDisconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  356. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  357. }
  358. else if (classifyDectect.className[result[maxCenterBoxId].id] == "triangle_disconnector")
  359. {
  360. IDetection::DectectResult dectectResultVec = triangleDisconnectorDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  361. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  362. }
  363. else if (classifyDectect.className[result[maxCenterBoxId].id] == "electron_plate")//电子数字盘
  364. {
  365. auto dectectResults = electronPlateDectect.GetDigitResults(ROI, result[maxCenterBoxId].box);
  366. if (dectectResults.size() > 0)
  367. {
  368. for (size_t i = 0; i < dectectResults.size(); i++)
  369. {
  370. string tempResultItem = "";
  371. string tempResultValueItem = "";
  372. if (dectectResults[i].m_sValue != "digit")
  373. {
  374. tempResultItem = "electron_plate_" + dectectResults[i].m_sValue;
  375. //tempResult = tempResultItem;
  376. }
  377. else {
  378. tempResultItem = "electron_plate_" + dectectResults[i].m_sValue;
  379. tempResultValueItem = std::to_string(dectectResults[i].m_dValue);
  380. }
  381. if (tempResultItem != "")
  382. {
  383. if (tempResult == "")
  384. {
  385. tempResult += tempResultItem;
  386. tempResultValue += tempResultValueItem;
  387. }
  388. else
  389. {
  390. tempResult += ";";
  391. tempResult += tempResultItem;
  392. tempResultValue += ";";
  393. tempResultValue += tempResultValueItem;
  394. }
  395. }
  396. }
  397. }
  398. else {
  399. auto resultStateValue = electronPlateDectect.GetDigitResult(ROI, result[maxCenterBoxId].box).m_sValue;
  400. if (resultStateValue == "") {
  401. tempResult = "electron_plate";
  402. }
  403. else {
  404. tempResult = "electron_plate_" + resultStateValue;
  405. }
  406. }
  407. }
  408. else if (classifyDectect.className[result[maxCenterBoxId].id] == "contact")
  409. {
  410. IDetection::DectectResult dectectResultVec = contactDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  411. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  412. }
  413. else if (classifyDectect.className[result[maxCenterBoxId].id] == "operating_handle")
  414. {
  415. IDetection::DectectResult dectectResultVec = operatingHandleStateDectect.GetStateResult(ROI, result[maxCenterBoxId].box);
  416. tempResult = classifyDectect.className[result[maxCenterBoxId].id] + "_" + dectectResultVec.m_sValue;
  417. }
  418. }
  419. ClassifyDectect& JdydAlgorithnm::GetClassifyDectectInstance() {
  420. return classifyDectect;
  421. }
  422. DisconnectorDectect& JdydAlgorithnm::GetdisconnectorDectectInstance() {
  423. return disconnectorDectect;
  424. }
  425. OilLevelDectect& JdydAlgorithnm::GetoilLevelDectectInstance() {
  426. return oilLevelDectect;
  427. }
  428. LightDectect& JdydAlgorithnm::GetlightDectectInstance(){
  429. return lightDectect;
  430. }
  431. AirSwitchDectect& JdydAlgorithnm::GetairSwitchDectectInstance() {
  432. return airSwitchDectect;
  433. }
  434. ChangeoverSwitchDectect& JdydAlgorithnm::GetchangeoverSwitchDectectInstance() {
  435. return changeoverSwitchDectect;
  436. }
  437. IndoorDisconnectorDectect& JdydAlgorithnm::GetindoorDisconnectorDectectInstance() {
  438. return indoorDisconnectorDectect;
  439. }
  440. MeterDectect& JdydAlgorithnm::GetmeterDectectInstance() {
  441. return meterDectect;
  442. }
  443. CharacterDectect& JdydAlgorithnm::GetcharacterDectectInstance() {
  444. return characterDectect;
  445. }
  446. DisconnectorStateDectect& JdydAlgorithnm::GetdisconnectorStateDectectInstance() {
  447. return disconnectorStateDectect;
  448. }
  449. ElectronPlateDectect& JdydAlgorithnm::GetelectronPlateDectectInstance() {
  450. return electronPlateDectect;
  451. }
  452. NumberDectect& JdydAlgorithnm::GeNumberDectectInstance()
  453. {
  454. return numberDectect;
  455. }
  456. OperatingHandleStateDectect& JdydAlgorithnm::GetoperatingHandleStateDectectInstance()
  457. {
  458. return operatingHandleStateDectect;
  459. }