---
# System prepended metadata

title: Cuda_video_streaming

---

# Cuda_video_streaming

This package requires opencv built with cuda.

## Ensure NVDEC and NVENC are built on your Machine.

Install
In order to install Video Codec SDK simply extract files from downloaded archive and move includes & libs to your cuda path (ex.: usr/local/cuda/). For example:

unzip Video_Codec_SDK.zip
cd Video_Codec_SDK
```
cp include/* /usr/local/cuda/include
cp Lib/linux/stubs/x86_64/* /usr/local/cuda/lib64/stubs
```

[USE THIS LINK ](https://gist.github.com/corenel/a615b6f7eb5b5425aa49343a7b409200)

There is another Complete  Useful Guide  [Here](https://medium.com/@pydoni/how-to-install-cuda-11-4-cudnn-8-2-opencv-4-5-on-ubuntu-20-04-65c4aa415a7b)

**This guide is Complete for getting GPU functional and trying out OpenCV GPU Code Examples !!**

**Recommended to follow this Guide.**




## Build Open CV with CUDA and OPEN GL

You probably forgot to install the GTK+ OpenGL Extensions and their headers

sudo apt-get install libgtkglext1 libgtkglext1-dev


https://yunusmuhammad007.medium.com/build-and-install-opencv-4-5-3-on-jetson-nano-with-cuda-opencl-opengl-and-gstreamer-enable-6dc7141be272

## Hacks tried for building with Open Gl and Without GTK, as there are several issues, currently **This Build Configuration Works !!** 




```bash
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-10 -DCMAKE_INSTALL_PREFIX=/usr/local  -D INSTALL_PYTHON_EXAMPLES=ON  -D INSTALL_C_EXAMPLES=OFF  -D WITH_TBB=ON   -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.4    -D  OpenCL_LIBRARY=/usr/local/cuda-11.4/lib64/libOpenCL.so   -D OpenCL_INCLUDE_DIR=/usr/local/cuda-11.4/include/      -D WITH_CUDA=ON  -D WITH_CUDNN=ON  -D OPENCV_DNN_CUDA=ON  -D CUDA_ARCH_BIN=8.6    -D BUILD_opencv_cudacodec=ON  -D ENABLE_FAST_MATH=1   -D CUDA_FAST_MATH=1  -D WITH_CUBLAS=1   -D WITH_V4L=ON    -D WITH_QT=OFF   -D WITH_OPENGL=ON -D WITH_GTK=OFF     -D WITH_GSTREAMER=ON  -D OPENCV_GENERATE_PKGCONFIG=ON   -D OPENCV_PC_FILE_NAME=opencv.pc   -D OPENCV_ENABLE_NONFREE=ON  -D BUILD_opencv_python3=ON   -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules   -D BUILD_EXAMPLES=ON   -D WITH_FFMPEG=OFF  -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF  -D BUILD_SHARED_LIBS=ON  -D OpenGL_GL_PREFERENCE=GLVND -D BUILD_opencv_rgbd=OFF -D WITH_NVCUVID=ON  -D WITH_QT=5 ..

```








###  Compiling Open CV with CUDA

**Before** running the script follow these steps.
1. Make a opencv folder and clone opencv 4.6 and opencv_contrib 4.6 under opencv folder in your desired location **(home/opencv)** or the latest version.
2. Alter the path of opencv_contrib as per your config in the below script as per your config.
3. Find out CUDA Compute Score and alter in build options in the below script.
4. Install latest gcc and g++ compiler and alter path in build options.
5. Install CUDA Toolkit and alter path in build options.


[Opencv_GPU_build_Script](https://gist.github.com/IshanBhatnagar14/b4a72755a7274f4f8ce4d9c28d8db6b8)


###### tags: OpenCV_GPU_Buildhttps://hackmd.io/dOOTh8hzSOaHst6LaDLxww#

```bash
#!/bin/bash

sudo apt update
sudo apt upgrade

sudo apt install -y gcc-10 g++-10
sudo apt install -y build-essential cmake pkg-config yasm git checkinstall
sudo apt install -y libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libgtkglext1 libgtkglext1-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev 
sudo apt install -y libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev 
sudo apt install -y libfaac-dev libmp3lame-dev libvorbis-dev
sudo apt-get install -y libavresample-dev libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils

mkdir opencv_build && cd opencv_build
wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.6.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.6.zip
unzip opencv.zip
unzip opencv_contrib.zip
echo "Moving onto the build portion of things"
cd opencv-4.6
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-10 -DCMAKE_INSTALL_PREFIX=/usr/local  -D INSTALL_PYTHON_EXAMPLES=ON  -D INSTALL_C_EXAMPLES=OFF  -D WITH_TBB=ON   -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.4    -D  OpenCL_LIBRARY=/usr/local/cuda-11.4/lib64/libOpenCL.so   -D OpenCL_INCLUDE_DIR=/usr/local/cuda-11.4/include/      -D WITH_CUDA=ON  -D WITH_CUDNN=ON  -D OPENCV_DNN_CUDA=ON  -D CUDA_ARCH_BIN=8.6    -D BUILD_opencv_cudacodec=ON  -D ENABLE_FAST_MATH=1   -D CUDA_FAST_MATH=1  -D WITH_CUBLAS=1   -D WITH_V4L=ON    -D WITH_QT=OFF   -D WITH_OPENGL=ON -D WITH_GTK=OFF     -D WITH_GSTREAMER=ON  -D OPENCV_GENERATE_PKGCONFIG=ON   -D OPENCV_PC_FILE_NAME=opencv.pc   -D OPENCV_ENABLE_NONFREE=ON  -D BUILD_opencv_python3=ON   -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules   -D BUILD_EXAMPLES=ON   -D WITH_FFMPEG=OFF  -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF  -D BUILD_SHARED_LIBS=ON  -D OpenGL_GL_PREFERENCE=GLVND -D BUILD_opencv_rgbd=OFF -D WITH_NVCUVID=ON  -D WITH_QT=5 ..
# cmake -D CMAKE_BUILD_TYPE=RELEASE 
#       -D CMAKE_C_COMPILER=/usr/bin/gcc-10 
#       -D CMAKE_INSTALL_PREFIX=/usr/local 
#       -D INSTALL_PYTHON_EXAMPLES=ON 
#       -D INSTALL_C_EXAMPLES=OFF 
#       -D WITH_TBB=ON  
#       -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.4   
#       -D  OpenCL_LIBRARY=/usr/local/cuda-11.4/lib64/libOpenCL.so 
#       -D OpenCL_INCLUDE_DIR=/usr/local/cuda-11.4/include/ 
#       -D WITH_CUDA=ON 
#       -D WITH_CUDNN=ON 
#       -D OPENCV_DNN_CUDA=ON 
#       -D CUDA_ARCH_BIN=8.6  
#       -D BUILD_opencv_cudacodec=OFF 
#       -D ENABLE_FAST_MATH=1 
#       -D CUDA_FAST_MATH=1 
#       -D WITH_CUBLAS=1 
#       -D WITH_V4L=ON 
#       -D WITH_QT=OFF 
#       -D WITH_OPENGL=ON 
#       -D WITH_GSTREAMER=ON 
#       -D OPENCV_GENERATE_PKGCONFIG=ON 
#       -D OPENCV_PC_FILE_NAME=opencv.pc 
#       -D OPENCV_ENABLE_NONFREE=ON 
#       -D BUILD_opencv_python3=ON  
#       -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules  
#       -D BUILD_EXAMPLES=ON 
#       -D WITH_FFMPEG=OFF 
#       -D BUILD_PERF_TESTS=OFF 
#       -D BUILD_TESTS=OFF 
#       -D BUILD_SHARED_LIBS=ON ..
      
echo "Configuring build & making OpenCV"

make -j8
sudo make install
```

#### lenovo-Ideapad 3 (GTX 1650, Ryzen 7) build config

```bash=
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc-9 \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.7 \
-D  OpenCL_LIBRARY=/usr/local/cuda-11.7/lib64/libOpenCL.so \
-D OpenCL_INCLUDE_DIR=/usr/local/cuda-11.7/include/ \
-D WITH_CUDA=ON \
-D WITH_CUDNN=OFF \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.6 \
-D BUILD_opencv_cudacodec=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GTK=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.6.0/modules \
-D BUILD_EXAMPLES=ON \
-D WITH_FFMPEG=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_SHARED_LIBS=ON \
-D OpenGL_GL_PREFERENCE=GLVND \
-D BUILD_opencv_rgbd=OFF \
-D WITH_NVCUVID=OFF \
-D WITH_QT=5 ..
```