經過將近一個月的折騰,終於安裝、編譯成功,在此做一個記錄,以備日後快速搭建環境,也希望能給各位踩坑的小夥伴一點參考。
下載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
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個地方進行修改:
CMAKE_BUILD_TYPE處選擇Release,如果沒有選項手動輸入即可;下方的CMAKE_INSTALL_PREFIX保持預設路徑/usr/local
OPENCV_EXTRA_MODULES_PATH處選擇/home/username/software/opencv-4.4.0/opencv_contrib-4.4.0/下的modules目錄
OPENCV_GENERATE_PKGCONFIG選項打勾,這一項是用來生成opencv.pc檔案,很重要
BUILD_opencv_world選項打勾,預設是不勾選的,勾選後最後只會產生一個庫檔案
直接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 <iostream>
#include <opencv2/opencv.hpp>
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」,參考這篇部落格