樹莓派安裝opencv3.4.1

前言

opencv是圖像處理的利器,內(nèi)置了許多圖像處理方法的庫,當(dāng)然也帶了人臉檢測等功能的庫及example,并且支持Intel深度學(xué)習(xí)推理引擎的計算加速。
通過查看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

如果過程中有遇到網(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小時。

  • 安裝
sudo make install
  • 單元測試驗證
./bin/opencv_test_core

運行各個核心組件的單元測試程序,該測試是在google test框架下寫的,均顯示OK說明安裝成功,筆者這里遇到了一個Failure,原因是其中一個測試case需要有圖片輸入,因此和opencv的安裝無關(guān),規(guī)避該錯誤需要拷貝任意.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獲取攝像頭數(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()
  • 錯誤一
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)致,運行命令
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 +后重新登錄當(dāng)前賬戶

運行起來效果:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容