inference.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "inference.h"
  2. //Inference::Inference(const std::string &onnxModelPath, const cv::Size2f &modelInputShape, const std::string &classesTxtFile, const bool &runWithCuda)
  3. //{
  4. // modelPath = onnxModelPath;
  5. // modelShape = modelInputShape;
  6. // classesPath = classesTxtFile;
  7. // cudaEnabled = runWithCuda;
  8. //
  9. // loadOnnxNetwork();
  10. // loadClassesFromFile();
  11. //}
  12. //
  13. //std::vector<Detection> Inference::runInference(const cv::Mat &input)
  14. //{
  15. // cv::Mat modelInput = input;
  16. // if (letterBoxForSquare && modelShape.width == modelShape.height)
  17. // modelInput = formatToSquare(modelInput);
  18. //
  19. // cv::Mat blob;
  20. // cv::dnn::blobFromImage(modelInput, blob, 1.0/255.0, modelShape, cv::Scalar(), true, false);
  21. // net.setInput(blob);
  22. //
  23. // std::vector<cv::Mat> outputs;
  24. // net.forward(outputs, net.getUnconnectedOutLayersNames());
  25. //
  26. // int rows = outputs[0].size[1];
  27. // int dimensions = outputs[0].size[2];
  28. //
  29. // bool yolov8 = false;
  30. // // yolov5 has an output of shape (batchSize, 25200, 85) (Num classes + box[x,y,w,h] + confidence[c])
  31. // // yolov8 has an output of shape (batchSize, 84, 8400) (Num classes + box[x,y,w,h])
  32. // if (dimensions > rows) // Check if the shape[2] is more than shape[1] (yolov8)
  33. // {
  34. // yolov8 = true;
  35. // rows = outputs[0].size[2];
  36. // dimensions = outputs[0].size[1];
  37. //
  38. // outputs[0] = outputs[0].reshape(1, dimensions);
  39. // cv::transpose(outputs[0], outputs[0]);
  40. // }
  41. // float *data = (float *)outputs[0].data;
  42. //
  43. // float x_factor = modelInput.cols / modelShape.width;
  44. // float y_factor = modelInput.rows / modelShape.height;
  45. //
  46. // std::vector<int> class_ids;
  47. // std::vector<float> confidences;
  48. // std::vector<cv::Rect> boxes;
  49. //
  50. // for (int i = 0; i < rows; ++i)
  51. // {
  52. // if (yolov8)
  53. // {
  54. // float *classes_scores = data+4;
  55. //
  56. // cv::Mat scores(1, classes.size(), CV_32FC1, classes_scores);
  57. // cv::Point class_id;
  58. // double maxClassScore;
  59. //
  60. // minMaxLoc(scores, 0, &maxClassScore, 0, &class_id);
  61. //
  62. // if (maxClassScore > modelScoreThreshold)
  63. // {
  64. // confidences.push_back(maxClassScore);
  65. // class_ids.push_back(class_id.x);
  66. //
  67. // float x = data[0];
  68. // float y = data[1];
  69. // float w = data[2];
  70. // float h = data[3];
  71. //
  72. // int left = int((x - 0.5 * w) * x_factor);
  73. // int top = int((y - 0.5 * h) * y_factor);
  74. //
  75. // int width = int(w * x_factor);
  76. // int height = int(h * y_factor);
  77. //
  78. // boxes.push_back(cv::Rect(left, top, width, height));
  79. // }
  80. // }
  81. // else // yolov5
  82. // {
  83. // float confidence = data[4];
  84. //
  85. // if (confidence >= modelConfidenseThreshold)
  86. // {
  87. // float *classes_scores = data+5;
  88. //
  89. // cv::Mat scores(1, classes.size(), CV_32FC1, classes_scores);
  90. // cv::Point class_id;
  91. // double max_class_score;
  92. //
  93. // minMaxLoc(scores, 0, &max_class_score, 0, &class_id);
  94. //
  95. // if (max_class_score > modelScoreThreshold)
  96. // {
  97. // confidences.push_back(confidence);
  98. // class_ids.push_back(class_id.x);
  99. //
  100. // float x = data[0];
  101. // float y = data[1];
  102. // float w = data[2];
  103. // float h = data[3];
  104. //
  105. // int left = int((x - 0.5 * w) * x_factor);
  106. // int top = int((y - 0.5 * h) * y_factor);
  107. //
  108. // int width = int(w * x_factor);
  109. // int height = int(h * y_factor);
  110. //
  111. // boxes.push_back(cv::Rect(left, top, width, height));
  112. // }
  113. // }
  114. // }
  115. //
  116. // data += dimensions;
  117. // }
  118. //
  119. // std::vector<int> nms_result;
  120. // cv::dnn::NMSBoxes(boxes, confidences, modelScoreThreshold, modelNMSThreshold, nms_result);
  121. //
  122. // std::vector<Detection> detections{};
  123. // for (unsigned long i = 0; i < nms_result.size(); ++i)
  124. // {
  125. // int idx = nms_result[i];
  126. //
  127. // Detection result;
  128. // result.class_id = class_ids[idx];
  129. // result.confidence = confidences[idx];
  130. //
  131. // std::random_device rd;
  132. // std::mt19937 gen(rd());
  133. // std::uniform_int_distribution<int> dis(100, 255);
  134. // result.color = cv::Scalar(dis(gen),
  135. // dis(gen),
  136. // dis(gen));
  137. //
  138. // result.className = classes[result.class_id];
  139. // result.box = boxes[idx];
  140. //
  141. // detections.push_back(result);
  142. // }
  143. //
  144. // return detections;
  145. //}
  146. //
  147. //void Inference::loadClassesFromFile()
  148. //{
  149. // std::ifstream inputFile(classesPath);
  150. // if (inputFile.is_open())
  151. // {
  152. // std::string classLine;
  153. // while (std::getline(inputFile, classLine))
  154. // classes.push_back(classLine);
  155. // inputFile.close();
  156. // }
  157. //}
  158. //
  159. //void Inference::loadOnnxNetwork()
  160. //{
  161. // net = cv::dnn::readNetFromONNX(modelPath);
  162. // if (cudaEnabled)
  163. // {
  164. // std::cout << "\nRunning on CUDA" << std::endl;
  165. // net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
  166. // net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
  167. // }
  168. // else
  169. // {
  170. // std::cout << "\nRunning on CPU" << std::endl;
  171. // net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
  172. // net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
  173. // }
  174. //}
  175. //
  176. //cv::Mat Inference::formatToSquare(const cv::Mat &source)
  177. //{
  178. // int col = source.cols;
  179. // int row = source.rows;
  180. // int _max = MAX(col, row);
  181. // cv::Mat result = cv::Mat::zeros(_max, _max, CV_8UC3);
  182. // source.copyTo(result(cv::Rect(0, 0, col, row)));
  183. // return result;
  184. //}