Skip to content

Instantly share code, notes, and snippets.

@madtunebk
Last active June 20, 2024 05:49
Show Gist options
  • Save madtunebk/5f20437725eb0e0cfc2a4934153b0ab4 to your computer and use it in GitHub Desktop.
Save madtunebk/5f20437725eb0e0cfc2a4934153b0ab4 to your computer and use it in GitHub Desktop.
Build OPENCV VERSION 4.10.0 on: Ubuntu 22.04, GPU Driver Version: 550.78, CUDA Version: 12.4, GCC-10

Build OPENCV VERSION 4.10.0 on: Ubuntu 22.04, GPU Driver Version: 550.78, CUDA Version: 12.4, GCC-10

Create a Project Folder: ~/project

mkdir ~/project  
cd ~/project/    

Pre-checks Before Proceeding

nvidia-smi -L
nvcc --version  
python3 --version

Install NVIDIA Drivers and Utilities if nvidia-smi -L Fails

sudo apt install nvidia-driver-550 nvidia-utils-550
# A system reboot will be necessary if you install the GPU driver.
sudo reboot
# After system reboot, try again
nvidia-smi -L

Install CUDA Toolkit if nvcc --version Fails

sudo apt install nvidia-cuda-toolkit

Install cuDNN 9.1.1:

Network Install

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt -y install cudnn9-cuda-12

Local Install

wget https://developer.download.nvidia.com/compute/cudnn/9.1.1/local_installers/cudnn-local-repo-ubuntu2004-9.1.1_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2004-9.1.1_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2004-9.1.1/cudnn-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt -y install cudnn9-cuda-12

Install Video Codec SDK 12.2 Header Files

You will need an Nvidia account to download these files from: NVIDIA Video Codec SDK.

unzip Video_Codec_Interface_12.2.72.zip
sudo cp ~/project/Video_Codec_Interface_12.2.72/Interface/*.h /usr/include/

Note: The following files are no longer needed and can be safely removed:

  • Video_Codec_Interface_12.2.72
  • Video_Codec_Interface_12.2.72.zip
  • cudnn-local-repo-ubuntu2004-9.1.1_1.0-1_amd64.deb
rm -rf ~/project/Video_Codec_Interface_12.2.72 ~/project/Video_Codec_Interface_12.2.72.zip ~/project/cudnn-local-repo-ubuntu2004-9.1.1_1.0-1_amd64.deb

Install CMake and Build Essentials

If you don't have gcc-10 and g++-10 installed, the compilation will fail.

sudo apt install cmake cmake-data build-essential 
sudo apt install gcc-10 g++-10 

Clone OpenCV and opencv_contrib Repositories

git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib

Install Dependencies:

sudo apt install libopenblas-dev libopenblas-base libatlas-base-dev liblapacke-dev
sudo apt install libjpeg-dev libpng-dev libtiff-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libv4l-dev v4l-utils
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install protobuf-compiler
sudo apt install python3-dev python3-venv python3-numpy python3-wheel python3-setuptools
sudo apt install tesseract-ocr

Create a Python Environment and Install the Necessary Libraries.

python3 -m venv .env --prompt opencv_cuda
source ~/project/.env/bin/activate
pip install wheel numpy tesseract
deactivate

Pre-checks Before Proceeding

ldconfig -p | grep libopenblas
ldconfig -p | grep libatlas
ldconfig -p | grep liblapacke

Create a Build Folder in the OpenCV Directory

mkdir ~/project/opencv/build
cd ~/project/opencv/build

Generate configuration for make

Note replace the CUDA_ARCH_BIN with your actual compute gpu: nvidia-smi --query-gpu=compute_cap --format=csv

export CC=/usr/bin/gcc-10
export CXX=/usr/bin/g++-10
export OPENCV_VERSION=4.10.0

cmake \
-D CMAKE_BUILD_TYPE=Release \
-D OPENCV_VERSION=${OPENCV_VERSION} \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=1 \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=6.1 \
-D Atlas_CBLAS_INCLUDE_DIR=/usr/lib/x86_64-linux-gnu/ \
-D OpenBLAS_LIB=/usr/lib/x86_64-linux-gnu/ \
-D OPENCV_EXTRA_MODULES_PATH=~/project/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=~/project/.env/bin/python \
-D PYTHON_LIBRARIES=~/project/.env/lib/python3.10/site-packages \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON ..

Handling lapacke.h File Issues

If there are issues related to lapacke.h, you may need to create a new folder and copy the relevant headers:

sudo mkdir /usr/include/openblas
sudo cp /usr/include/lapacke.h /usr/include/openblas/
sudo cp /usr/include/lapacke_mangling.h /usr/include/openblas/
sudo cp /usr/include/lapacke_config.h /usr/include/openblas/
sudo cp /usr/include/lapacke_utils.h  /usr/include/openblas/
sudo cp /usr/include/x86_64-linux-gnu/cblas*.h /usr/include/
rm -rf ~/project/opencv/build/*
# Regenerate configuration for make

Compile and Install it

make -j$(nproc)
sudo make install

Final Step:

Either Link CV2 to the Environment or Use the WHL to Install

To link CV2 to the environment:

ln -s /usr/local/lib/python3.12/site-packages/cv2 \
  ~/project/.env/lib/python3.12/site-packages/cv2

To create and install the WHL file:

# To create and install the WHL file:
cd ~/project/opencv/build/python_loader
python3 setup.py bdist_wheel
# Activate the virtual environment
source ~/project/.env/bin/activate
# Check if there is any wheel
ls ~/project/opencv/build/python_loader/dist/
# Install the generated wheel file
pip install dist/opencv-4.10.0-py3-none-any.whl

Check if CUDA is Enabled

python -c "import cv2; print(f'Cuda Devices: {cv2.cuda.getCudaEnabledDeviceCount()}'); print('OpenCV version:', cv2.__version__);"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment