Created
July 19, 2024 03:42
-
-
Save bitcode/18cca7b669fdf5bd998faf0a361b718d to your computer and use it in GitHub Desktop.
Opencv Cuda Support Installation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Prerequisites: | |
| # 1. Ensure you have the latest NVIDIA drivers installed | |
| # 2. Install CUDA toolkit from the official NVIDIA site | |
| # 3. Install cuDNN (NVIDIA CUDA Deep Neural Network library) | |
| # Update and upgrade the system | |
| sudo apt update && sudo apt upgrade -y | |
| # Install dependencies | |
| sudo apt install -y build-essential cmake git pkg-config libgtk-3-dev \ | |
| libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \ | |
| libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \ | |
| gfortran openexr libatlas-base-dev python3-dev python3-numpy \ | |
| libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \ | |
| libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev | |
| # Additional dependencies | |
| sudo apt install -y libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev | |
| sudo apt install -y python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev | |
| sudo apt install -y libcanberra-gtk* libatlas-base-dev gfortran | |
| sudo apt install -y python3-dev python3-pip | |
| sudo -H pip3 install -U pip numpy | |
| sudo apt install -y python3-testresources | |
| # Install additional libraries | |
| sudo apt install -y libprotobuf-dev protobuf-compiler | |
| sudo apt install -y libgoogle-glog-dev libgflags-dev | |
| sudo apt install -y libgphoto2-dev libeigen3-dev libhdf5-dev doxygen | |
| # Download OpenCV and OpenCV contrib source | |
| wget -O opencv.zip https://github.com/opencv/opencv/archive/4.10.0.zip | |
| wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.10.0.zip | |
| # Unzip the archives | |
| unzip opencv.zip | |
| unzip opencv_contrib.zip | |
| # Navigate to OpenCV directory | |
| cd opencv-4.10.0 | |
| # Create build directory | |
| mkdir build && cd build | |
| # Configure the build | |
| cmake -D CMAKE_BUILD_TYPE=RELEASE \ | |
| -D CMAKE_INSTALL_PREFIX=/usr/local \ | |
| -D WITH_TBB=ON \ | |
| -D ENABLE_FAST_MATH=1 \ | |
| -D CUDA_FAST_MATH=1 \ | |
| -D WITH_CUBLAS=1 \ | |
| -D WITH_CUDA=ON \ | |
| -D BUILD_opencv_cudacodec=OFF \ | |
| -D WITH_CUDNN=ON \ | |
| -D OPENCV_DNN_CUDA=ON \ | |
| -D CUDA_ARCH_BIN=7.5 \ | |
| -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 HAVE_opencv_python3=ON \ | |
| -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.10.0/modules \ | |
| -D INSTALL_PYTHON_EXAMPLES=OFF \ | |
| -D INSTALL_C_EXAMPLES=OFF \ | |
| -D BUILD_EXAMPLES=OFF \ | |
| -D WITH_NVCUVID=ON \ | |
| .. | |
| # Build OpenCV | |
| make -j$(nproc) | |
| # Install OpenCV | |
| sudo make install | |
| # Update library cache | |
| sudo ldconfig | |
| # Verify the installation | |
| python3 -c "import cv2; print(cv2.__version__)" | |
| # Create a test script | |
| cat << EOF > opencv_cuda_test.py | |
| import cv2 | |
| import numpy as np | |
| print(f"OpenCV version: {cv2.__version__}") | |
| print(f"CUDA enabled: {cv2.cuda.getCudaEnabledDeviceCount() > 0}") | |
| print(f"Number of CUDA devices: {cv2.cuda.getCudaEnabledDeviceCount()}") | |
| # Simple CUDA operation test | |
| if cv2.cuda.getCudaEnabledDeviceCount() > 0: | |
| gpu_mat = cv2.cuda_GpuMat() | |
| gpu_mat.upload(np.random.randint(0, 255, (1000, 1000), dtype=np.uint8)) | |
| result = cv2.cuda.subtract(gpu_mat, gpu_mat) | |
| print("CUDA operation successful") | |
| else: | |
| print("No CUDA devices available") | |
| EOF | |
| # Run the test script | |
| python3 opencv_cuda_test.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment