点击下方卡片,关注“机器视觉与AI深度学习”
视觉/图像重磅干货,第一时间送达!
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
which includes libavresample functionality) Install FFmpeg (
sudo apt install ffmpeg
for hardware acceleration Optional: Install VA-API
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
for video device header Create a symbolic link
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
$ 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
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
# cmake
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=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())
watch -n 1 -d nvidia-smi