Skip to content

Instantly share code, notes, and snippets.

@tonyreina
Last active August 13, 2024 05:12
Show Gist options
  • Save tonyreina/f3f4e35650a485632145a2eff4db923d to your computer and use it in GitHub Desktop.
Save tonyreina/f3f4e35650a485632145a2eff4db923d to your computer and use it in GitHub Desktop.
Build OpenCV with CUDA and Python
#!/bin/bash
# Downloads and builds OpenCV with NVIDIA CUDA support.
# Builds C++ and Python libraries.
# To use in Python just import cv2
echo -e "Building OpenCV with CUDA support...\n\n"
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR
mkdir -p opencv_build_${datetime_string}
cd opencv_build_${datetime_string}
# Function to check if a package is installed
is_installed() {
dpkg -s "$1" &> /dev/null
}
# Function to check if a Python package is installed
is_python_package_installed() {
python -c "import $1" &> /dev/null
}
# Get CUDNN version
if is_installed libcudnn9; then
CUDNN_VERSION=$(cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 | grep "#define CUDNN_MAJOR" | cut -d " " -f3)
else
CUDNN_VERSION=9
fi
LIBCUDNN=libcudnn$CUDNN_VERSION
# Get CUDA version
if command -v nvcc &> /dev/null; then
CUDA_VERSION_DOT=$(nvcc --version | grep release | cut -d " " -f5 | cut -d "," -f1)
CUDA_VERSION_MAJOR=$(nvcc --version | grep release | cut -d " " -f5 | cut -d "." -f1)
else
CUDA_VERSION_DOT=12.6
CUDA_VERSION_MAJOR=12
fi
CUDA_VERSION_DASH=$(echo ${CUDA_VERSION_DOT} | sed 's/\./-/g')
GCC_VERSION=$(gcc --version | grep gcc | cut -d " " -f 4 | cut -d "." -f1)
echo "CUDA Version: ${CUDA_VERSION_DOT}"
echo "LIBCUDNN: ${LIBCUDNN}"
echo "GCC Version: ${GCC_VERSION}"
# Install dependencies
# ${LIBCUDNN}-cuda-dev-${CUDA_VERSION_MAJOR} \
sudo apt update && sudo apt install -y \
build-essential \
cmake \
cmake-data \
cuda-toolkit-${CUDA_VERSION_DASH} \
ffmpeg \
gcc-${GCC_VERSION} \
g++-${GCC_VERSION} \
git \
libavcodec-dev \
libavformat-dev \
${LIBCUDNN}-cuda-${CUDA_VERSION_MAJOR} \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libgtk-3-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libswscale-dev \
libv4l-dev \
libxvidcore-dev \
libvtk9-dev \
libx264-dev \
libyaml-cpp-dev \
pkg-config \
protobuf-compiler \
pylint \
python3-numpy \
tesseract-ocr \
qv4l2 \
v4l-utils
# Verify NVIDIA drivers
nvidia-smi
# Update PATH for CUDA
export PATH=/usr/local/cuda-${CUDA_VERSION_DOT}/bin:$PATH
# Check if NumPy is installed, if not install it
if ! is_python_package_installed numpy; then
echo "NumPy not found. Installing NumPy..."
$(which pip) install numpy
else
echo "NumPy is already installed."
fi
# Clone OpenCV repositories
INSTALL_ROOT=$(pwd)
echo "Git clone may take a few minutes. Please wait..."
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.10.0
cd ..
echo "Git clone may take a few minutes. Please wait..."
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.10.0
cd ..
# Build OpenCV with CUDA support
cd opencv
mkdir build
cd build
# Run CMake configuration
cmake \
-D BUILD_EXAMPLES=OFF \
-D BUILD_JAVA=OFF \
-D BUILD_opencv_apps=ON \
-D BUILD_opencv_cudacodec=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_TIFF=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_PROTOBUF=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=gcc-${GCC_VERSION} \
-D CMAKE_CXX_COMPILER=g++-${GCC_VERSION} \
-D CMAKE_CXX_VERSION=20 \
-D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
-D CUDA_ARCH_BIN=$(nvidia-smi --query-gpu=compute_cap --format=csv | tail -1) \
-D CUDA_FAST_MATH=ON \
-D ENABLE_FAST_MATH=ON \
-D HAVE_PROTOBUF=OFF \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_EXTRA_MODULES_PATH=${INSTALL_ROOT}/opencv_contrib/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D PROTOBUF_UPDATE_FILES=ON \
-D PYTHON3_EXECUTABLE=$(which python) \
-D PYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_LIBRARIES=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D WITH_FFMPEG=ON \
-D WITH_GDAL=ON \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D WITH_NVCUVENC=ON \
-D WITH_NVCUVID=ON \
-D WITH_OPENGL=ON \
-D WITH_OPENMP=ON \
-D WITH_PNG=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
..
# Build and install OpenCV
make clean
echo "Building OpenCV with CUDA support"
make -j$(nproc)
echo "Installing Python and C++ library"
make install
echo -e "OpenCV build complete.\n\n"
# Check if the installation was successful
num_cuda_devices=$(python -c "import cv2; count = cv2.cuda.getCudaEnabledDeviceCount(); print(count)")
if [ ${num_cuda_devices} -gt 0 ]; then
echo -e "\e[32mInstalled OpenCV with CUDA support successfully! Found ${num_cuda_devices} CUDA devices.\e[0m"
else
echo -e "\e[31mInstall unsuccessful. CUDA is not enabled and/or GPU not detected.\e[0m"
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment