经过将近一个月的折腾,终于安装、编译成功,在此做一个记录,以备日后快速搭建环境,也希望能给各位踩坑的小伙伴一点参考。
下载Ubuntu20.04系统镜像和U盘启动制作工具,使用UltraISO制作Ubuntu20.04系统盘。
分区按以下顺序进行:
!EFI一定要放在最前面
注: 我的台式机装完后无法上有线网,如果有同样问题的小伙伴可以到github下载r8125的网卡驱动,自行安装,可以参考这篇文章
sudo apt-get purge ibus
sudo apt-get install fcitx-table-wbpy
其他输入法如下:
防止出现两个输入法图标
sudo apt remove fcitx-ui-classic
sudo reboot
sudo gedit /etc/modprobe.d/blacklist.comf
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
sudo update-initramfs -u
sudo reboot
ctrl + alt + F2
sudo service lightdm stop
sudo apt install lightdm
,弹出的界选择lightdm,再用上面的指令关闭sudo init 3
cd ~/Download
chmod +x NVIDIA-Linux-x86_64-455.28.run
sudo sh NVIDIA-Linux-x86_64-455.28.run --no-opengl-files
注意:no前面是两个横杠,否则会导致安装失败
1、An alternate method of installing the NVIDIA driver was detected. 选择continue installation
这个应该是推荐你通过Ubuntu的“Software & application”中的“Additional Drivers”安装驱动,不用管,继续安装
2、The distribution-provided pre-install script failed! Are you sure you want to continue? 选择 yes 继续。
3、Would you like to register the kernel module sources with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later? 选择NO继续
4、Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up. 选择 Yes 继续
5、Install NVIDIA’s 32-bit compatibility libraries? 选择No 继续
注1:如果提示找不到gcc和make,可以在命令行中安装gcc和make后再安装驱动
sudo apt-get install gcc
sudo apt-get install make
注2:如果出现下面的错误提示,是因为bios的Secure Boot打开了,需要进入bios关闭
The target kernel has CONFIG_MODULE_SIG set, which means that it supports cryptographic signatures on kernel modules. On some systems, the kernel may refuse to load modules without a valid signature from a trusted key. This system also has UEFI Secure Boot enabled; many distributions enforce module signature verification on UEFI systems when Secure Boot is enabled. Would you like to sign the NVIDIA kernel module?
sudo reboot
wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda_11.1.0_455.23.05_linux.run
sudo sh cuda_11.1.0_455.23.05_linux.run
sudo gedit /etc/profile
export PATH=/usr/local/cuda-11.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64$LD_LIBRARY_PATH
sudo reboot
cd /usr/local/cuda-11.1/samples/1_Utilities/deviceQuery
sudo make
/deviceQuery
nvcc -V
cuDNN Runtime Library for Ubuntu18.04 x86_64
cuDNN Developer Library for Ubuntu18.04 x86_64
cuDNN Code Samples and User Guide for Ubuntu18.04 x86_64
sudo dpkg -i libcudnn8_8.0.4.30-1+cuda11.1_amd64.deb
sudo dpkg -i libcudnn8-dev_8.0.4.30-1+cuda11.1_amd64.deb
sudo dpkg -i libcudnn8-samples_8.0.4.30-1+cuda11.1_amd64.deb
opencv安装过程中会有很多文件不能下载导致安装失败,如果有需要到文末百度云下载。
sudo apt-get update -y # Update the list of packages
sudo apt-get remove -y x264 libx264-dev # Remove the older version of libx264-dev and x264
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm
sudo apt-get install -y git gfortran
sudo add-apt-repository -y “deb http://security.ubuntu.com/ubuntu xenial-security main”
sudo apt-get update
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install -y libtiff-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install -y libxine2-dev libv4l-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install -y libvorbis-dev libxvidcore-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install -y x264 v4l-utils
安装以上依赖我并没有出错,如果出现错误提示,这里列出几个错误及解决方案,具体参考这篇博客
E: Unable to locate package libjasper-dev
sudo add-apt-repository “deb http://security.ubuntu.com/ubuntu xenial-security main”
sudo apt-get update
sudo apt-get install libjasper-dev
E: Unable to locate package libgstreamer0.10-dev\
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
安装libpng时缺少libpng12-0
sudo add-apt-repository ppa:linuxuprising/libpng12
sudo apt update
sudo apt --fix-broken install
OpenCV 4.4.0:
https://github.com/opencv/opencv/tags
OpenCV contrib 4.4.0
https://github.com/opencv/opencv_contrib/tags
cd ~
mkdir software
cd Download
tar -zxvf opencv-4.4.0.tar.gz -C /home/username/software
tar -zxvf opencv_contrib-4.4.0.tar.gz -C /home/username/software/opencv4.4.0
sudo apt-get install cmake-gui
cd /home/username/software/opencv-4.4.0
mkdir build
cd build
cmake-gui …
注意:cmake-gui后面的两个点不能丢
/home/username/software/opencv-4.4.0
/home/username/software/opencv-4.4.0/build
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018/ippicv
注:其他opencv版本如果不知道对应的文件,可以先Configure一次,完成后查看红色信息会找到对应的版本,从官网下载对应的文件即可,可以参考这篇博客
“file:/home/username/software/opencv-4.4.0/3rdparty/ippicv/”
Configure完成后,对4个地方进行修改:
直接Generate通常会失败,同样是因为下载文件出错
boostdesc_lbgm.i
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_48.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_120.i
face_landmark_model.dat
https://github.com/opencv/opencv_3rdparty/tree/contrib_xfeatures2d_boostdesc_20161012
https://github.com/opencv/opencv_3rdparty/tree/contrib_xfeatures2d_vgg_20160317
“file:/home/username/software/opencv-4.4.0/opencv_contrib-4.4.0/modules/xfeatures2d/src/”
“file:/home/username/software/opencv-4.4.0/opencv_contrib-4.4.0/modules/xfeatures2d/src/”
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
这个链接不太好用,直接上网盘下载
“file:/home/username/software/opencv-4.4.0/opencv_contrib-4.4.0/modules/face/”
cd /home/username/software/opencv-4.4.0/build
make
注:这里可以多线程编译,不过直接make出问题的概率小一点
多线程:
(1)查看CPU核心数:nproc
(2)make -j16
sudo make install
再次出现100%,就成功了。
/usr/local/lib
sudo gedit /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
sudo gedit /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存后退出,source一下
source /etc/bash.bashrc
到此,带有CUDA的OpenCV4.4.0就安装完成了,可以用以下命令查看opencv的版本:
pkg-config --modversion opencv
接下来我们测试一下opencv。
cd ~
mkdir Projects/opencv_test -p
cd Projects/opencv_test
touch main.cpp CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(opencv_test)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test ${OpenCV_LIBS})
#include
#include
int main(int argc, char** argv) {
cv::Mat src = cv::imread("../1.jpeg");
if (src.empty()) {
std::cout << "could not load image..." << std::endl;
return -1;
}
cv::namedWindow("input image", cv::WINDOW_AUTOSIZE);
cv::imshow("input image", src);
cv::waitKey(0);
return 0;
}
—opencv_test
|__ build
|__ 1.jpeg
|__ CMakeLists.txt
|__ main.cpp
mkdir build
cd build
cmake …
make
./opencv_test
下载darknet:github链接
git clone https://github.com/AlexeyAB/darknet
下载权重文件(YOLO权重文件下载很慢,已经下好放在百度云,有需要到文末下载):
wget https://pjreddie.com/media/files/yolov3.weights
修改Makefile文件:
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
将ARCH后面的-gencode arch=compute_30,code=sm_30删掉
ARCH= -gencode arch=compute_35,code=sm_35
-gencode arch=compute_50,code=[sm_50,compute_50] /
-gencode arch=compute_52,code=[sm_52,compute_52] /
-gencode arch=compute_61,code=[sm_61,compute_61]
运行YOLO v3:
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
检测结果如下:
注1:如果遇到类似"error: ‘IplImage’ does not name a type"和"error: ‘CV_CAP_PROP_FRAME_WIDTH’ was not declared in this scope"的错误,可以参考这篇博客
注2:如果使用官网的darknet,可能会出现"error:‘CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT’ undeclared(first use in this function);did you mean ‘CUDNN_CONVOLUTION_FWD_ALGO_DIRECT’"这样的错误,这是因为CUDNN版本的问题,可以参考这篇博客,用本文给出的darknet链接应该不会报这个错
注3:“error: conversion from ‘cv::Mat’ to non-scalar type ‘IplImage’ {aka ‘_IplImage’} requested”,参考这篇博客