從opencv1的時代,編譯這玩意就不是過輕鬆。以前都是在win下。2.x時代,開始用cmake GUI,選vs版本,x86 x64 各類依賴庫選項,debug release,...java
如今3.4了,在ubuntu下也不是太容易。python
人老了,懶得本身折騰了,先湊合在anaconda3下用別人編譯好現成的opencv,用python隨便試試算法吧。linux
實在不行了,再本身編譯opencv,純用C++寫吧。c++
根據opencv官網17年的說法,目前opencv的python接口仍是沒法直接使用自定義的cuda GpuMat之類。算法
http://answers.opencv.org/question/172135/how-to-use-cuda-in-pyhton/ubuntu
可是調用C++封裝的使用gpu的算法是沒問題的!。因此,用python作這個粘合劑,在anaconda環境裏查看效果,仍是很好的。app
1用C++ 版的opencv寫gpu版的算法,打包ide
2 在anaconda中,用py調用,間接使用gpuui
本文主要解決2的問題。spa
分2步
1host安裝配置cuda
2anaconda配置路徑(找到本機的cuda bin和lib64路徑)
如今nv官網尚未針對ubuntu 18.04的deb包,要用runfile安裝。
但若是一路yes是要安裝396.37顯卡驅動的,這種安裝方法必須停掉X window。而我這種懶人用的是mint的GUI,不想折騰CLI了,因此拆成兩步:
1 單獨用第三方源安裝顯卡驅動到396.54
2 安裝cuda時選擇不安裝顯卡驅動,其餘選擇一路默認
用第三方源安裝顯卡驅動:
sudo add-apt-repository ppa:graphics-drivers/ppa
而後在GUI 系統管理,驅動管理器裏,應該就能看到396.54了,點擊切換就能夠了
也能夠用命令行
sudo apt install nvidia-driver-396sudo apt install nvidia-settings
可是安裝好以後,還要根據提示配置PATH和LD_LIBRARY_PATH, 這是爲了在host機器上能使用nvcc以及找到各類cuda的so
sudo xed ~/.profile
加入
PATH="/usr/local/cuda/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
LIBRARY_PATH="/usr/local/cuda/lib64:$LIBRARY_PATH"
爲了python,以防萬一,也加上
PYTHONPATH="/usr/bin:$PYTHONPATH"
PYTHONPATH="/usr/local/cuda/lib64:$PYTHONPATH"
PYTHONPATH="/usr/local/cuda/bin:$PYTHONPATH"
保存,關閉
source ~/.profile
nvcc -V
應該能看到
本機的cuda已經配好。
在 anaconda-navigator Environments裏下面點create,新建一個env,好比cuda-opencv。
anaconda的每一個env和pipenv之類的相似,都是獨立安裝庫的隔離環境。
建立完畢在~/anaconda3/envs下就會出現cuda-opencv子目錄,裏面就是各類安裝的庫。
建立一些文件,讓cuda-opencv每次啓動時,能找到host的cuda庫就能夠了
參考https://stackoverflow.com/questions/46826497/conda-set-ld-library-path-for-env-only
在anaconda-navigator ->Environments-> cuda-opencv 箭頭上點擊,啓動一個終端
mkdir -p ./etc/conda/activate.d mkdir -p ./etc/conda/deactivate.d touch ./etc/conda/activate.d/env_vars.sh touch ./etc/conda/deactivate.d/env_vars.sh
相似python的virualenv 每次啓動時會activate,那麼sh腳本里export變量就好了
編輯啓動env的腳本
xed ./etc/conda/activate.d/env_vars.sh
加入
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH}
export LIBRARY_PATH=/usr/local/cuda/lib64:${LIBRARY_PATH}
export PATH=/usr/local/cuda/bin:${PATH}
保存退出。
cuda是否配好,要在下面裝好opencv以後才能判斷。
有好事之徒(好心人)已經編譯了opencv3.4.2帶cuda的版本,直接用env的控制檯安裝
conda install -c oddconcepts opencv-cuda
在spyder IDE裏 import cv2
會提示,找不到libjasper.so.1
這是由於ubuntu 在17.04以後取消了libjasper-dev了,(按其餘教程要裝這個那都是ubuntu 16.04的,在18.04裏是裝不上的)
Note that
libjasper-dev
has been removed from Ubuntu 17.04 (https://launchpad.net/ubuntu/zesty/amd64/libjasper-dev/1.900.1-debian1-2.4+deb8u1), and they suggest to use OpenJPEG instead (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=812630).Since OpenCV uses
jasper
trying to install it manually may be an option.
那麼仍是在env裏用conda裝,可是注意不要搜libjasper,可能裝上的是.a的靜態庫版本,那麼仍是會找不到.so
這麼裝:
conda install -c conda-forge jasper
而後應該 import cv2時不報錯。
這時,檢查一下opencv安裝的狀況:
print(cv2.getBuildInformation())
我這裏的信息是:
General configuration for OpenCV 3.4.2 =====================================
Version control: 3.4.2Extra modules:
Location (extra): /home/tee/anaconda3/conda-bld/opencv-cuda_1534922162608/work/opencv_contrib-3.4.2/modules
Version control (extra): 3.4.2Platform:
Timestamp: 2018-08-22T07:16:54Z
Host: Linux 4.4.0-130-generic x86_64
CMake: 3.9.4
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: ReleaseCPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (3 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (4 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKXC/C++:
Built as dynamic libs?: YES
C++ Compiler: /usr/bin/c++ (ver 5.4.0)
C++ flags (Release): -I/home/machinelearning/anaconda3/envs/cuda-opencv/include -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -I/home/machinelearning/anaconda3/envs/cuda-opencv/include -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -I/home/machinelearning/anaconda3/envs/cuda-opencv/include -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -I/home/machinelearning/anaconda3/envs/cuda-opencv/include -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release):
Linker flags (Debug):
ccache: NO
Precompiled headers: YES
Extra dependencies: dl m pthread rt cudart nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64
3rdparty dependencies:OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: js world
Disabled by dependency: dnn_objdetect text
Unavailable: cnn_3dobj cvv dnn java matlab ovis python2 sfm ts viz
Applications: apps
Documentation: NO
Non-free algorithms: NOGUI:
Media I/O:
ZLib: /home/machinelearning/anaconda3/envs/cuda-opencv/lib/libz.so (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 1.5.3-62)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.34)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
HDR: YES
SUNRASTER: YES
PXM: YESVideo I/O:
DC1394: YES (ver 2.2.4)
FFMPEG: YES
avcodec: YES (ver 58.18.100)
avformat: YES (ver 58.12.100)
avutil: YES (ver 56.14.100)
swscale: YES (ver 5.1.100)
avresample: YES (ver 4.0.0)
GStreamer: NO
libv4l/libv4l2: NO
v4l/v4l2: linux/videodev2.h
gPhoto2: NOParallel framework: OpenMP
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2017.0.3 [2017.0.3]
at: /home/tee/anaconda3/conda-bld/opencv-cuda_1534922162608/work/build/3rdparty/ippicv/ippicv_lnx
Intel IPP IW: sources (2017.0.3)
at: /home/tee/anaconda3/conda-bld/opencv-cuda_1534922162608/work/build/3rdparty/ippicv/ippiw_lnx
Lapack: NO
Eigen: YES (ver 3.3.3)
Custom HAL: NONVIDIA CUDA: YES (ver 9.2, CUFFT CUBLAS)
NVIDIA GPU arch: 30 35 37 50 52 60 61 70
NVIDIA PTX archs:Python 3:
Interpreter: /home/machinelearning/anaconda3/envs/cuda-opencv/bin/python (ver 3.6.6)
Libraries: /home/machinelearning/anaconda3/envs/cuda-opencv/lib/libpython3.6m.so (ver 3.6.6)
numpy: /home/machinelearning/anaconda3/envs/cuda-opencv/lib/python3.6/site-packages/numpy/core/include (ver 1.11.3)
packages path: /home/machinelearning/anaconda3/envs/cuda-opencv/lib/python3.6/site-packagesPython (for build): /home/machinelearning/anaconda3/envs/cuda-opencv/bin/python
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NOInstall to: /home/machinelearning/anaconda3/envs/cuda-opencv
-----------------------------------------------------------------
標紅的部分說明opencv成功找到了cuda。
到此爲止,先後折騰了快1天,能夠看出,處處都有點小坑,可是不斷拆成2小步,把某一小步替換掉,仍是比較容易解決的。