ChangeoverSwitchDectect.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "ChangeoverSwitchDectect.h"
  2. using namespace std;
  3. bool ChangeoverSwitchDectect::Init(bool isCuda)
  4. {
  5. string model_path = "models/changeover_switch-sim.onnx";
  6. try {
  7. net = cv::dnn::readNet(model_path);
  8. }
  9. catch (const std::exception& ex)
  10. {
  11. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  12. return false;
  13. }
  14. //cuda
  15. if (isCuda) {
  16. net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
  17. net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA_FP16);
  18. }
  19. //cpu
  20. else {
  21. net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
  22. net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
  23. }
  24. return true;
  25. }
  26. IDetection::DectectResult ChangeoverSwitchDectect::GetStateResult(cv::Mat img, cv::Rect rec)
  27. {
  28. std::cout << "test" << std::endl;
  29. try
  30. {
  31. cv::Mat ROI = img(rec);
  32. //imwrite("test.png", ROI);
  33. //YunDaISASImageRecognitionService::SetImage(QString::fromStdString("test.png"));
  34. Detect(ROI);
  35. }
  36. catch (const std::exception& ex)
  37. {
  38. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  39. }
  40. if (resultValue.m_confidence < 0.1)
  41. {
  42. resultValue = DectectResult(0.45, 0, className[1]);
  43. }
  44. return resultValue;
  45. }
  46. IDetection::DectectResult ChangeoverSwitchDectect::GetDigitResult(cv::Mat img, cv::Rect rec)
  47. {
  48. return resultValue;
  49. }
  50. bool ChangeoverSwitchDectect::Detect(cv::Mat& SrcImg) {
  51. cv::Mat blob;
  52. int col = SrcImg.cols;
  53. int row = SrcImg.rows;
  54. int maxLen = MAX(col, row);
  55. cv::Mat netInputImg = SrcImg.clone();
  56. if (maxLen > 1.2 * col || maxLen > 1.2 * row) {
  57. cv::Mat resizeImg = cv::Mat::zeros(maxLen, maxLen, CV_8UC3);
  58. SrcImg.copyTo(resizeImg(cv::Rect(0, 0, col, row)));
  59. netInputImg = resizeImg;
  60. }
  61. cv::dnn::blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(netWidth, netHeight), cv::Scalar(0, 0, 0), true, false);
  62. net.setInput(blob);
  63. std::vector<cv::Mat> netOutputImg;
  64. net.forward(netOutputImg, net.getUnconnectedOutLayersNames());
  65. std::vector<int> classIds;//结果id数组
  66. std::vector<float> confidences;//结果每个id对应置信度数组
  67. std::vector<cv::Rect> boxes;//每个id矩形框
  68. float ratio_h = (float)netInputImg.rows / netHeight;
  69. float ratio_w = (float)netInputImg.cols / netWidth;
  70. int net_width = className.size() + 5; //输出的网络宽度是类别数+5
  71. float* pdata = (float*)netOutputImg[0].data;
  72. for (int stride = 0; stride < strideSize; stride++) { //stride
  73. int grid_x = (int)(netWidth / netStride[stride]);
  74. int grid_y = (int)(netHeight / netStride[stride]);
  75. for (int anchor = 0; anchor < 3; anchor++) { //anchors
  76. const float anchor_w = netAnchors[stride][anchor * 2];
  77. const float anchor_h = netAnchors[stride][anchor * 2 + 1];
  78. for (int i = 0; i < grid_y; i++) {
  79. for (int j = 0; j < grid_x; j++) {
  80. float box_score = pdata[4]; ;//获取每一行的box框中含有某个物体的概率
  81. if (box_score >= boxThreshold) {
  82. cv::Mat scores(1, className.size(), CV_32FC1, pdata + 5);
  83. cv::Point classIdPoint;
  84. double max_class_socre;
  85. minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
  86. max_class_socre = (float)max_class_socre;
  87. if (max_class_socre >= classThreshold)
  88. {
  89. //rect [x,y,w,h]
  90. float x = pdata[0]; //x
  91. float y = pdata[1]; //y
  92. float w = pdata[2]; //w
  93. float h = pdata[3]; //h
  94. int left = (x - 0.5 * w) * ratio_w;
  95. int top = (y - 0.5 * h) * ratio_h;
  96. left = left < 0 ? 0 : left;
  97. top = top < 0 ? 0 : top;
  98. int widthBox = int(w * ratio_w);
  99. int heightBox = int(h * ratio_h);
  100. widthBox = widthBox > col ? col : widthBox;
  101. heightBox = heightBox > row ? row : heightBox;
  102. if (left < 0 || left>col || top < 0 || top>row || widthBox > col || heightBox > row)
  103. {
  104. continue;
  105. }
  106. classIds.push_back(classIdPoint.x);
  107. confidences.push_back(max_class_socre * box_score);
  108. boxes.push_back(cv::Rect(left, top, widthBox, heightBox));
  109. }
  110. }
  111. pdata += net_width;//下一行
  112. }
  113. }
  114. }
  115. }
  116. //执行非最大抑制以消除具有较低置信度的冗余重叠框(NMS)
  117. vector<int> nms_result;
  118. cv::dnn::NMSBoxes(boxes, confidences, nmsScoreThreshold, nmsThreshold, nms_result);
  119. float confidenceTypeMax = 0;
  120. int confidenceTypeMaxId = 0;
  121. float confidenceStateMax = 0;
  122. int confidenceStateMaxId = 0;
  123. string typeStr = "";
  124. string stateStr = "";
  125. if (nms_result.size()>0)
  126. {
  127. for (int i = 0; i < nms_result.size(); i++) {
  128. int idx = nms_result[i];
  129. string key = className[classIds[idx]];
  130. if (std::count(classTypeName.begin(), classTypeName.end(), key)) {
  131. std::cout << "Element found";
  132. if (confidences[idx] > confidenceTypeMax) {
  133. confidenceTypeMax = confidences[idx];
  134. typeStr = key;
  135. }
  136. }
  137. else {
  138. std::cout << "Element not found";
  139. if (confidences[idx] > confidenceStateMax) {
  140. confidenceStateMax = confidences[idx];
  141. stateStr = key;
  142. }
  143. if (std::count(classLightStateName.begin(), classLightStateName.end(), key))
  144. {
  145. confidenceStateMax = 0.99;
  146. stateStr = key;
  147. }
  148. }
  149. /*if (confidences[idx] > confidenceMax)
  150. {
  151. confidenceMax = confidences[idx];
  152. resultValue = DectectResult(confidenceMax, 0, className[classIds[idx]]);
  153. }*/
  154. YunDaISASImageRecognitionService::ConsoleLog(QString::fromStdString(className[classIds[idx]]));
  155. //resultValue.push_back(DectectResult(, 0, className[classIds[idx]]));
  156. }
  157. }
  158. else {
  159. typeStr = className[15];
  160. stateStr = className[18];
  161. }
  162. if (typeStr!="")
  163. {
  164. resultValue = DectectResult(confidenceTypeMax, 0, typeStr + '_' + stateStr);
  165. }
  166. else
  167. {
  168. resultValue = DectectResult(confidenceStateMax, 0, stateStr);
  169. }
  170. return true;
  171. }