ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api

前言:每一次编译都有可能出现不同的问题,注意版本间的适配问题。注意虚拟环境与主系统执行文件的链接问题(lib.so文件)。GOOD LUCK!

目录

opencv3.4.6 cuda10编译

conda虚拟环境编译opencv_cuda版本的要点

安装成功后,在conda 虚拟环境下import 仍然为系统版本

conda 虚拟环境下 编译完opencv-3.4.6后,编译openpose


服务器配置

lubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第1张图片

opencv3.4.6 cuda10编译

进入conda 虚拟环境 python=3.6

(使用python3.7 但是尝试各种方法,python3.7无法正常导入有cuda opencv3.4.6)

opencv编译所需依赖

sudo apt-get update
 
sudo apt-get upgrade
 
sudo apt-get install build-essential cmake pkg-config libatlas-base-dev gfortran unzip
 
sudo apt install python-pip
 
pip install numpy

 

创建build 文件夹

mkdir build && cd build

解压缩opencv-3.4.6 ,进入文件夹

https://github.com/opencv/opencv_3rdparty/

在编译时找到ippcv对应版本手动下载

修改ippcv.make文件倒数第九行

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第2张图片

 

 

 

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第3张图片

 

 

 

 

 

 

conda虚拟环境编译opencv_cuda版本的要点

1,C++编译器需要能够找到虚拟环境。没有这一步。设置-D CMAKE_INSTALL_PREFIX参数无效

 

export CPLUS_INCLUDE_PATH=/lib/python3.6

2, cmake需要能找到虚拟环境下的python版本,链接/lib/ 文件夹下的这4个文件,到/usr/lib/x86_64-linux-gnu 文件夹下。否则编译是cmake 无法找到指定版本的python3

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第4张图片

配置 cmake的各种尝试 (刚开始使用的是python3.7,一直无法编译成功,改为python3.6+cuda10+opencv-3.4.6)

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=~/anaconda3/envs/abaction \
-D BUILD_EXAMPLES=ON -D PYTHON3_EXCUTABLE=~/anaconda3/envs/abaction/bin/python3 \ -D PYTHON3_LIBRARY=~/anaconda3/envs/abaction/lib/libpython3.6m.so \
-D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/abaction/include/python3.7m \ 
-D PYTHON3_PACKAGES_PATH=~/anaconda3/envs/abaction/lib/python3.7/site-packages \ 
-D BUILD_opencv_python3=ON \ 
-D WITH_OPENMP=ON \ 
-D BUILD_SHARED_LIBS=ON \
-D WITH_CUDA=ON \
-D PYTHON_NUMPY_PATH=~/anaconda3/envs/abaction/lib/python3.7/site-packages \ 
-D PYTHON_NUMPY_INCLUDE_DIRS=~/anaconda3/envs/abaction/lib/python3.7/site-packages/numpy/core/include ..


cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=~/anaconda3/envs/abaction \ -D PYTHON3_EXCUTABLE=~/anaconda3/envs/abaction/bin/python3 \ -D PYTHON3_LIBRARY=~/anaconda3/envs/abaction/lib/libpython3.7m.so \ -D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/abaction/include/python3.7m \ -D PYTHON3_PACKAGES_PATH=~/anaconda3/envs/abaction/lib/python3.7/site-packages \ -D BUILD_opencv_python3=ON \ -D BUILD_opencv_python2=OFF \ -D WITH_CUDA=ON \ -D BUILD_EXAMPLES=OFF \ -D ENABLE_CXX11=1 ..

cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=~/anaconda3/envs/abaction \ -D PYTHON3_EXCUTABLE=~/anaconda3/envs/abaction/bin/python3 \ -D PYTHON3_LIBRARY=~/anaconda3/envs/abaction/lib/libpython3.7m.so \ -D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/abaction/include/python3.7m \ -D PYTHON3_PACKAGES_PATH=~/anaconda3/envs/abaction/lib/python3.7/site-packages \ -D BUILD_opencv_python3=ON \ -D BUILD_opencv_python2=OFF \ -D CUDA_CUDA_LIBRARY=/usr/local/cuda-10.0/lib64/stubs/libcuda.so \ -D CUDA_ARCH_BIN="7.5" \ -D CUDA_ARCH_PTX="" \ -D WITH_CUDA=ON \ -D WITH_TBB=ON \ -D BUILD_EXAMPLES=ON \ -D ENABLE_CXX11=1 \-D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_NVCUVID=ON \ -D BUILD_opencv_cudacodec=OFF ..

make -j20

make install

最终编译并安装成功的配置如下:有几个要点

-D BUILD_opencv_cudacodec=OFF

-D BUILD_opencv_python2=OFF(在cmake-gui中直接删除python2的相关路径更有效)

-D BUILD_opencv_python3=ON

3,使用cmake-gui 确认编译后cuda的信息,以及python交互和 用于编译的python路径。

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第5张图片

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第6张图片

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第7张图片

 

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第8张图片

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第9张图片

安装成功后,在conda 虚拟环境下import 仍然为系统版本

将opencv安装路径下的<安装路径>./lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so

软链接(ln -s )到/lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so

(cv2整个文件夹都要进行软链接)

之后再bashrc文件中设置(如果想进行opencv版本切换,再设置bashrc文件即可)

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:

conda 虚拟环境下 编译完opencv-3.4.6后,编译openpose

1,不要使用之前已经编译好的openpose库,重新下载

git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose.git

 2,在models文件夹下,下载openpose model文件

3,下载pybind11文件夹,放在openpose/3rdparty文件夹

4,在openpose文件夹下 cmake-gui. 设置openpose/build路径后,configure,Caffe文件包含在openpose/3rdparty文件夹下

5,设置Caffe的配置文件。

(1)配置Makefile.config文件

cp Makefile.config.Ubuntu16_cuda9_JetsonTX2_JetPack33 Makefile.config

修改Makefile.config文件

将Python2环境注销,换成Anaconda3下的Python环境.

# We need to be able to find Python.h and numpy/arrayobject.h.
#PYTHON_INCLUDE := /usr/include/python2.7 \
#		/usr/lib/python2.7/dist-packages/numpy/core/include \
#		/usr/local/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
 ANACONDA_HOME := $home$/anaconda3/envs/abbaction


# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_LIBRARIES := boost_python-py35 python3.6m 
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		 $(ANACONDA_HOME)/include/python3.6m \
		 $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib /usr/local/lib
 PYTHON_LIB := $(ANACONDA_HOME)/lib

PYTHON_LIBRARIES := boost_python-py35 python3.6m #boost_python-py35 是看/usr/lib/x86_64-linux-gnu/文件夹下的libboost_python文件,启动python3编译。python3.6m是虚拟环境下的python版本

 ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第10张图片

(2) 配置Makefile文件

184行左右

#LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

212行

#PYTHON_LIBRARIES ?= boost_python python2.7
PYTHON_LIBRARIES ?= boost_python-py35 python3.6m

431行

#NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

 

6,在虚拟环境下编译openpose,注意,设置opencv路径,OpenCV_DIR,OpenCV_INCLUDE_DIRS,OpenCV_LIBS_DIR

设置cmake PYTHON相关执行路径 点击Add_Entry,

PYTHON_EXECUTABLE:FILEPATH=~/anaconda3/envs/$envname$/bin/python3.6
 
PYTHON_LIBRARY:FILEPATH=~/anaconda3/envs//$envname$/lib/libpython3.6m.so
 

BUILD_CAFFE=ON

BUILD_PYTHON=ON

 

ubuntu16.04 conda下配置-opencv3.4.6-cuda10-openpose python api_第11张图片

 

你可能感兴趣的:(ubuntu环境搭建手册)