title: Opencv how to read webcam stream via on GPU?
body: <p>I can use <a href="https://docs.opencv.org/4.3.0/db/ded/classcv_1_1cudacodec_1_1VideoReader.html" rel="nofollow noreferrer">VideoReader class</a> of OpenCV to decode an IP camera stream or any video file by using its path. This decoding process is using GPU as expected, no problem up to now. Here is simple code which is working fine and using GPU for decoding:</p>
  <pre><code>int main()
  {
      const std::string fname(&quot;rtsp://user:pwd@192.168.1.108&quot;); 
      // const std::string fname(&quot;/path/to/video/file.mp4&quot;); // this also works    
      cv::cuda::GpuMat d_frame;
      cv::Ptr&lt;cv::cudacodec::VideoReader&gt; d_reader = cv::cudacodec::createVideoReader(fname);    
      Mat frame;        
      for (;;)
      {
  
          if (!d_reader-&gt;nextFrame(d_frame))
              break;    
          Mat myMat(d_frame);            
          cv::imshow(&quot;GPU&quot;, myMat);
  
          if (cv::waitKey(3) &gt; 0)
              break;
      }
  
      return 0;
  }
  </code></pre>
  <p>I want to use GPU to capture streams from my webcam as like <code>VideoCapture(0)</code>. I know as @berak mentioned <a href="https://answers.opencv.org/question/226410/decode-with-multiple-gpus-how-can-i-specify-whichi-gpu-to-use-in-cvvideocapture/" rel="nofollow noreferrer">here</a> : There is no way to do that with <code>VideoCapture</code></p>
  <h2>My questions are:</h2>
  <p>1 - Is it possible to stream by using GPU with VideoReader class? Because VideoReader class only accepts strings not indexes.</p>
  <p>2- What are the other ways to be able to stream by using GPU?</p>