object_detection_yolo.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // This code is written at BigVision LLC. It is based on the OpenCV project. It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html
  2. // Usage example: ./object_detection_yolo.out --video=run.mp4
  3. // ./object_detection_yolo.out --image=bird.jpg
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <opencv2/dnn.hpp>
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/highgui.hpp>
  10. const char* keys =
  11. "{help h usage ? | | Usage examples: \n\t\t./object_detection_yolo.out --image=dog.jpg \n\t\t./object_detection_yolo.out --video=run_sm.mp4}"
  12. "{image i |<none>| input image }"
  13. "{video v |<none>| input video }"
  14. "{device d |<cpu>| input device }"
  15. ;
  16. using namespace cv;
  17. using namespace dnn;
  18. using namespace std;
  19. // Initialize the parameters
  20. float confThreshold = 0.5; // Confidence threshold
  21. float nmsThreshold = 0.4; // Non-maximum suppression threshold
  22. int inpWidth = 416; // Width of network's input image
  23. int inpHeight = 416; // Height of network's input image
  24. vector<string> classes;
  25. // Remove the bounding boxes with low confidence using non-maxima suppression
  26. void postprocess(Mat& frame, const vector<Mat>& out);
  27. // Draw the predicted bounding box
  28. void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
  29. // Get the names of the output layers
  30. vector<String> getOutputsNames(const Net& net);
  31. int main(int argc, char** argv)
  32. {
  33. CommandLineParser parser(argc, argv, keys);
  34. parser.about("Use this script to run object detection using YOLO3 in OpenCV.");
  35. if (parser.has("help"))
  36. {
  37. parser.printMessage();
  38. return 0;
  39. }
  40. // Load names of classes
  41. string classesFile = "coco.names";
  42. ifstream ifs(classesFile.c_str());
  43. string line;
  44. while (getline(ifs, line)) classes.push_back(line);
  45. string device = "cpu";
  46. device = parser.get<String>("device");
  47. // Give the configuration and weight files for the model
  48. String modelConfiguration = "yolov3.cfg";
  49. String modelWeights = "yolov3.weights";
  50. // Load the network
  51. Net net = readNetFromDarknet(modelConfiguration, modelWeights);
  52. if (device == "cpu")
  53. {
  54. cout << "Using CPU device" << endl;
  55. net.setPreferableBackend(DNN_TARGET_CPU);
  56. }
  57. else if (device == "gpu")
  58. {
  59. cout << "Using GPU device" << endl;
  60. net.setPreferableBackend(DNN_BACKEND_CUDA);
  61. net.setPreferableTarget(DNN_TARGET_CUDA);
  62. }
  63. // Open a video file or an image file or a camera stream.
  64. string str, outputFile;
  65. VideoCapture cap;
  66. VideoWriter video;
  67. Mat frame, blob;
  68. try {
  69. outputFile = "yolo_out_cpp.avi";
  70. if (parser.has("image"))
  71. {
  72. // Open the image file
  73. str = parser.get<String>("image");
  74. ifstream ifile(str);
  75. if (!ifile) throw("error");
  76. cap.open(str);
  77. str.replace(str.end()-4, str.end(), "_yolo_out_cpp.jpg");
  78. outputFile = str;
  79. }
  80. else if (parser.has("video"))
  81. {
  82. // Open the video file
  83. str = parser.get<String>("video");
  84. ifstream ifile(str);
  85. if (!ifile) throw("error");
  86. cap.open(str);
  87. str.replace(str.end()-4, str.end(), "_yolo_out_cpp.avi");
  88. outputFile = str;
  89. }
  90. // Open the webcaom
  91. else cap.open(parser.get<int>("device"));
  92. }
  93. catch(...) {
  94. cout << "Could not open the input image/video stream" << endl;
  95. return 0;
  96. }
  97. // Get the video writer initialized to save the output video
  98. if (!parser.has("image")) {
  99. video.open(outputFile, VideoWriter::fourcc('M','J','P','G'), 28, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)));
  100. }
  101. // Create a window
  102. static const string kWinName = "Deep learning object detection in OpenCV";
  103. namedWindow(kWinName, WINDOW_NORMAL);
  104. // Process frames.
  105. while (waitKey(1) < 0)
  106. {
  107. // get frame from the video
  108. cap >> frame;
  109. // Stop the program if reached end of video
  110. if (frame.empty()) {
  111. cout << "Done processing !!!" << endl;
  112. cout << "Output file is stored as " << outputFile << endl;
  113. waitKey(3000);
  114. break;
  115. }
  116. // Create a 4D blob from a frame.
  117. blobFromImage(frame, blob, 1/255.0, cv::Size(inpWidth, inpHeight), Scalar(0,0,0), true, false);
  118. //Sets the input to the network
  119. net.setInput(blob);
  120. // Runs the forward pass to get output of the output layers
  121. vector<Mat> outs;
  122. net.forward(outs, getOutputsNames(net));
  123. // Remove the bounding boxes with low confidence
  124. postprocess(frame, outs);
  125. // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
  126. vector<double> layersTimes;
  127. double freq = getTickFrequency() / 1000;
  128. double t = net.getPerfProfile(layersTimes) / freq;
  129. string label = format("Inference time for a frame : %.2f ms", t);
  130. putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
  131. // Write the frame with the detection boxes
  132. Mat detectedFrame;
  133. frame.convertTo(detectedFrame, CV_8U);
  134. if (parser.has("image")) imwrite(outputFile, detectedFrame);
  135. else video.write(detectedFrame);
  136. imshow(kWinName, frame);
  137. }
  138. cap.release();
  139. if (!parser.has("image")) video.release();
  140. return 0;
  141. }
  142. // Remove the bounding boxes with low confidence using non-maxima suppression
  143. void postprocess(Mat& frame, const vector<Mat>& outs)
  144. {
  145. vector<int> classIds;
  146. vector<float> confidences;
  147. vector<Rect> boxes;
  148. for (size_t i = 0; i < outs.size(); ++i)
  149. {
  150. // Scan through all the bounding boxes output from the network and keep only the
  151. // ones with high confidence scores. Assign the box's class label as the class
  152. // with the highest score for the box.
  153. float* data = (float*)outs[i].data;
  154. for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
  155. {
  156. Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
  157. Point classIdPoint;
  158. double confidence;
  159. // Get the value and location of the maximum score
  160. minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
  161. if (confidence > confThreshold)
  162. {
  163. int centerX = (int)(data[0] * frame.cols);
  164. int centerY = (int)(data[1] * frame.rows);
  165. int width = (int)(data[2] * frame.cols);
  166. int height = (int)(data[3] * frame.rows);
  167. int left = centerX - width / 2;
  168. int top = centerY - height / 2;
  169. classIds.push_back(classIdPoint.x);
  170. confidences.push_back((float)confidence);
  171. boxes.push_back(Rect(left, top, width, height));
  172. }
  173. }
  174. }
  175. // Perform non maximum suppression to eliminate redundant overlapping boxes with
  176. // lower confidences
  177. vector<int> indices;
  178. NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
  179. for (size_t i = 0; i < indices.size(); ++i)
  180. {
  181. int idx = indices[i];
  182. Rect box = boxes[idx];
  183. drawPred(classIds[idx], confidences[idx], box.x, box.y,
  184. box.x + box.width, box.y + box.height, frame);
  185. }
  186. }
  187. // Draw the predicted bounding box
  188. void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
  189. {
  190. //Draw a rectangle displaying the bounding box
  191. rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
  192. //Get the label for the class name and its confidence
  193. string label = format("%.2f", conf);
  194. if (!classes.empty())
  195. {
  196. CV_Assert(classId < (int)classes.size());
  197. label = classes[classId] + ":" + label;
  198. }
  199. //Display the label at the top of the bounding box
  200. int baseLine;
  201. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  202. top = max(top, labelSize.height);
  203. rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
  204. putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,0),1);
  205. }
  206. // Get the names of the output layers
  207. vector<String> getOutputsNames(const Net& net)
  208. {
  209. static vector<String> names;
  210. if (names.empty())
  211. {
  212. //Get the indices of the output layers, i.e. the layers with unconnected outputs
  213. vector<int> outLayers = net.getUnconnectedOutLayers();
  214. //get the names of all the layers in the network
  215. vector<String> layersNames = net.getLayerNames();
  216. // Get the names of the output layers in names
  217. names.resize(outLayers.size());
  218. for (size_t i = 0; i < outLayers.size(); ++i)
  219. names[i] = layersNames[outLayers[i] - 1];
  220. }
  221. return names;
  222. }