Ubuntu24.04 安装 OpenCV4.10.0 和CUDA12.6

文摘   2024-10-30 07:45   重庆  

点击下方卡片,关注“OpenCV与AI深度学习

视觉/图像重干货,第一时间送达!

先决条件
    安装 nvidia-driver-560 & CUDA-12.6
安装必要依赖
$ sudo apt update$ sudo apt upgrade
# Python VirtualEnvironments with VirtualenvWrapper (Optional)# -------------------------------# Install PIPX:$ sudo apt install pipx$ pipx ensurepath
# Install Virtualenv and Wrapper:$ pipx install virtualenv$ pipx install virtualenvwrapper
# Configure ~/.bashrc:$ vim ~/.bashrc>> export WORKON_HOME=~/.virtualenvs>> export VIRTUALENVWRAPPER_PYTHON=/home/wish/.local/share/pipx/venvs/virtualenvwrapper/bin/python3>> source /home/wish/.local/share/pipx/venvs/virtualenvwrapper/bin/virtualenvwrapper_lazy.sh$ source ~/.bashrc
# Create virtualenv and activate:$ mkvirtualenv opencv$ workon opencv$ pip install numpy# -------------------------------
# Generic tools$ sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
# Image I/O libs$ sudo apt install libjpeg-dev libpng-dev libtiff-dev
# Video/Audio Libs - FFMPEG, GSTREAMER, x264, and etc# Install basic codec libraries$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev# Install GStreamer development libraries$ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev# Install additional codec and format libraries$ sudo apt install libxvidcore-dev libx264-dev libmp3lame-dev libopus-dev# Install additional audio codec libraries$ sudo apt install libmp3lame-dev libvorbis-dev# Install FFmpeg (which includes libavresample functionality)$ sudo apt install ffmpeg# Optional: Install VA-API for hardware acceleration$ sudo apt install libva-dev
# Cameras programming interface libs# Install video capture libraries and utilities$ sudo apt install libdc1394-25 libdc1394-dev libxine2-dev libv4l-dev v4l-utils# Create a symbolic link for video device header$ sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
# GTK lib for the graphical user functionalites coming from OpenCV highghui module$ sudo apt-get install libgtk-3-dev
# Parallelism library C++ for CPU$ sudo apt-get install libtbb-dev
# Optimization libraries for OpenCV$ sudo apt-get install libatlas-base-dev gfortran
# Optional libraries:$ sudo apt-get install libprotobuf-dev protobuf-compiler$ sudo apt-get install libgoogle-glog-dev libgflags-dev$ sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen$ sudo apt-get install python3.12-dev
安装cuDNN9.4.0

$ cd ~/Downloads/$ wget https://developer.download.nvidia.com/compute/cudnn/9.4.0/local_installers/cudnn-local-repo-ubuntu2404-9.4.0_1.0-1_amd64.deb$ sudo dpkg -i cudnn-local-repo-ubuntu2404-9.4.0_1.0-1_amd64.deb$ sudo cp /var/cudnn-local-repo-ubuntu2404-9.4.0/cudnn-*-keyring.gpg /usr/share/keyrings/$ sudo apt-get update$ sudo apt-get -y install cudnn$ dpkg -l | grep libcudnn
CMake、make 和 make install
$ cd ~/Downloads && mkdir opencv && cd opencv$ wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.10.0.zip$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.10.0.zip$ unzip opencv.zip$ unzip opencv_contrib.zip
$ echo "Procced with the installation"$ cd opencv-4.10.0$ mkdir build && cd build
# cmakecmake -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=8.9 \ -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 OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/opencv/lib/python3.12/site-packages \ -D PYTHON_EXECUTABLE=~/.virtualenvs/opencv/bin/python \ -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv/opencv_contrib-4.10.0/modules \ -D PYTHON3_LIBRARY=/usr/lib/python3.12/config-3.12-x86_64-linux-gnu/libpython3.12.so \ -D PYTHON3_INCLUDE_DIR=/usr/include/python3.12 \ -D BUILD_opencv_python3=ON \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D BUILD_EXAMPLES=OFF ..
$ make -j$( nproc ) $ sudo make install
配置与验证
# Include the libs in your environment$ sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'$ sudo ldconfig
# Check cv2$ ls ~/.virtualenvs/opencv/lib/python3.12/site-packages/
$ python3>> import cv2>> print(cv2.getBuildInformation())
# Make available opencv python bindings in the system environment$ sudo cp -r ~/.virtualenvs/opencv/lib/python3.12/site-packages/cv2 /usr/local/lib/python3.12/dist-packages$ ls /usr/local/lib/python3.12/dist-packages/
$ echo "Modify config-3.12.py to point to the target directory" $ sudo nano /usr/local/lib/python3.12/dist-packages/cv2/config-3.12.py >> PYTHON_EXTENSIONS_PATHS = [>> os.path.join('/usr/local/lib/python3.12/dist-packages/cv2', 'python3.12')>> ] + PYTHON_EXTENSIONS_PATHS
# 退出虚拟环境$ deactivate $ sudo apt install python3-numpy $ python3 >> import cv2 >> print (cv2.getBuildInformation())
示例:cv2.cuda_FarnebackOpticalFlow
$ watch -n 1 -d nvidia-smi

—THE END—

下载1:Pytorch常用函数手册

在「OpenCV与AI深度学习公众号后台回复:Pytorch函数手册即可下载学习全网第一份Pytorch函数常用手册,包括Tensors介绍、基础函数介绍、数据处理函数、优化函数、CUDA编程、多处理等十四章内容。

下载2:145个OpenCV实例应用代码
在「OpenCV与AI深度学习」公众号后台回复:OpenCV145即可下载学习145个OpenCV实例应用代码(Python和C++双语言实现)。

欢迎加入CV学习交流微信

觉得有用,记得点个赞和在看 

OpenCV与AI深度学习
专注计算机视觉、深度学习和人工智能领域干货、应用、行业资讯的分享交流!
 最新文章