您的位置:首页 > 编程语言 > Python开发

Ubuntu 16.04 部署opencv3.1.0流程与详细的亲测问题整理(python3 for build)

2017-12-13 19:28 1581 查看

Ubuntu 16.04 部署opencv3.1.0流程与详细的亲测问题整理(python3 for build)

简介:

1.上一篇文章主要介绍了roboschool的部署方式,下面将要介绍的opencv3.1.0是建立在其环境之上部署的(因为baselines算法需要cv2的支持),如果不清楚可以参考上一篇文章,链接在这里

2.本文基于叶落寒蝉的文章opencv在Ubuntu上的安装方法基础上完成。主要整理了一些常见问题,并且对该文章进行了修正。

1.编译环境安装:

sudo apt-get install build-essential


2.必需安装包:

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev


3.可选安装包(建议安装):

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev


为了避免遗漏,我安装时按照github上的热心网友指导,又补充了下面的安装包(有些已经安装过了,linux系统会自动识别的):

sudo apt-get install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip


4.下载OpenCV3.1.0:

wget https://github.com/Itseez/opencv/archive/3.1.0.zip[/code] 


git clone git://github.com/Itseez/opencv.git


5.安装OpenCV3.1.0:

注意,这里安装opencv我并没有按照官方教程的方法,由于我希望采用python3+对其进行build因此在设置上需要进行一些修改。

此外,根据github上的issues汇总,最终整理如下:

cd ~/opencv-3.1.0
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv-3.1.0/opencv_contrib/modules -D BUILD_EXAMPLES=OFF -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D PYTHON_EXECUTABLE=$(which python3) -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..
// 注意这里面~/opencv-3.1.0/opencv_contrib/modules 是我的电脑上的路径,需要根据用户情况自行设置


如果配置成功会给出相应的显示,并且你会看到:

Python (for build): /home/xxx/anaconda3/bin/python3

也就是说成功选择我们的python3去build

build的错误整理:

此外,这期间会遇到很多错误,常见的:

1. CMake Error at 3rdparty/ippicv/downloader

2. fatal error: sys/videoio.h: not found

3. fatal error: linux/videodev.h: not found

4. fatal error: ffmpeg/avformat.h: not found

对于问题1:

手动下载ippicv_linux_20151201.tgz到/home/xxx/opencv-3.1.0/3rdparty/ippicv/downloads文件夹下的自动创建的目录:linux-d41d8cd98f00b204e9800998ecf8427e中就可以了

对于问题2:

该问题解决了其他问题后并且按照步骤完成后应该会自动消失,比较神奇,我去网上查了很多资料没有详细的解决办法,我分析原因是由于其他的地方配置出错导致的,因为安装成功的案例里面并没有在这个文件夹找到这个videoio.h文件。

对于问题3:

4000
这个问题有很多具体的解决方案,我是按照下面来执行的,两条命令搞定:

sudo apt-get install libv4l-dev*
sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h


对于问题4:

这个问题遇到的人还是蛮多的,解决方法各式各样,可以移步到github上opencv的issues汇总看一下。我这里用如下方法一次性成功:

sudo apt remove ffmpeg
sudo apt-get install ffmpeg
ffmpeg --version

// 安装之后相关信息:--libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu

sudo -i
mkdir /usr/include/ffmpeg
cd /usr/include/ffmpeg
ln -sf /usr/include/x86_64-linux-gnu/libavcodec/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libavformat/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libswscale/*.h ./


虽然看着挺繁琐,但是经过我实际测试,其实不用按照后面的命令执行,只要安装完成后,在usr/include文件夹下建立一个ffmpeg文件夹,里面拷贝一下/usr/include/x86_64-linux-gnu/libavformat/文件夹下的三个.h文件即可;

6.安装OpenCV3.1.0:

安装只需要一条命令了:

make -j7 && sudo make install


install的错误整理1:

opencv3.1.0/modules/cudalegacy/src/graphcuts.cpp:274:146:error...


在其他文件也可能出现类似的错误。出现错误的地方在cuda上。解决方案是找到opencv3.1.0/modules/cudalegacy/src/graphcuts.cpp文件,将下面的一行:

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)


改为

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)|| (CUDART_VERSION>=8000)


install的错误整理2:

The imported target "vtkRenderingPythonTkWidgets" references the file
"/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
but not all the files it references.

-- The imported target "vtk" references the file
"/usr/bin/vtk"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
but not all the files it references.


可以通过命令
sudo apt install python-vtk
解决

install的错误整理3:

/usr/include/string.h: In function ‘void* __mempcpy_inline(void*, const void*, size_t)’:
/usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope
return (char *) memcpy (__dest, __src, __n) + __n;
^
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:266 (message):
Error generating file
/home/zdl/opencv-3.1.0/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o

modules/core/CMakeFiles/opencv_core.dir/build.make:399: recipe for target 'modules/core/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gpu_mat.cu.o' failed
make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gpu_mat.cu.o] Error 1
CMakeFiles/Makefile2:2203: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2


很多网友受到其他教程的误导,采用了caffe的解决办法,将
-D_FORCE_INLINES
设置项加入到了根目录的CMakeList.txt文件中,其实这是不对的!

正确的做法应该是,将文件
opencv目录/cmake/OpencvDetectCuda.cmake


set(NVCC_FLAGS_EXTRA ${NVCC_FLAGS_EXTRA} -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})


修改为:

set(NVCC_FLAGS_EXTRA ${NVCC_FLAGS_EXTRA} -D_FORCE_INLINES -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})


这样就可以成功解决问题了!

install的错误整理4:

hdf5.hpp:40:18: fatal error: hdf5.h: not found


找到文件opencv目录下的modules/python/common.cmake

在最开始:

添加两句话,将下面的代码修改:

# This file is included from a subdirectory
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")

ocv_add_module(${MODULE_NAME} BINDINGS)


变成:

# This file is included from a subdirectory
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")

find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIRS})

ocv_add_module(${MODULE_NAME} BINDINGS)


解决问题!

OK以上就是我为大家带来的opencv3.1.0部署详细解析,花费了作者两天的时间,的确是煎熬,还是本着让更多人少走弯路的心态,谢谢大家阅读,转载请注明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu opencv