在Ubuntu16.04环境中编译OpenCV3.4.1的python版本,python为Anaconda3环境中的某个指定版本
1.使用Anaconda3创建指定的Python环境
这里是我为tensorflow创建的python环境,但我也想让该环境支持OpenCV,在这里我选择自己编译OpenCV的源码。
conda create -n tensorflow python=3.5.2
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.6.0-cp35-cp35m-linux_x86_64.whl
source activate tensorflow
(tensorflow) king@king-desktop:~/source/opencv-3.4.1$
2.编译OpenCV3.4.1
保持在(tensorflow)这个环境下,运行如下脚步:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D PYTHON_DEFAULT_EXECUTABLE=$(python -c "import sys; print(sys.executable)") \
-D PYTHON3_EXECUTABLE=$(python -c "import sys; print(sys.executable)") \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$(python -c "import numpy; print (numpy.get_include())") \
-D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D CMAKE_CXX_FLAGS="-std=c++11" \
-D CUDA_NVCC_FLAGS="--compiler-options "-std=c++03"" ..
在cmake过程中脚本会找到我们在anaconda中配置的tensorflow这个环境,python的输出如下:
-- Python 3:
-- Interpreter: /home/king/anaconda3/envs/tensorflow/bin/python (ver 3.5.2)
-- Libraries: /home/king/anaconda3/envs/tensorflow/lib/libpython3.5m.so (ver 3.5.2)
-- numpy: /home/king/anaconda3/envs/tensorflow/lib/python3.5/site-packages/numpy/core/include (ver 1.13.1)
-- packages path: /home/king/anaconda3/envs/tensorflow/lib/python3.5/site-packages
--
-- Python (for build): /home/king/anaconda3/envs/tensorflow/bin/python
如果是上面的输出,表明已经正确的找到了我们想使用的环境。
3安装(make install)
在安装过程中会有如下的输出:
-- Installing: /home/king/anaconda3/envs/tensorflow/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
-- Set runtime path of "/home/king/anaconda3/envs/tensorflow/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so" to "/usr/local/lib:/usr/local/cuda/lib64"
上面的输出表明已经将opencv安装到了,指定的Anaconda环境中了。
4编译OpenCV过程中C++代码和CUDA代码报错的解决方式
解决方式就是上面脚本的最后两行,如果不加会有如下报错:
/usr/local/include/vtk-8.1/vtkSetGet.h:655:12: error: ‘nullptr’ was not declared in this scope
/usr/local/include/vtk-8.1/vtkSetGet.h:110:22: error: ‘nullptr’ was not declared in this scope
/usr/local/include/vtk-8.1/vtkSetGet.h:123:18: error: ‘nullptr’ was not declared in this scope
/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h(436): error: identifier "nullptr" is undefined
/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h(436): error: expected a ";"
/usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h(200): error: expected a ";"
/usr/include/c++/5/exception(63): error: expected a ";"
/usr/include/c++/5/exception(68): error: expected a ";"
/usr/include/c++/5/exception(76): error: expected a ";"
/usr/include/c++/5/exception(83): error: expected a ";"
/usr/include/c++/5/exception(93): error: expected a "{"
/usr/include/c++/5/bits/exception_ptr.h(64): error: function "std::current_exception" returns incomplete type "std::__exception_ptr::exception_ptr"
到此博客完成,但我也遇到了个问题,python代码调用cv2没有问题,但conda list里面却没有opencv的安装记录。可能是我没有使用安装包安装,而是直接通过make install拷贝过去的原因。如果谁能教我,使用opencv源码编译出opencv的python安装包来,我将不胜感激。