【问题标题】:Opencv how to read webcam stream via on GPU?Opencv如何通过GPU读取网络摄像头流?
【发布时间】:2020-11-17 15:36:22
【问题描述】:

我可以使用 OpenCV 的 VideoReader class 通过其路径解码 IP 摄像机流或任何视频文件。此解码过程按预期使用GPU,到目前为止没有问题。这是运行良好并使用 GPU 进行解码的简单代码:

int main()
{
    const std::string fname("rtsp://user:pwd@192.168.1.108"); 
    // const std::string fname("/path/to/video/file.mp4"); // this also works    
    cv::cuda::GpuMat d_frame;
    cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);    
    Mat frame;        
    for (;;)
    {

        if (!d_reader->nextFrame(d_frame))
            break;    
        Mat myMat(d_frame);            
        cv::imshow("GPU", myMat);

        if (cv::waitKey(3) > 0)
            break;
    }

    return 0;
}

我想像 VideoCapture(0) 一样使用 GPU 从我的网络摄像头捕获流。我知道@berak 提到了hereVideoCapture 没有办法做到这一点

我的问题是:

1 - 是否可以使用带有 VideoReader 类的 GPU 进行流式传输?因为 VideoReader 类只接受字符串而不接受索引。

2- 使用 GPU 进行流式传输的其他方法有哪些?

【问题讨论】:

    标签: c++ opencv video-streaming gpu webcam


    【解决方案1】:

    1) 是的,看起来是这样!我在 openCV GPU 示例here 中找到了以下代码。你可以试一试。不过,您需要使用 OpenGL 构建 OpenCV……目前这就是我遇到的问题。

    2)我不确定其他选项,但这里是来自 Github 的代码。

    #include <iostream>
    
    #include "opencv2/opencv_modules.hpp"
    
    #if defined(HAVE_OPENCV_CUDACODEC)
    
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    
    #include <opencv2/core.hpp>
    #include <opencv2/core/opengl.hpp>
    #include <opencv2/cudacodec.hpp>
    #include <opencv2/highgui.hpp>
    
    int main(int argc, const char* argv[])
    {
        std::cout << "Starting,...\n";
        const std::string fname = "0";
        
        cv::namedWindow("CPU", cv::WINDOW_NORMAL);
        cv::namedWindow("GPU", cv::WINDOW_OPENGL);
        cv::cuda::setGlDevice();
    
        cv::Mat frame;
        cv::VideoCapture reader(fname);
    
        cv::cuda::GpuMat d_frame;
        cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
    
        cv::TickMeter tm;
        std::vector<double> cpu_times;
        std::vector<double> gpu_times;
    
        int gpu_frame_count=0, cpu_frame_count=0;
    
        for (;;)
        {
            tm.reset(); tm.start();
            if (!reader.read(frame))
                break;
            tm.stop();
            cpu_times.push_back(tm.getTimeMilli());
            cpu_frame_count++;
    
            cv::imshow("CPU", frame);
    
            if (cv::waitKey(3) > 0)
                break;
        }
    
        for (;;)
        {
            tm.reset(); tm.start();
            if (!d_reader->nextFrame(d_frame))
                break;
            tm.stop();
            gpu_times.push_back(tm.getTimeMilli());
            gpu_frame_count++;
    
            cv::imshow("GPU", d_frame);
    
            if (cv::waitKey(3) > 0)
                break;
        }
    
        if (!cpu_times.empty() && !gpu_times.empty())
        {
            std::cout << std::endl << "Results:" << std::endl;
    
            std::sort(cpu_times.begin(), cpu_times.end());
            std::sort(gpu_times.begin(), gpu_times.end());
    
            double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
            double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
    
            std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << " Frames " << cpu_frame_count << std::endl;
            std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << " Frames " << gpu_frame_count << std::endl;
        }
    
        return 0;
    }
    
    #else
    
    int main()
    {
        std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
        return 0;
    }
    
    #endif
    

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2017-04-03
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多