前言
opencv是图像处理的利器,内置了许多图像处理方法的库,当然也带了人脸检测等功能的库及example,并且支持Intel深度学习推理引擎的计算加速。
通过查看opencv-3.4.1的更新日志,我们看到如果使用加速引擎,推理效率非常高(见https://github.com/opencv/opencv/wiki/ChangeLog#version341):
Model | CPU, default backend | CPU, Inference Engine backend, MKL-DNN plugin | Model Optimizer + Inference Engine, MKL-DNN plugin (a standalone application) |
---|---|---|---|
AlexNet | 14.44ms | 12.09ms (x1.19) | 12.05ms |
GoogLeNet | 15.26ms | 8.92ms (x1.71) | 8.75ms |
ResNet-50 | 35.78ms | 19.53ms (x1.83) | 19.4ms |
SqueezeNet v1.1 | 4.01ms | 2.60ms (x1.54) | 2.5ms |
MobileNet-SSD from Caffe | 21.62ms | 8.89ms (x2.43) | |
DenseNet-121 | 61.71ms | 28.21ms (x2.18) | |
OpenPose (COCO) @ 368x368 | 885.57ms | 544.05ms (x1.62) | |
OpenPose (MPI) @ 368x368 | 879.13ms | 533.96ms (x1.64) | |
OpenPose (MPI, 4 stages) @ 368x368 | 605.63ms | 378.49ms (x1.60) | |
OpenFace | 3.84ms | 2.59ms (x1.48) |
opencv安装
opencv源码下载
我们选择最新的opencv-3.4.1,下载:https://github.com/opencv/opencv/archive/3.4.1.zip
安装方法参考:
https://docs.opencv.org/3.4.1/d7/d9f/tutorial_linux_install.html
依赖项及安装
- 依赖项
Required Packages
GCC 4.4.x or later
CMake 2.8.7 or higher
Git
GTK+2.x or higher, including headers (libgtk2.0-dev)
pkg-config
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
[optional] libtbb2 libtbb-dev
[optional] libdc1394 2.x
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
[optional] CUDA Toolkit 6.5 or higher
- 安装
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
如果过程中有遇到网络问题导致安装失败的,可以通过安装命令后加 --fix-missing
再次安装,如sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev --fix-missing
,若一直失败,可以通过更改源的方式再次尝试,如:
sudo -s
echo -e "deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi \n deb-src http://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi" > /etc/apt/sources.list
echo -e "deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/ stretch main ui" > /etc/apt/sources.list.d/raspi.list
exit
sudo apt update && sudo apt -y upgrade
opencv编译
- 解压源码包
unzip opencv-3.4.1.zip
- 创建build目录及配置
cd opencv-3.4.1/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
- 编译
make
编译过程大概4小时。
- 安装
sudo make install
- 单元测试验证
./bin/opencv_test_core
运行各个核心组件的单元测试程序,该测试是在google test框架下写的,均显示OK说明安装成功,笔者这里遇到了一个Failure,原因是其中一个测试case需要有图片输入,因此和opencv的安装无关,规避该错误需要拷贝任意.jpg和.png图片各一张到build/bin/
目录下并重命名为lena.jpg和lena.png。
笔者遇到的错误:
/home/yinan/install/opencv/opencv-3.4.1/modules/core/test/test_io.cpp:562: Failure
Expected: (lenas.size()) > (pngLenas.size()), actual: 0 vs 0
[ FAILED ] Core_globbing.accuracy (479 ms)
...
[----------] Global test environment tear-down
[==========] 10532 tests from 210 test cases ran. (724467 ms total)
[ PASSED ] 10531 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] Core_globbing.accuracy
1 FAILED TEST
YOU HAVE 10 DISABLED TESTS
- 验证python中引用opencv
yinan@raspberrypi:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
正常引用cv2无错误说明一切OK!
- 运行opencv获取摄像头数据,代码:
#!/usr/bin/env python
import cv2
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
print "frame.shape: {}".format(frame.shape)
cv2.imshow("capture", frame)
if cv2.waitKey(100) & 0xff == ord("q"):
break
cv2.destroyAllWindows()
- 错误一
Traceback (most recent call last):
File "video.py", line 13, in <module>
print "frame.shape: {}".format(frame.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
这是由于当前用户对/dev/video0没有权限导致,运行命令
sudo chmod 666 /dev/video0
- 错误二
frame.shape: (480, 640, 3)
MobaXterm X11 proxy: Unsupported authorisation protocol
(capture:1404): Gtk-WARNING **: cannot open display: localhost:11.0
使用pi账户运行命令xhost +
后重新登录当前账户
运行起来效果: