I am trying to build OpenCV with CUDA support, it worked but now I wanted to add cudacodec as well, however I am having issues with installing the NVIDIA Video Codec SDK and get errors for the headers and libraries. I am building it on a Modal Image (similar to docker) for Cloud GPU use, so if anyone has any suggestions on how to get it working I’d be thankful. I even tried getting the .h files from PyNvVideoCodec.
image = (
modal.Image.debian_slim(python_version="3.10")
.apt_install(
[
"wget",
"unzip",
"ffmpeg",
"libgl1-mesa-glx",
"libglib2.0-0",
"build-essential",
"cmake",
"pkg-config",
"libgtk-3-dev",
"libavcodec-dev",
"libavformat-dev",
"libswscale-dev",
"libv4l-dev",
"libxvidcore-dev",
"libx264-dev",
"ninja-build",
"git",
"libatlas-base-dev", # Fix BLAS/LAPACK issue
"libgflags-dev", # Fix gflags issue
"libgoogle-glog-dev", # Fix glog issue
"libhdf5-dev", # Recommended for OpenCV ML/DNN modules
"ca-certificates",
]
)
.pip_install(
[
"numpy", # Fix missing NumPy error
"moviepy==1.0.3",
"Pillow",
"spacy==3.7.2",
"pandas",
]
)
.run_commands(
# CUDA Installation
"wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run",
"sh cuda_12.1.0_530.30.02_linux.run --toolkit --silent --override",
# cuDNN Installation
"mkdir -p /cudnn",
"cd /cudnn",
"wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz -O cudnn.tar.xz --no-check-certificate",
"tar -xf cudnn.tar.xz",
"cp cudnn-linux-x86_64-8.9.7.29_cuda12-archive/include/cudnn*.h /usr/local/cuda/include",
"cp -P cudnn-linux-x86_64-8.9.7.29_cuda12-archive/lib/libcudnn* /usr/local/cuda/lib64",
"chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*",
# NVIDIA VIDEO HEADERS
"wget --content-disposition 'https://api.ngc.nvidia.com/v2/resources/org/nvidia/pynvvideocodec/1.0.2/files?redirect=true&path=PyNvVideoCodec_1.0.2.zip' -O PyNvVideoCodec_1.0.2.zip && unzip PyNvVideoCodec_1.0.2.zip -d ~/PyNvVideoCodec && mkdir -p /usr/local/include/nvidia-video-codec-sdk && cp -r ~/PyNvVideoCodec/src/VideoCodecSDKUtils/Interface/*.h /usr/local/include/nvidia-video-codec-sdk/",
# OpenCV Installation
"git clone https://github.com/opencv/opencv.git",
"git clone https://github.com/opencv/opencv_contrib.git",
"mkdir -p opencv/build",
"""cd opencv/build && cmake -GNinja \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 8.9 \
-D CUDA_ARCH_PTX=8.9 \
-D WITH_CUDNN=ON \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
-D OPENCV_DNN_CUDA=ON \
-D WITH_NVCUVID=ON \
-D WITH_NVCUVENC=ON \
-D BUILD_OPENCV_CUDACODEC=ON \
-D CUDA_nvcuvid_INCLUDE_DIR=/usr/local/include/nvidia-video-codec-sdk \
-D CUDA_nvcuvid_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvcuvid.so.1 \
-D CUDA_nvidia_encode_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvidia-encode.so.1 \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_V4L=ON \
-D WITH_FFMPEG=ON \
-D BUILD_opencv_python3=ON \
-D PYTHON_EXECUTABLE=/usr/local/bin/python \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
..""",
"cd opencv/build && ninja",
"cd opencv/build && ninja install",
"ldconfig",
"python -m spacy download es_core_news_sm",
"export PATH=/usr/local/cuda/bin:$PATH && export CUDA_ROOT=/usr/local/cuda && export CUDA_INC_DIR=/usr/local/cuda/include && export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH && pip install 'pycuda>=2022.1'",
)
)