RPi:Install Open CV

Contents

システムを最新版に更新

以下のコマンドを実行して、最新版に更新します。
pi@raspberrypi:~ $ sudo apt-get update
Get:1 http://archive.raspberrypi.org/debian buster InRelease [32.6 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Get:3 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages [13.0 MB]
Get:4 http://archive.raspberrypi.org/debian buster/main armhf Packages [330 kB]
Get:5 http://raspbian.raspberrypi.org/raspbian buster/non-free armhf Packages [104 kB]
Fetched 13.5 MB in 11s (1,246 kB/s)
Reading package lists… Done
pi@raspberrypi:~ $ sudo apt-get upgrade
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
The following package was automatically installed and is no longer required:
rpi-eeprom-images
Use ‘sudo apt autoremove’ to remove it.
The following packages will be upgraded:
arandr ca-certificates firmware-atheros firmware-brcm80211 firmware-libertas firmware-misc-nonfree firmware-realtek libfm-data libfm-extra4
libfm-gtk-data libfm-gtk4 libfm-modules libfm4 libgnutls30 libnode-dev libnode64 libraspberrypi-bin libraspberrypi-dev libraspberrypi-doc
libraspberrypi0 libvlc-bin libvlc5 libvlccore9 lxpanel lxpanel-data nodejs nodejs-doc pcmanfm pi-greeter pi-package pi-package-data
pi-package-session piclone pipanel piwiz raspberrypi-bootloader raspberrypi-kernel raspberrypi-ui-mods raspi-config rp-bookshelf rp-prefapps
rpi-chromium-mods rpi-eeprom rpi-eeprom-images scratch2 vlc vlc-bin vlc-data vlc-l10n vlc-plugin-base vlc-plugin-notify vlc-plugin-qt
vlc-plugin-samba vlc-plugin-skins2 vlc-plugin-video-output vlc-plugin-video-splitter vlc-plugin-visualization
57 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 225 MB of archives.
After this operation, 7,697 kB disk space will be freed.
Do you want to continue? [Y/n] y
。。。

Processing triggers for ca-certificates (20200601~deb10u1) …
Updating certificates in /etc/ssl/certs…
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d…

done.
done.
Processing triggers for libvlc-bin:armhf (3.0.11-0+deb10u1+rpt1) …
pi@raspberrypi:~ $

 

opencvインストール

opencvインストールしてみる。
(env) pi@donkeypi3chen02:~ $ sudo pip3 install opencv-python
Collecting opencv-python

Downloading https://www.piwheels.org/simple/opencv-python/opencv_python-4.1.1.26-cp37-cp37m-linux_armv7l.whl (10.0MB)

100% |████████████████████████████████| 10.0MB 45kB/s

Requirement already satisfied: numpy>=1.16.2 in /usr/lib/python3/dist-packages (from opencv-python) (1.16.2)

Installing collected packages: opencv-python

Successfully installed opencv-python-4.1.1.26

(2023/1/18 update)最近上記のコマンドでbuildが延々と終わらない、最後にエラーになりました。数日悩んだ解決策は:

pipのバージョンが古いため起きる問題ではないかと、試す。

pi@chlab-a71b3eca:~ $ pip install –upgrade pip
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pip in /usr/lib/python3/dist-packages (20.3.4)
Collecting pip
Using cached pip-22.3.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
WARNING: The scripts pip, pip3, pip3.10 and pip3.9 are installed in ‘/home/pi/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
Successfully installed pip-22.3.1

それでも効果がない!

https://teratail.com/ でARMアーキテクチャでは cmake の問題で OpenCV のビルドに失敗するようです。ビルド済みのパッケージのインストールではいいらしい。

pi@chlab-a71b3eca:~ $ sudo apt-get update
Get:1 http://raspbian.raspberrypi.org/raspbian bullseye InRelease [15.0 kB]
Get:2 http://archive.raspberrypi.org/debian bullseye InRelease [23.6 kB]
Get:3 http://archive.raspberrypi.org/debian bullseye/main armhf Packages [311 kB]
Fetched 349 kB in 3s (124 kB/s)
Reading package lists… Done
pi@chlab-a71b3eca:~ $ sudo apt-get install python-opencv
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
E: Unable to locate package python-opencv

ビルド済みのパッケージがない?

pi@chlab-a71b3eca:~ $ sudo apt-get install python3-opencv

これてうまくいく。

opencvで動作確認

追加パッケージ

必要なライブラリが足りないと、プログラム中エラーになるので、さらに不足のライブラリを一気に追加する。

$ sudo apt-get install libjasper-dev
$ sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig libqt4-test
$ sudo apt-get install libatlas-base-dev

Pythonからopencvの動作確認する。

環境変数にLD_PRELOAD追加

(env) pi@donkeypi3chen02:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
  File “/usr/local/lib/python3.7/dist-packages/cv2/__init__.py”, line 3, in <module>
    from .cv2 import *
ImportError: /usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
>>>
ネットで調べて、次の処置で通りました。
.bashrcに、export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1 #この一文を追加
(env) pi@donkeypi3chen02:~ $ vi .bashrc
(env) pi@donkeypi3chen02:~ $ source .bashrc

ついにopencvの動作確認でき

ここまで苦労して、ついにopencvの動作確認できた。
(env) pi@donkeypi3chen02:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
>>> cv2.__version__
‘4.1.1’
>>>
ここまで、成功

参考

  • https://creepfablic.site/2020/04/10/python-opencv-error/ 
  • https://qiita.com/muramasa2/items/80a2cae31bafb9de0ea2
  • https://teratail.com/questions/301644