淘先锋技术网

首页 1 2 3 4 5 6 7

根据项目需要,安装opencv并提供给开发使用,并且使用opencv提供python3的API接口。虽然不知道是个啥,还是简单了解下。

opencv是什么?

OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行,可以在商业和研究领域中免费使用。OpenCV可用于开发实时的图像处理、计算机视觉以及模式识别程序。该程序库也可以使用英特尔公司的IPP(收费库)进行加速处理。

环境信息:

  • opencv-3.4.4 //3.x版本以上建议使用7系列来安装,省掉超多升级步骤
  • opencv_contrib-3.4.4 //opencv3以后 SIFT 和 SURF 之类的属性被移到了 contrib 中
  • centos7
  • gcc 4.8.x
  • cmake 2.8.x
  • pyenv //shell脚本编写的python版本管理工具
  • pyenv-virtualenv //虚拟环境
  • python 3.x //使用opencv提供的python3的API接口

一、下载源码包

二、安装python环境

  1. 安装pyenv
    注:pyenv和python环境安装可参考:https://www.cnblogs.com/dance-walter/p/9364652.html

  2. 安装依赖包
//安装编译包
~]# yum install -y cmake gcc gcc-c++  
//安装图形特性功能
]# yum install -y gtk* libdc1394-devel libv4l-devel ffmpeg-devel gstreamer-plugins-base-devel
//安装图片功能
]# yum install -y libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel

三、安装python依赖库

~]# pyenv virtualenv 3.4.1 opencv   //创建一个opencv别名的python3.4.1版本的环境
~]# pyenv activate opencv           //切换至虚拟opencv环境中
(opencv) ~]# pip install numpy==1.13.1    //根据项目需要选择性安装
(opencv) ~]# pip install dlib==19.9.0
(opencv) ~]# pip install scipy==0.18.0

四、编译安装opencv

在linux下安装gtk-devel时会引入安装python2的安装,导致在使用cmake时会检测到两个python版本,而cmake会倾向于使用低版本的python2。

错误过程

(opencv) ~]# tar xf opencv-3.4.4.tar.gz -C /usr/local/pkg/
(opencv) ~]# tar xf opencv_contrib-3.4.4.tar.gz -C /usr/local/pkg/
(opencv) ~]# cd /usr/local/pkg/
(opencv) ~]# mkdir opencv-3.4.4/build    //创建build编译目录
(opencv) ~]# cd opencv-3.4.4/build
(opencv) ~]# cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=/usr/local/pkg/opencv_contrib-3.4.4/modules ..
....
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Performing Test HAVE_CXX11 (check file: cmake/checks/cxx11.cpp)
-- Performing Test HAVE_CXX11 - Failed
**-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.5", minimum required is "2.7") 
-- Found PythonLibs: /lib64/libpython2.7.so (found suitable exact version "2.7.5")**    //这里已经查找到相应的环境信息
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
**-- Found PythonInterp: /usr/local/python/.pyenv/versions/3.4.1/bin/python (found suitable version "3.4.1", minimum required is "3.2") 
-- Could NOT find PythonLibs (missing:  PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is exact version "3.4.1")** 
//这里没有发现PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS环境信息。需要要在cmake时指定相应信息
....
--   OpenCL:                        YES (no extra features)
--     Include path:                /usr/local/pkg/opencv-3.4.4/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
**--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.5)
--     Libraries:                   /lib64/libpython2.7.so (ver 2.7.5)
--     numpy:                       /usr/lib64/python2.7/site-packages/numpy/core/include (ver 1.13.1)
--     packages path:               lib/python2.7/site-packages**
//这不是我想要的,需求是python3
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/lib/jvm/java/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local


正确过程

...
(opencv) ~]# cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D INSTALL_PYTHON_EXAMPLES=ON \
**-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \**
-D PYTHON_EXECUTABLE=$(which python) \
-D PYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D OPENCV_EXTRA_MODULES_PATH=/usr/local/pkg/opencv_contrib-3.4.4/modules ..
...
--   OpenCL:                        YES (no extra features)
--     Include path:                /usr/local/python/opencv-3.4.4/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/local/python/.pyenv/shims/python3 (ver 3.4.1)
--     Libraries:                   /usr/local/python/.pyenv/versions/3.4.1/lib (ver 3.4.1)
--     numpy:                       /usr/local/python/.pyenv/versions/3.4.1/lib/python3.4/site-packages/numpy/core/include (ver 1.13.1)
--     packages path:               lib/python3.4/site-packages
-- 
--   Python (for build):            /usr/local/python/.pyenv/shims/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/lib/jvm/java/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/python/opencv-3.4.4/build
(opencv) ~]# make -j 8    //使用几个CPU来运,越多速度越快
(opencv) ~]# make install 
....
-- Set runtime path of "/usr/local/python/cv2/python-3.4/cv2.cpython-34m.so" to "/usr/local/lib64"
...
//这时已经安装好,并且生成了相应.so文件,我们只需把库文件拷贝到相应python版本的库目录下即可
(opencv) ~]# cp -a /usr/local/python/cv2/python-3.4/cv2.cpython-34m.so /usr/local/pkg/.pyenv/versions/3.4.1/lib/python3.4/cv2.so
(opencv) ~]#

六、测试

(opencv) ~]# python
>>> import cv2
>>> cv2.__version__
'3.4.4'

官方文档:https://docs.opencv.org/3.4.4/dd/dd5/tutorial_py_setup_in_fedora.html#gsc.tab=0

转载于:https://www.cnblogs.com/dance-walter/p/10155342.html