sudo vim /etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
options nouveau modeset=0
sudo update-initramfs -u
sudo reboot
lsmod | grep nouveau
回车后没有输出,则代表成功。
sudo apt-get remove --purge nvidia*
sudo service lightdm stop
按Ctrl-Alt+F1
进入命令行界面,输入用户名和密码
登录。(驱动安装好以后,可通过命令:sudo service lightdm start ,然后按Ctrl-Alt+F7来恢复到图形界面。
)
# 进到驱动程序路径
sudo chmod a+x NVIDIA-Linux-x86_64-460.56.run
#赋予执行权限
sudo ./NVIDIA-Linux-x86_64-460.56.run --no-opengl-files
其中,–no-opengl-files:表示只安装驱动文件,不安装OpenGL文件。这个参数不可省略,否则会导致登陆界面死循环。(因为NVIDIA的驱动默认会安装OpenGL,而Ubuntu的内核本身也有OpenGL、且与GUI显示息息相关,一旦NVIDIA的驱动覆写了OpenGL,在GUI需要动态链接OpenGL库的时候就引起问题
)
reboot
,如果安装失败,重复上面步骤,多安装几次即可。nvidia-smi
cuda下载:
cuda: https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=2004&target_type=runfilelocal
安装cuda:
sudo sh cuda_11.2.0_460.27.04_linux.run
gedit ~/.bashrc
命令打开文件,在文件结尾输入以下语句,保存。export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
source ~/.bashrc
需要登录账号
)sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
(py38) ytf@ytf:~$ python
Python 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 16:22:27)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> flag = torch.cuda.is_available()
>>> print(flag)
True
opencv-4.5.1.tar.gz
和opencv_contrib-4.5.1.tar.gz
,可在这里进行下载。然后分别解压:tar -xzvf opencv-4.5.1.tar.gz
tar -xzvf opencv_contrib-4.5.1.tar.gz
解压后目录结构如下:
opencv4.5.1/
/opencv-4.5.1 # opencv 源码目录
/opencv_contrib-4.5.1 # opencv_contrib 源码目录
/opencv_build # opencv 构建结果目录
sudo apt install -y build-essential cmake git pkg-config libopenexr-dev libblas-dev
sudo apt install -y python-dev python-numpy libtbb2 libtbb-dev
sudo apt install -y libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libjasper-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libavutil-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
sudo apt install -y \
libx264-dev \
mesa-utils \
libgtk2.0-dev \
libxvidcore-dev \
yasm \
libxine2-dev \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libv4l-dev \
libfaac-dev \
libmp3lame-dev \
libopencore-amrnb-dev \
libtheora-dev \
libvorbis-dev \
ffmpeg \
libeigen3-dev libeigen3-doc \
liblapack-dev \
tesseract-ocr \
tesseract-ocr-jpn \
libgflags-dev \
libleptonica-dev \
libtesseract-dev \
gphoto2 \
liblapacke-dev \
libgoogle-glog-dev \
libprotobuf-dev \
libprotoc-dev \
protobuf-compiler \
ccache \
libgphoto2-dev \
libavresample-dev \
libatlas-base-dev \
gfortran
opencv_build
路径,执行 cmake
,其中CUDA_ARCH_BIN
不是定值,可在这里查看自己显卡的计算力:cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.1/modules \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_ARCH_BIN=6.1 \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_WEBP=OFF \
-D WITH_CUBLAS=1 \
-D WITH_VTK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_JAVA=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D HAVE_OPENCV_PYTHON3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
../opencv-4.5.1
此过程中如若遇到报错:
/opencv_contrib-4.5.1/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory
不要着急,可通过以下命令进行解决:
cd /home/ytf/workspace/opencv4.5/opencv-4.5.1/.cache/xfeatures2d/
unzip /home/audrey/Downloads/patch.zip
其中,“patch.zip
”可在此网站进行下载https://github.com/opencv/opencv_contrib/files/4520852/patch__.zip,然后解压到上述指定路径。再次cmake
前,需将opencv_build
文件夹清空:
rm -rf ./*
make -j18 # 其中"18”为pc线程数
make install
python3 -c "import cv2; print(cv2.__version__)"
如若遇到错误1:
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "", line 1, in
File "/home/ytf/anaconda3/lib/python3.8/site-packages/cv2/__init__.py", line 102, in
bootstrap()
File "/home/ytf/anaconda3/lib/python3.8/site-packages/cv2/__init__.py", line 90, in bootstrap
import cv2
ImportError: libopenh264.so.5: cannot open shared object file: No such file or directory
解决方法有两种:安装或更新ffmpeg、更改名称(推荐)。
第一种方法:
# ffmpeg 3.4.1
conda install ffmpeg x264=20131218 -c conda-forge
# ffmpeg 4
conda install x264=='1!152.20180717' ffmpeg=4.0.2 -c conda-forge
# or
conda update ffmpeg
如果第一种方法后仍然报这个错误的话,请尝试第二种解决方法:
ln -s ~/anaconda3/lib/libopenh264.so ~/anaconda3/envs/py38/lib/libopenh264.so.5
如若遇到错误2:
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
发生此错误的原因是numpy版本没有匹配,解决方法如下(可重复卸装几次):
pip uninstall numpy
pip install numpy
再次验证就可得到opencv版本:
(py38) ytf@ytf:~$ python3 -c "import cv2; print(cv2.__version__)"
4.5.1
就此大功告成!!!