anaconda3 安装opencv3.4.2 cuda9.2 mint19(ubuntu 18.04)

2023-06-12,,

从opencv1的时代,编译这玩意就不是太轻松。之前都是在win下。2.x时代,开始用cmake GUI,选vs版本,x86 x64 各种依赖库选项,debug release,...

现在3.4了,在ubuntu下也不是太容易。

人老了,懒得自己折腾了,先凑合在anaconda3下用别人编译好现成的opencv,用python随便试试算法吧。

实在不行了,再自己编译opencv,纯用C++写吧。

根据opencv官网17年的说法,目前opencv的python接口还是无法直接使用自定义的cuda GpuMat之类。

http://answers.opencv.org/question/172135/how-to-use-cuda-in-pyhton/

但是调用C++封装的使用gpu的算法是没问题的!。所以,用python做这个粘合剂,在anaconda环境里查看效果,还是很好的。

1用C++ 版的opencv写gpu版的算法,打包

2 在anaconda中,用py调用,间接使用gpu

本文主要解决2的问题。

1配置cuda 9.2

分2步

1host安装配置cuda

2anaconda配置路径(找到本机的cuda bin和lib64路径)

1.1安装cuda 9.2

现在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-396
sudo 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已经配好。

1.2 anaconda3中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之后才能判断。

2 安装opencv-cuda

有好事之徒(好心人)已经编译了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.2

Extra modules:
Location (extra): /home/tee/anaconda3/conda-bld/opencv-cuda_1534922162608/work/opencv_contrib-3.4.2/modules
Version control (extra): 3.4.2

Platform:
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: Release

CPU/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_SKX

C/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: NO

GUI:

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: YES

Video 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: NO

Parallel 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: NO

NVIDIA 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-packages

Python (for build): /home/machinelearning/anaconda3/envs/cuda-opencv/bin/python

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Install to: /home/machinelearning/anaconda3/envs/cuda-opencv
-----------------------------------------------------------------

标红的部分说明opencv成功找到了cuda。

到此为止,前后折腾了快1天,可以看出,到处都有点小坑,但是不断拆成2小步,把某一小步替换掉,还是比较容易解决的。

anaconda3 安装opencv3.4.2 cuda9.2 mint19(ubuntu 18.04)的相关教程结束。

《anaconda3 安装opencv3.4.2 cuda9.2 mint19(ubuntu 18.04).doc》

下载本文的Word格式文档,以方便收藏与打印。