InstructionsDectect.cpp 4.7 KB

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