前言
opencv是圖像處理的利器远荠,內(nèi)置了許多圖像處理方法的庫矮固,當(dāng)然也帶了人臉檢測等功能的庫及example,并且支持Intel深度學(xué)習(xí)推理引擎的計(jì)算加速譬淳。
通過查看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
依賴項(xiàng)及安裝
- 依賴項(xiàng)
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
如果過程中有遇到網(wǎng)絡(luò)問題導(dǎo)致安裝失敗的守伸,可以通過安裝命令后加 --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
- 創(chuàng)建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小時(shí)剂娄。
- 安裝
sudo make install
- 單元測試驗(yàn)證
./bin/opencv_test_core
運(yùn)行各個(gè)核心組件的單元測試程序窘问,該測試是在google test框架下寫的,均顯示OK說明安裝成功宜咒,筆者這里遇到了一個(gè)Failure,原因是其中一個(gè)測試case需要有圖片輸入把鉴,因此和opencv的安裝無關(guān)故黑,規(guī)避該錯(cuò)誤需要拷貝任意.jpg和.png圖片各一張到build/bin/
目錄下并重命名為lena.jpg和lena.png儿咱。
筆者遇到的錯(cuò)誤:
/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
- 驗(yàn)證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無錯(cuò)誤說明一切OK!
- 運(yùn)行opencv獲取攝像頭數(shù)據(jù)场晶,代碼:
#!/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()
- 錯(cuò)誤一
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'
這是由于當(dāng)前用戶對/dev/video0沒有權(quán)限導(dǎo)致混埠,運(yùn)行命令
sudo chmod 666 /dev/video0
- 錯(cuò)誤二
frame.shape: (480, 640, 3)
MobaXterm X11 proxy: Unsupported authorisation protocol
(capture:1404): Gtk-WARNING **: cannot open display: localhost:11.0
使用pi賬戶運(yùn)行命令xhost +
后重新登錄當(dāng)前賬戶
運(yùn)行起來效果: