• yolov6-onnx + opencv-DNN


    yolov6-onnx + opencv-DNN 

    yolov6n.onnx https://github.com/meituan/YOLOv6/releases 官方提供

    1. #include
    2. #include
    3. // Namespaces.
    4. using namespace cv;
    5. using namespace std;
    6. using namespace cv::dnn;
    7. // Constants.
    8. const float INPUT_WIDTH = 640.0;
    9. const float INPUT_HEIGHT = 640.0;
    10. const float SCORE_THRESHOLD = 0.5;
    11. const float NMS_THRESHOLD = 0.45;
    12. const float CONFIDENCE_THRESHOLD = 0.45;
    13. // Text parameters.
    14. const float FONT_SCALE = 0.7;
    15. const int FONT_FACE = FONT_HERSHEY_SIMPLEX;
    16. const int THICKNESS = 1;
    17. // Colors.
    18. Scalar BLACK = Scalar(0, 0, 0);
    19. Scalar BLUE = Scalar(255, 178, 50);
    20. Scalar YELLOW = Scalar(0, 255, 255);
    21. Scalar RED = Scalar(0, 0, 255);
    22. // Draw the predicted bounding box.
    23. void draw_label(Mat& input_image, string label, int left, int top)
    24. {
    25. // Display the label at the top of the bounding box.
    26. int baseLine;
    27. Size label_size = getTextSize(label, FONT_FACE, FONT_SCALE, THICKNESS, &baseLine);
    28. top = max(top, label_size.height);
    29. // Top left corner.
    30. Point tlc = Point(left, top);
    31. // Bottom right corner.
    32. Point brc = Point(left + label_size.width, top + label_size.height + baseLine);
    33. // Draw black rectangle.
    34. rectangle(input_image, tlc, brc, BLACK, FILLED);
    35. // Put the label on the black rectangle.
    36. putText(input_image, label, Point(left, top + label_size.height), FONT_FACE, FONT_SCALE, YELLOW, THICKNESS);
    37. }
    38. vector pre_process(Mat& input_image, Net& net)
    39. {
    40. // Convert to blob.
    41. Mat blob;
    42. blobFromImage(input_image, blob, 1. / 255., Size(INPUT_WIDTH, INPUT_HEIGHT), Scalar(), true, false);
    43. net.setInput(blob);
    44. // Forward propagate.
    45. vector outputs;
    46. net.forward(outputs, net.getUnconnectedOutLayersNames());
    47. return outputs;
    48. }
    49. Mat post_process(Mat& input_image, vector& outputs, const vector& class_name)
    50. {
    51. // Initialize vectors to hold respective outputs while unwrapping detections.
    52. vector<int> class_ids;
    53. vector<float> confidences;
    54. vector boxes;
    55. // Resizing factor.
    56. float x_factor = input_image.cols / INPUT_WIDTH;
    57. float y_factor = input_image.rows / INPUT_HEIGHT;
    58. float* data = (float*)outputs[0].data;
    59. const int dimensions = 85;
    60. const int rows = 8400;
    61. // Iterate through 8400 detections.
    62. for (int i = 0; i < rows; ++i)
    63. {
    64. float confidence = data[4];
    65. // Discard bad detections and continue.
    66. if (confidence >= CONFIDENCE_THRESHOLD)
    67. {
    68. float* classes_scores = data + 5;
    69. // Create a 1x85 Mat and store class scores of 80 classes.
    70. Mat scores(1, class_name.size(), CV_32FC1, classes_scores);
    71. // Perform minMaxLoc and acquire index of best class score.
    72. Point class_id;
    73. double max_class_score;
    74. minMaxLoc(scores, 0, &max_class_score, 0, &class_id);
    75. // Continue if the class score is above the threshold.
    76. if (max_class_score > SCORE_THRESHOLD)
    77. {
    78. // Store class ID and confidence in the pre-defined respective vectors.
    79. confidences.push_back(confidence);
    80. class_ids.push_back(class_id.x);
    81. // Center.
    82. float cx = data[0];
    83. float cy = data[1];
    84. // Box dimension.
    85. float w = data[2];
    86. float h = data[3];
    87. // Bounding box coordinates.
    88. int left = int((cx - 0.5 * w) * x_factor);
    89. int top = int((cy - 0.5 * h) * y_factor);
    90. int width = int(w * x_factor);
    91. int height = int(h * y_factor);
    92. // Store good detections in the boxes vector.
    93. boxes.push_back(Rect(left, top, width, height));
    94. }
    95. }
    96. // Jump to the next column.
    97. data += 85;
    98. }
    99. // Perform Non Maximum Suppression and draw predictions.
    100. vector<int> indices;
    101. NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, indices);
    102. for (int i = 0; i < indices.size(); i++)
    103. {
    104. int idx = indices[i];
    105. Rect box = boxes[idx];
    106. int left = box.x;
    107. int top = box.y;
    108. int width = box.width;
    109. int height = box.height;
    110. // Draw bounding box.
    111. rectangle(input_image, Point(left, top), Point(left + width, top + height), BLUE, 3 * THICKNESS);
    112. // Get the label for the class name and its confidence.
    113. string label = format("%.2f", confidences[idx]);
    114. label = class_name[class_ids[idx]] + ":" + label;
    115. // Draw class labels.
    116. draw_label(input_image, label, left, top);
    117. }
    118. return input_image;
    119. }
    120. int main(int argc, char** argv)
    121. {
    122. // Usage: "./yolov6 /path/to/your/model/yolov6n.onnx /path/to/image/sample.jpg /path/to/coco.names"
    123. // printf(CV_VERSION);
    124. // Load class list.
    125. vector class_list;
    126. ifstream ifs("coco.names");// coco.names argv[3]
    127. string line;
    128. while (getline(ifs, line))
    129. {
    130. class_list.push_back(line);
    131. }
    132. // Load image.
    133. Mat frame;
    134. frame = imread("v_0.jpg");//v_0.jpg argv[2]
    135. Mat input_frame = frame.clone();
    136. // Load model.
    137. Net net;
    138. net = readNetFromONNX("model_s/yolov6n.onnx");//argv[1]//yolov6n.onnx yolov6s_base_bs1.onnx
    139. // Put efficiency information.
    140. // The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
    141. int cycles = 30;
    142. double total_time = 0;
    143. double freq = getTickFrequency() / 1000;
    144. Mat img;
    145. for (int i = 0; i < cycles; ++i)
    146. {
    147. vector detections;
    148. Mat input = input_frame.clone();
    149. detections = pre_process(input, net);
    150. img = post_process(input, detections, class_list);
    151. vector<double> layersTimes;
    152. double t = net.getPerfProfile(layersTimes);
    153. total_time = total_time + t;
    154. cout << format("Cycle [%d]:\t%.2f\tms", i + 1, t / freq) << endl;
    155. }
    156. double avg_time = total_time / cycles;
    157. string label = format("Average inference time : %.2f ms", avg_time / freq);
    158. cout << label << endl;
    159. putText(img, label, Point(20, 40), FONT_FACE, FONT_SCALE, RED);
    160. string model_path = "model_s/yolov6n.onnx";// argv[1]//yolov6n.onnx yolov6s_base_bs1.onnx
    161. int start_index = model_path.rfind("/");
    162. string model_name = model_path.substr(start_index + 1, model_path.length() - start_index - 6);
    163. imshow("C++_" + model_name, img);
    164. waitKey(0);
    165. return 0;
    166. }

    YOLOV5可参考另一篇文章:

     yolov5-onnx + opencv-DNN​​​​​​​​​​​​​​

     

  • 相关阅读:
    axios的简介认识(从头到尾详细)
    ubuntu bind9 主从配置
    arm-linux交叉编译Gstreamer
    机器学习--决策树(sklearn)
    从零开始安装并运行YOLOv5
    容器服务(三)自动化监控 Prometheus、Grafana
    jenkins 从机连接主机显示 404 Not Found
    Redis(链表数据结构)
    Docker 安装 Oracle Database 23c
    小项目-词法分析器
  • 原文地址:https://blog.csdn.net/zaibeijixing/article/details/128197885