CentOS6 - Linux下安装OpenCV
项目需要做人脸识别,通过人脸进行自动切图,开源项目就OpenCV比较全的文档,且有Java的实现方案,所以就使用OpenCV进行开发。不过OpenCV进行人脸识别需要安装很多库文件和依赖,过程繁琐且复杂,为了避免再次入坑,下面就把这个过程记录下来,以备不时之需。
准备:以下需要提前做,否则后面会报错:
1、升级gcc版本:
# curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo
# yum install gcc gcc-g++ -y
# gcc --version
# g++ --version
1. 安装依赖
Java--即JDK,此处略
yum -y install epel-release
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
yum install -y gcc gcc-c++ make cmake ant ImageMagick ImageMagick-devel GraphicsMagick autoconf automake libpng-devel libjpeg-devel libtiff-devel jasper-devel freetype-devel libtool-ltdl libtool-ltdl-devel* libtool-ltdl libtool-ltdl-devel libgnomeui-devel
2.ant 验证
ant --help
3. ImageMagick验证
convert -version
4. GraphicsMagick验证
gm
或者
gm convert -list formats
安装OpenCV
1. 安装OpenCV依赖库(可能和前面yum安装有重复,忽略)
yum install -y gcc gcc-c++ cmake camke-gui git pkgconfig gtk2 gtk2-devel gtk2-devel-docs python python-pip python-devel python-numpy numpy gimp-devel gimp-devel-tools gimp-help-browser libjpeg-devel libtiff-devel libpng-devel gstreamer-devel gstreamer-plugins-base-devel libv4l libv4l-devel mencoder flvtool2 libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils zlib-devel unzip swig libtool nasm libgnomeui-devel gnome-devel gnome-devel-docs kernel-devel gcc-gfortran ffmpeg ffmpeg-devel
升级 binutils至2.25
#wget http://ftp.gnu.org/gnu/binutils/binutils-2.25.1.tar.bz2
#tar -xjf binutils-2.25.1.tar.bz2
# cd binutils-2.25.1
# ./configure --prefix=/usr
# make
# make install
# as --version
# objdump -v
# ld -v
#若安装 ffmpeg-devel flvtool2 失败,记得执行以下步骤:
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
2. 下载
可以到官网下载 选择对应版本,我这里是3.4.1
https://codeload.github.com/opencv/opencv/zip/3.4.1
wget https://codeload.github.com/opencv/opencv/zip/3.4.1 --no-check_certificate
3. 解压
先解压,然后进入解压目录,创建 build 目录,进入build 目录,准备预编译
unzip opencv-3.4.1.zip
cd opencv-3.4.1
mkdir build && cd build/
4. 预编译
通过cmake命令预先编译一次,编译完成后查看输出结果, 在To be built里包含java这一项就表示预编译成功
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TIFF=ON -DWITH_GPHOTO2=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ..
预编译过程中,在下载ippicv_linux_xxxxxxxx.tgz这个文件会慢,(这里有一个解决方案,就是先终止预编译,然后手动下载后,放入opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ 目录, 然后去CSDN下载地址 下载, 也可以去官网获取下载地址,然后用迅雷下。 注意:linux-808b791a6eac9ed78d32a7666804320e目录名称是根据你的机器随机变化的,每个人的服务器上 linux- 后面可能不一样.)
5. 编译
cmake完成后,在build目录直接输入make进行编译,建议不使用-j n,多线程编译可能会出问题。
make
############################################
编译过程可能出现异常,提示:
1、
假如没有更新gcc版本,则会报如下错误:
In file included from /usr/local/src/opencv-3.4.1/3rdparty/include/opencl/1.2/CL/cl.h:30,
from /usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp:101,
from /usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/opencl/runtime/opencl_core.hpp:68,
from /usr/local/src/opencv-3.4.1/modules/core/src/va_intel.cpp:22:
/usr/local/src/opencv-3.4.1/3rdparty/include/opencl/1.2/CL/cl_platform.h:214: error: reference to ‘int8_t’ is ambiguous
/usr/include/stdint.h:37: error: candidates are: typedef signed char int8_t
/usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/cvdef.h:486: error: typedef int8_t cv::int8_t
/usr/local/src/opencv-3.4.1/3rdparty/include/opencl/1.2/CL/cl_platform.h:214: error: reference to ‘int8_t’ is ambiguous
/usr/include/stdint.h:37: error: candidates are: typedef signed char int8_t
/usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/cvdef.h:486: error: typedef int8_t cv::int8_t
……………………
/usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp:370: error: redefinition of ‘int cl_int’
/usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp:283: error: ‘int cl_int’ previously defined here
/usr/local/src/opencv-3.4.1/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp:370: error: ‘clWaitForEvents_pfn’ was not declared in this scope
make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/va_intel.cpp.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make: *** [all] Error 2
解决办法:
升级gcc版本至4.8,重新预编译:cmake后,再make。
2、
假如没有升级binutils的版本
[ 49%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/convert.avx2.cpp.o
/tmp/ccA6i8a9.s: Assembler messages:
/tmp/ccA6i8a9.s:11: Error: suffix or operands invalid for `vbroadcastss'
/tmp/ccA6i8a9.s:12: Error: suffix or operands invalid for `vbroadcastss'
/tmp/ccA6i8a9.s:27: Error: no such instruction: `vpermq $216,%ymm2,%ymm2'
/tmp/ccA6i8a9.s:28: Error: suffix or operands invalid for `vpunpcklwd'
/tmp/ccA6i8a9.s:29: Error: suffix or operands invalid for `vpunpckhwd'
/tmp/ccA6i8a9.s:30: Error: suffix or operands invalid for `vpsrad'
/tmp/ccA6i8a9.s:33: Error: suffix or operands invalid for `vpsrad'
make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/convert.avx2.cpp.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make: *** [all] Error 2
解决办法:
升级 binutils至2.25
#wget http://ftp.gnu.org/gnu/binutils/binutils-2.25.1.tar.bz2
#tar -xjf binutils-2.25.1.tar.bz2
# cd binutils-2.25.1
# ./configure --prefix=/usr
# make
# make install
# as --version
# objdump -v
# ld -v
3、
/usr/include/jasper/jas_math.h:117:22: error: ‘SIZE_MAX’ was not declared in this scope
出现这个异常,不要慌,可以通过修改/usr/include/jasper/jas_math.h 文件源码解决。
在/usr/include/jasper/jas_math.h 的头部#include 的下面添加:
#if ! defined SIZE_MAX
#endif
然后重新编译就可以了。如果不行,请删除build目录下的内容,重新预编译、编译就应该没问题了
4、
假如没有升级libgphonto2,会报如下错误:
[ 80%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_gphoto2.cpp.o
/usr/local/src/opencv-3.4.1/modules/videoio/src/cap_gphoto2.cpp: In member function ‘void cv::gphoto2::DigitalCameraCapture::initContext()’:
/usr/local/src/opencv-3.4.1/modules/videoio/src/cap_gphoto2.cpp:323:66: error: invalid conversion from ‘void (*)(GPContext*, const char*, void*) ’ to ‘GPContextErrorFunc ’ [-fpermissive]
gp_context_set_error_func(context, ctxErrorFunc, (void*) this);
………………………………
/usr/local/src/opencv-3.4.1/modules/videoio/src/cap_gphoto2.cpp:248:27: note: in definition of macro ‘CR’
if ((r_0629c47b758 = (GPHOTO2_FUN))
^
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_gphoto2.cpp.o] Error 1
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2
解决办法:编译升级 libgphonto2 phonto2 至2.5版本(未验证) 或 cmake时 加入-DWITH_GPHOTO2=OFF
wget https://nchc.dl.sourceforge.net/project/gphoto/libgphoto/2.5.8/libgphoto2-2.5.8.tar.bz2 --no-check_certificate
wget https://nchc.dl.sourceforge.net/project/gphoto/gphoto/2.5.8/gphoto2-2.5.8.tar.bz2 --no-check_certificate
5、没有升级python2.7 会报如下错误:
[ 91%] Generate files for Java bindings
File "/usr/local/src/opencv-3.4.1/modules/java/generator/../generator/gen_java.py", line 1079
^
SyntaxError: invalid syntax
make[2]: *** [CMakeFiles/dephelper/gen_opencv_java_source] Error 1
make[1]: *** [modules/java_bindings_generator/CMakeFiles/gen_opencv_java_source.dir/all] Error 2
make: *** [all] Error 2
解决办法:升级python至2.7,不要用3,
# wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz ; --no-check-certificate
# xz -d Python-2.7.11.tar.xz
# tar -xf Python-2.7.11.tar
# cd Python-2.7.11
# ./configure
# make
# make install
# mv /usr/bin/python /usr/bin/python.bak
# ln -s /usr/local/bin/python2.7 /usr/bin/python
# vi /usr/bin/yum
!/usr/bin/python,修改成:!/usr/bin/python2.6
###############################################################################################################
6. 安装
编译完成,就可以进行安装,安装过程是生成opencv对应的库文件,我这里是java项目需要用到,所以也会顺便生成java相关的库文件
make install
命令执行完成,会在结果中看到/usr/local/share/OpenCV/java/目录生成了两个java相关的依赖库文件 opencv-341.jar 和 libopencv_java341.so , 当然,每个人的环境不同,可能目录也不同,具体看日志输出就行了。
# ll /usr/local/share/OpenCV/java/ -h
total 69M
-rwxr-xr-x 1 root root 68M May 24 22:57 libopencv_java341.so
-rw-r--r-- 1 root root 413K May 24 22:54 opencv-341.jar
测试
先略,后期添加
领取专属 10元无门槛券
私享最新 技术干货