当前位置:   article > 正文

c代码调用YOLO模型_yolo vc运用

yolo vc运用
  1. #include <fstream>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <opencv2/dnn.hpp>
  5. #include <opencv2/imgproc.hpp>
  6. #include <opencv2/highgui.hpp>
  7. #include<vector>
  8. using namespace std;
  9. using namespace cv;
  10. using namespace dnn;
  11. vector<string> classes;
  12. vector<String> getOutputsNames(Net&net)
  13. {
  14. static vector<String> names;
  15. if (names.empty())
  16. {
  17. //Get the indices of the output layers, i.e. the layers with unconnected outputs
  18. vector<int> outLayers = net.getUnconnectedOutLayers();
  19. //get the names of all the layers in the network
  20. vector<String> layersNames = net.getLayerNames();
  21. // Get the names of the output layers in names
  22. names.resize(outLayers.size());
  23. for (size_t i = 0; i < outLayers.size(); ++i)
  24. names[i] = layersNames[outLayers[i] - 1];
  25. }
  26. return names;
  27. }
  28. void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
  29. {
  30. //Draw a rectangle displaying the bounding box
  31. rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
  32. //Get the label for the class name and its confidence
  33. string label = format("%.5f", conf);
  34. if (!classes.empty())
  35. {
  36. CV_Assert(classId < (int)classes.size());
  37. label = classes[classId] + ":" + label;
  38. }
  39. //Display the label at the top of the bounding box
  40. int baseLine;
  41. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  42. top = max(top, labelSize.height);
  43. rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
  44. putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
  45. }
  46. void postprocess(Mat& frame, const vector<Mat>& outs, float confThreshold, float nmsThreshold)
  47. {
  48. vector<int> classIds;
  49. vector<float> confidences;
  50. vector<Rect> boxes;
  51. for (size_t i = 0; i < outs.size(); ++i)
  52. {
  53. // Scan through all the bounding boxes output from the network and keep only the
  54. // ones with high confidence scores. Assign the box's class label as the class
  55. // with the highest score for the box.
  56. float* data = (float*)outs[i].data;
  57. for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
  58. {
  59. Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
  60. Point classIdPoint;
  61. double confidence;
  62. // Get the value and location of the maximum score
  63. minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
  64. if (confidence > confThreshold)
  65. {
  66. int centerX = (int)(data[0] * frame.cols);
  67. int centerY = (int)(data[1] * frame.rows);
  68. int width = (int)(data[2] * frame.cols);
  69. int height = (int)(data[3] * frame.rows);
  70. int left = centerX - width / 2;
  71. int top = centerY - height / 2;
  72. classIds.push_back(classIdPoint.x);
  73. confidences.push_back((float)confidence);
  74. boxes.push_back(Rect(left, top, width, height));
  75. }
  76. }
  77. }
  78. // Perform non maximum suppression to eliminate redundant overlapping boxes with
  79. // lower confidences
  80. vector<int> indices;
  81. NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
  82. for (size_t i = 0; i < indices.size(); ++i)
  83. {
  84. int idx = indices[i];
  85. Rect box = boxes[idx];
  86. drawPred(classIds[idx], confidences[idx], box.x, box.y,
  87. box.x + box.width, box.y + box.height, frame);
  88. }
  89. }
  90. int main()
  91. {
  92. string names_file = "/home/xxp/darknet/data/voc-mask.names";
  93. String model_def = "/home/xxp/darknet/cfg/yolov3-tiny-mask3.cfg";
  94. String weights = "/home/xxp/darknet/backup/yolov3-tiny-mask3_best.weights";
  95. int in_w, in_h;
  96. double thresh = 0.5;
  97. double nms_thresh = 0.25;
  98. in_w = in_h = 608;
  99. string img_path = "/home/xxp/darknet/testfiles/120.jpg";
  100. //read names
  101. ifstream ifs(names_file.c_str());
  102. string line;
  103. while (getline(ifs, line)) classes.push_back(line);
  104. //init model
  105. Net net = readNetFromDarknet(model_def, weights);
  106. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  107. net.setPreferableTarget(DNN_TARGET_CPU);
  108. //read image and forward
  109. VideoCapture capture(0);// VideoCapture:OENCV中新增的类,捕获视频并显示出来
  110. while (1)
  111. {
  112. Mat frame, blob;
  113. capture >> frame;
  114. blobFromImage(frame, blob, 1 / 255.0, Size(in_w, in_h), Scalar(), true, false);
  115. vector<Mat> mat_blob;
  116. imagesFromBlob(blob, mat_blob);
  117. //Sets the input to the network
  118. net.setInput(blob);
  119. // Runs the forward pass to get output of the output layers
  120. vector<Mat> outs;
  121. net.forward(outs, getOutputsNames(net));
  122. postprocess(frame, outs, thresh, nms_thresh);
  123. vector<double> layersTimes;
  124. double freq = getTickFrequency() / 1000;
  125. double t = net.getPerfProfile(layersTimes) / freq;
  126. string label = format("Inference time for a frame : %.2f ms", t);
  127. putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
  128. imshow("res", frame);
  129. waitKey(10);
  130. }
  131. return 0;
  132. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/980763
推荐阅读
相关标签
  

闽ICP备14008679号