Nick Saw
Nick Saw

Reputation: 507

Howto OPENCV+CUDA+VideoCapture?

I have a machine with 2 Nvidia RTX GPU. The main task is to decode an h264 video from an IP camera and encode raw frames to JPEG with GPU.

I built opencv + cuda from sources by:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_QT=OFF -D WITH_NVCUVID -D WITH_NVCUVID=ON -D CMAKE_C_COMPILER=/usr/bin/gcc-6 ..
make
sudo make install

I have a simple program which captures RTSP and saves JPEGs:

#include <strings.h>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp>

using namespace std;

int main(int argc, char** argv)
{
    if ( argc != 2 ) {
      printf("no args\n");
      return -1;
    }
    cv::Mat frame;
    cv::VideoCapture cap;
    cap.open(argv[1]);
    if (!cap.isOpened()) {
        printf("ERROR! Unable to open camera\n");
        return -1;
    }
    printf("Start grabbing\n");
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            printf("ERROR! blank frame grabbed\n");
            break;
        }
        vector<int> p;
        vector<unsigned char> buf;
        p.push_back(cv::IMWRITE_JPEG_QUALITY);
        p.push_back(50);
        imencode(".jpg", frame, buf, p);
        // Do something with buf ......
        printf("READY JPEG\n");
    }
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( test_jpeg_cpu )
find_package( OpenCV REQUIRED )
find_package( CUDA REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} ${CUDA_INCLUDE_DIRS} )
add_executable( test_jpeg_cpu test_jpeg_cpu.cpp )
target_link_libraries( test_jpeg_cpu ${OpenCV_LIBS} ${CUDA_LIBRARIES} cuda ${CMAKE_DL_LIBS} )

But it uses only CPU. What do I have to change to make this program use GPU?

Thanks in advance.

Upvotes: 0

Views: 8075

Answers (1)

gavinb
gavinb

Reputation: 20018

You're using the imwrite API which uses libjpeg on the back end for CPU based encoding. If you want to use the GPU, you'll need to use the cv::cudacodec::createVideoWriter API (assuming it has been built into your OpenCV installation).

There is example code for GPU-accelerated video encoding in the OpenCV source repo.

Upvotes: 3

Related Questions