herrDeng網內搜尋

自訂搜尋

Ads

2021年12月6日 星期一

Visual C++ darknet opencv cuda yolo4測試即時影像、小孩玩車、柴犬影片識別

 
 Visual C++ darknet opencv cuda yolo4測試即時影像、柴犬影片識別。「Vcpkg安裝darknet cuda並以C++ yolo v3 tiny辨識柴犬白狗動作影片」https://www.youtube.com/watch?v=ZODKCQABcKM

  1. #include <iostream>
  2. #include <fstream>
  3. #include <opencv2/opencv.hpp>
  4. #include <vector>
  5. using namespace cv;
  6. using namespace std;
  7. using namespace cv::dnn;
  8. vector<string> coco;
  9. void name_list() {
  10. fstream fin("coco.names", ios::in);
  11. string tmp = "";
  12. while (getline(fin, tmp))
  13. coco.push_back(tmp);
  14. fin.close();
  15. cout << coco.size() << endl;
  16. }
  17. int main()
  18. {
  19. cout << "參考https://pjreddie.com/darknet/yolo/ \n";
  20. name_list();
  21. cv::setUseOptimized(1);
  22. VideoCapture cap("shiba_turns_around.mp4");
  23. cap.set(CAP_PROP_FRAME_WIDTH, 1280);
  24. cap.set(CAP_PROP_FRAME_HEIGHT, 720);
  25. string model = "yolov4.weights";
  26. string config = "yolov4.cfg";
  27. Net network = readNet(model, config, "Darknet");
  28. network.setPreferableBackend(DNN_BACKEND_CUDA);
  29. network.setPreferableTarget(DNN_TARGET_CUDA);
  30. string wname = "Darknet yolo4 ";
  31. namedWindow(wname);
  32. Vec3b colors[] = { Vec3b(255,255,0), Vec3b(0, 255,0), Vec3b(0,255,255), Vec3b(255,0,255) };
  33. int i = 0;
  34. while (1)
  35. {
  36. Mat im;
  37. cap >> im;
  38. if (!im.data) break;
  39. Mat blobFromIm;
  40. bool swapRB = true;
  41. blobFromImage(im, blobFromIm, 1, Size(416, 416), Scalar(), swapRB, false);
  42. //Size(608, 608) or (416, 416)
  43. float scale = 1.0 / 255.0;
  44. Scalar mean = 0;
  45. network.setInput(blobFromIm, "", scale, mean);
  46.  
  47. Mat detection = network.forward();
  48. // rows represent number of detected objects
  49. int rowsNoOfDetection = detection.rows;
  50.  
  51. // [x ; y ; w; h; class 0 ; class 1 ; class 2 ;....]
  52. //
  53. int xywh_scores = detection.cols;
  54.  
  55. // Loop over number of detected object.
  56. for (int j = 0; j < rowsNoOfDetection; ++j)
  57. {
  58. // for each row, the score is from element 5 up to end
  59. Mat scores = detection.row(j).colRange(5, xywh_scores);
  60.  
  61. Point PositionOfMax;
  62. double confidence;
  63.  
  64. //This function find indexes of min and max confidence and related index.
  65. minMaxLoc(scores, 0, &confidence, 0, &PositionOfMax);
  66.  
  67. if (confidence > 0.01)
  68. {
  69. // [x ; y ; w; h;
  70. int x = (int)(detection.at<float>(j, 0) * im.cols);
  71. int y = (int)(detection.at<float>(j, 1) * im.rows);
  72. int w = (int)(detection.at<float>(j, 2) * im.cols);
  73. int h = (int)(detection.at<float>(j, 3) * im.rows);
  74.  
  75. int left = x - w / 2;
  76. int up = y - h / 2;
  77.  
  78. stringstream ss;
  79. int i = PositionOfMax.x;
  80. ss << coco[i] << ",prob=" << 100 * confidence << "%";
  81. string what = ss.str();
  82. cout << '#' << j << ":[" << x << ',' << y << ',' << w << ',' << h << "]:";
  83. cout<< what << endl;
  84. Vec3b color = confidence * colors[i % 4];
  85. rectangle(im, Rect(left, up, w, h), color, 2, LINE_8, 0);
  86.  
  87. int baseLine = 0;
  88. Size labelSize = getTextSize(what, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  89. rectangle(im, Rect(Point(left, up - labelSize.height),
  90. Size(labelSize.width, labelSize.height + baseLine)), color, -1);
  91. putText(im, what, Point(left, up), FONT_HERSHEY_SIMPLEX, 0.5, Scalar());
  92. }
  93. }
  94. imshow(wname, im);
  95. if (waitKey(1) == 27) {
  96. imwrite("doggy.png", im);
  97. break;
  98. }
  99. }
  100. destroyAllWindows();
  101. cap.release();
  102. return 0;
  103. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章