title: Use YoloV3 to detect objects in C++ project VS
body: <p>I follow one topic to install yolo for c++ project (<a href="https://stackoverflow.com/questions/61286151/include-darknet-yolov3-for-object-detection/62638921#62638921">Include darknet Yolov3 for object detection</a>), but I have one more question : I try to launch Yolov3 in my c++ project. I built the yolo_cpp_dll_no_gpu.sln (I don't have GPU), so I have darknet_no_gpu.exe</p>
  <p>I would like to detect some objects on my frame from my webcam. I obtain my frame, but I don't know how to launch yolo detection in c++ project. Maybe it's similar to python commands, but I don't find the c++ syntax...</p>
  <p>Can you help me please ?</p>
  <p>edit: (I give more details)</p>
  <ul>
  <li><p>I need to use YoloV3 for hand detection on a c++ project on visual studio 2019.</p>
  </li>
  <li><p>So I trained Yolov3 with python commands. I obtained my .weights file and the detection works when I launch this command on the cmd :</p>
  <blockquote>
  <p>darknet_no_gpu detector demo data/obj.data cfg/yolov3-tiny.cfg yolov3-tiny_last.weights</p>
  </blockquote>
  </li>
  <li><p>Now, I would like to use this detection in VS project. I built <strong>yolo_cpp_dll_no_gpu.sln</strong> to obtain <strong>darknet_no_gpu.exe</strong> <a href="https://i.stack.imgur.com/0wM7y.png" rel="nofollow noreferrer">yolo cpp dll no gpu</a></p>
  </li>
  <li><p>In my project, I had the path of the include and library in the properties <a href="https://i.stack.imgur.com/OwEwI.png" rel="nofollow noreferrer">path in properties</a> and the dependence <a href="https://i.stack.imgur.com/4h6wR.png" rel="nofollow noreferrer">dependence</a></p>
  </li>
  </ul>
  <p>So, my question is, what is the syntax to use yolo on my frame: `</p>
  <pre><code>VideoCapture cap(0); // open camera
      if (!cap.isOpened())  
          return -1;
      cap.set(CAP_PROP_FRAME_WIDTH, 320);
      cap.set(CAP_PROP_FRAME_HEIGHT, 240);
  
      for (;;) {
          cap &gt;&gt; frame;
  
          if (frame.empty())
          break;
  </code></pre>
  <p>`
  because I can't use this one in VS</p>
  <pre><code>darknet_no_gpu detector demo data/obj.data cfg/yolov3-tiny.cfg yolov3-tiny_last.weights frame
  </code></pre>
  <p><strong>edit 2:</strong>
  Hello,
  After some research with your help, I have 1 more problem...
  Here is my code:</p>
  <pre><code>    #include &lt;Windows.h&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  
  #include &lt;opencv2/dnn.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  
  using namespace cv;
  using namespace dnn;
  using namespace std;
  
  // Initialize the parameters
  float confThreshold = 0.5; // Confidence threshold
  float nmsThreshold = 0.4;  // Non-maximum suppression threshold
  int inpWidth = 416;  // Width of network's input image
  int inpHeight = 416; // Height of network's input image
  vector&lt;string&gt; classes;
  
  // Remove the bounding boxes with low confidence using non-maxima suppression
  void postprocess(Mat&amp; frame, const vector&lt;Mat&gt;&amp; out);
  
  // Draw the predicted bounding box
  void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat&amp; frame);
  
  // Get the names of the output layers
  vector&lt;String&gt; getOutputsNames(const Net&amp; net);
  
  int main(int argc, char** argv)
  {
      // Load names of classes
      string classesFile = &quot;obj.names&quot;;
      ifstream ifs(classesFile.c_str());
      string line;
      while (getline(ifs, line)) classes.push_back(line);
  
      // Give the configuration and weight files for the model
      String modelConfiguration = &quot;yolov3-tiny.cfg&quot;;
      String modelWeights = &quot;yolov3-tiny_last.weights&quot;;
  
      // Load the network
      Net net = readNetFromDarknet(modelConfiguration, modelWeights);
      net.setPreferableBackend(DNN_BACKEND_OPENCV);
      net.setPreferableTarget(DNN_TARGET_CPU);
  
      // Open a video file or an image file or a camera stream.
      string outputFile;
      //VideoCapture cap;
      VideoWriter video;
      Mat frame;
      Mat blob, ex;
  
      VideoCapture cap(0); // ouvre la camera par défaut
      if (!cap.isOpened())  // check si succès
          return -1;  
  
      for (;;) {
          // get frame from the video
          cap &gt;&gt; frame;
          cvtColor(frame, ex, COLOR_BGR2YCrCb);
  
          cout &lt;&lt; frame.size &lt;&lt; endl;
          imshow(&quot;e&quot;, ex);
          // Stop the program if reached end of video
          if (frame.empty()) {
              cout &lt;&lt; &quot;Done processing !!!&quot; &lt;&lt; endl;
              cout &lt;&lt; &quot;Output file is stored as &quot; &lt;&lt; outputFile &lt;&lt; endl;
              waitKey(3000);
              break;
          }
          // Create a 4D blob from a frame.
          blobFromImage(frame, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
  
          //Sets the input to the network
          net.setInput(blob);
  
          // Runs the forward pass to get output of the output layers
          vector&lt;Mat&gt; outs;
          net.forward(outs, getOutputsNames(net));
  
          // Remove the bounding boxes with low confidence
          postprocess(frame, outs);
  
          // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
          vector&lt;double&gt; layersTimes;
          double freq = getTickFrequency() / 1000;
          double t = net.getPerfProfile(layersTimes) / freq;
          string label = format(&quot;Inference time for a frame : %.2f ms&quot;, t);
          putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
  
          // Write the frame with the detection boxes
          Mat detectedFrame;
          frame.convertTo(detectedFrame, CV_8U);
  
          imshow(&quot;f&quot;, frame);
  
          Sleep(3000);
  
      }
  
      cap.release();
  
      return 0;
  }
  
  // Remove the bounding boxes with low confidence using non-maxima suppression
  void postprocess(Mat&amp; frame, const vector&lt;Mat&gt;&amp; outs)
  {
      vector&lt;int&gt; classIds;
      vector&lt;float&gt; confidences;
      vector&lt;Rect&gt; boxes;
  
      for (size_t i = 0; i &lt; outs.size(); ++i)
      {
          // Scan through all the bounding boxes output from the network and keep only the
          // ones with high confidence scores. Assign the box's class label as the class
          // with the highest score for the box.
          float* data = (float*)outs[i].data;
          for (int j = 0; j &lt; outs[i].rows; ++j, data += outs[i].cols)
          {
              Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
              Point classIdPoint;
              double confidence;
              // Get the value and location of the maximum score
              minMaxLoc(scores, 0, &amp;confidence, 0, &amp;classIdPoint);
              if (confidence &gt; confThreshold)
              {
                  int centerX = (int)(data[0] * frame.cols);
                  int centerY = (int)(data[1] * frame.rows);
                  int width = (int)(data[2] * frame.cols);
                  int height = (int)(data[3] * frame.rows);
                  int left = centerX - width / 2;
                  int top = centerY - height / 2;
  
                  classIds.push_back(classIdPoint.x);
                  confidences.push_back((float)confidence);
                  boxes.push_back(Rect(left, top, width, height));
              }
          }
      }
  
      // Perform non maximum suppression to eliminate redundant overlapping boxes with
      // lower confidences
      vector&lt;int&gt; indices;
      NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
      for (size_t i = 0; i &lt; indices.size(); ++i)
      {
          int idx = indices[i];
          Rect box = boxes[idx];
          drawPred(classIds[idx], confidences[idx], box.x, box.y,
              box.x + box.width, box.y + box.height, frame);
      }
  }
  
  // Draw the predicted bounding box
  void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat&amp; frame)
  {
      //Draw a rectangle displaying the bounding box
      rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
  
      //Get the label for the class name and its confidence
      string label = format(&quot;%.2f&quot;, conf);
      if (!classes.empty())
      {
          CV_Assert(classId &lt; (int)classes.size());
          label = classes[classId] + &quot;:&quot; + label;
      }
  
      //Display the label at the top of the bounding box
      int baseLine;
      Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &amp;baseLine);
      top = max(top, labelSize.height);
      rectangle(frame, Point(left, top - round(1.5 * labelSize.height)), Point(left + round(1.5 * labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
      putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
  }
  
  // Get the names of the output layers
  vector&lt;String&gt; getOutputsNames(const Net&amp; net)
  {
      static vector&lt;String&gt; names;
      if (names.empty())
      {
          //Get the indices of the output layers, i.e. the layers with unconnected outputs
          vector&lt;int&gt; outLayers = net.getUnconnectedOutLayers();
  
          //get the names of all the layers in the network
          vector&lt;String&gt; layersNames = net.getLayerNames();
  
          // Get the names of the output layers in names
          names.resize(outLayers.size());
          for (size_t i = 0; i &lt; outLayers.size(); ++i)
              names[i] = layersNames[outLayers[i] - 1];
      }
      return names;
  }
  </code></pre>
  <p>So it would be working, but I obtain a gray windows with imshow. Like on the picture <a href="https://i.stack.imgur.com/4PemO.png" rel="nofollow noreferrer">gray window</a></p>