RPi:Install Open CV

システムを最新版に更新

以下のコマンドを実行して、最新版に更新します。
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

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

$ 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

opencvで動作確認

Pythonから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
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の動作確認できた。
(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/