[PYTHON] Build an OpenCV4 environment on Raspberry Pi using Poetry

Introduction

OpenCV4 environment on Raspberry Pi 4 (OS: Raspbian Lite) using Poetry, a tool for managing Python dependencies. When I built it, I personally had some addictive points, so I'll leave it here as a note.

If you have any mistakes or improvements, please let us know.

It is assumed that Poetry is pre-installed. This article may be helpful for installing and using Poetry.

environment

The environment on the Raspberry Pi side is as follows.

Model Raspberry Pi 4 Model B 4GB
OS Raspbian Buster Lite
Poetry version 1.0.5
Python 3.7.3
X Server X.Org X Server 1.20.4
Command and execution result when checking version information
$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster
$ sudo cat /etc/debian_version
10.3
$ poetry --version
Poetry version 1.0.5
$ python3.7 -V
Python 3.7.3
$ Xorg -version

X.Org X Server 1.20.4
X Protocol Version 11, Revision 0
Build Operating System: Linux 4.15.0-48-generic armv8l Raspbian
Current Operating System: Linux pi4b-02 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l
Kernel command line: coherent_pool=1M 8250.nr_uarts=0 cma=64M cma=256M  smsc95xx.macaddr=DC:A6:32:70:FB:B0 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000  console=ttyS0,115200 console=tty1 root=PARTUUID=6c586e13-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Build Date: 05 June 2019  12:49:54PM
xorg-server 2:1.20.4-1+rpt1 (https://www.debian.org/support)
Current version of pixman: 0.36.0
        Before reporting problems, check http://wiki.x.org
        to make sure that you have the latest version.

Also, X transfer is used when displaying an image as an operation check. The environment on the client side used when checking the operation is as follows.

OS Windows 10 Pro (version: 1903)
SSH client WSL 1(Ubuntu 18.04)
VcXsrv X Server Version 1.20.6.0(12 Jan 2020)

Creating a virtual environment

Move to the working directory and create a virtual environment with the following command.

$ cd ~/work/OpencvPythonExperiment/  #Move to any working directory
$ python3.7 -m venv .venv  #Create a virtual environment

When creating a virtual environment, the following message may be displayed. (I haven't checked it properly, but in an environment where pip is not installed, the following message is displayed?)

$ python3.7 -m venv .venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

In this case, it was solved by executing the following command.

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python3.7-venv  #Explicitly specify the minor version and install
$ python3.7 -m venv .venv  #Create a virtual environment

OpenCV installation

Creating a pyproject.toml file

You can go to your working directory and interactively create a pyproject.toml file with the following command:

$ poetry init
(Omitted below)

After inputting properly, the following file was generated. If you don't want to create the pyproject.toml file yourself, just copy and paste the following content and you'll be fine.

pyproject.toml


name = "opencv-python-experiment"
version = "0.1.0"
description = ""
authors = ["Takahiro55555"]

[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

OpenCV installation

I ran the following command and tried to install OpenCV but it failed.

$ poetry add opencv-contrib-python

Specifically, the following error was displayed.

[EnvCommandError]
Command ['/home/pi/work/OpencvPythonExperiment/.venv/bin/pip', 'install', '--no-deps', 'opencv-contrib-python==4.2.0.34'] errored with the following return code 1, and output:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-contrib-python==4.2.0.34
  Could not find a version that satisfies the requirement opencv-contrib-python==4.2.0.34 (from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.6.27, 3.4.7.28, 4.0.1.24, 4.1.0.25, 4.1.1.26)
No matching distribution found for opencv-contrib-python==4.2.0.34

Looking at the error, it seems that the error occurred because the version of OpenCV you are trying to install is not in the list of installable OpenCV versions.

So I explicitly specified the version of OpenCV that I could install and tried to install it again, but a new error occurred.

$ poetry add opencv-contrib-python=4.1.1.26

Specifically, the following error was displayed.

[EnvCommandError]
Command ['/home/pi/work/OpencvPythonExperiment/.venv/bin/pip', 'install', '--no-deps', 'opencv-contrib-python==4.1.1.26'] errored with the following return code 1, and output:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-contrib-python==4.1.1.26
  Downloading https://www.piwheels.org/simple/opencv-contrib-python/opencv_contrib_python-4.1.1.26-cp37-cp37m-linux_armv7l.whl (15.9MB)
THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
    opencv-contrib-python==4.1.1.26 from https://www.piwheels.org/simple/opencv-contrib-python/opencv_contrib_python-4.1.1.26-cp37-cp37m-linux_armv7l.whl#sha256=8000d53db64bc9b5b093d9cae4cecb540af97f1dfdd967d8ac29bc5f5032528b:
        Expected sha256 8000d53db64bc9b5b093d9cae4cecb540af97f1dfdd967d8ac29bc5f5032528b
             Got        47247bbcadb068654922b0d94b3cc5f8437a3ab74d549668efec2e489a5f21aa

So, after updating pip in the virtual environment, I tried to install OpenCV again and the installation was successful.

$ .venv/bin/pip install -U pip
$ poetry add opencv-contrib-python=4.1.1.26

OpenCV import errors and solutions

Immediately, I tried to use OpenCV, but the following error occurred at ʻimport`.

$ poetry shell
(.venv) $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.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 "/home/pi/work/OpencvPythonExperiment/.venv/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: libhdf5_serial.so.103: cannot open shared object file: No such file or directory

Installation of packages corresponding to errors

[This article](https://qiita.com/atuyosi/items/5f73baa08c3408f248e8#%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%A8%E5%AF%BE % E5% BF% 9C% E3% 81% 99% E3% 82% 8B% E3% 83% 91% E3% 83% 83% E3% 82% B1% E3% 83% BC% E3% 82% B8% E5 % 90% 8D% E3% 81% AE% E3% 83% AA% E3% 82% B9% E3% 83% 88), and installed the missing package. In my environment, some of the packages installed in the above reference article are out of date or missing, so the ones that have been fixed or added are shown below.

The following is a summary of the installed packages.

sudo apt install libhdf5-103
sudo apt install libharfbuzz0b
sudo apt install liblapack3
sudo apt install libatlas-base-dev
sudo apt install libwebp6
sudo apt install libtiff5
sudo apt install libjasper1
sudo apt install libilmbase23
sudo apt install libopenexr23
sudo apt install libavcodec-extra58
sudo apt install libavformat58
sudo apt install libswscale5
sudo apt install libqtgui4
sudo apt install libqt4-test 

HDF5

Error.txt


ImportError: libhdf5_serial.so.103: cannot open shared object file: No such file or directory

-> sudo apt install libhdf5-103

HarfBuzz

Error.txt


ImportError: libharfbuzz.so.0: cannot open shared object file: No such file or directory

-> sudo apt install libharfbuzz0b

???

Error.txt


ImportError: liblapack.so.3: cannot open shared object file: No such file or directory

-> sudo apt install liblapack3

???

Error.txt


ImportError: libcblas.so.3: cannot open shared object file: No such file or directory

-> sudo apt install libatlas-base-dev

WebP

Error.txt


ImportError: libwebp.so.6: cannot open shared object file: No such file or directory

-> sudo apt install libwebp6

???

Error.txt


ImportError: libtiff.so.5: cannot open shared object file: No such file or directory

-> sudo apt install libtiff5

Jasper

Error.txt


ImportError: libjasper.so.1: cannot open shared object file: No such file or directory

-> sudo apt install libjasper1

???

Error.txt


ImportError: libImath-2_2.so.23: cannot open shared object file: No such file or directory

-> sudo apt install libilmbase23

OpenEXR

Error.txt


ImportError: libIlmImf-2_2.so.23: cannot open shared object file: No such file or directory

-> sudo apt install libopenexr23

FFmpeg related

Error.txt


ImportError: libavcodec.so.58: cannot open shared object file: No such file or directory

-> sudo apt install libavcodec-extra58

Error.txt


ImportError: libavformat.so.58: cannot open shared object file: No such file or directory

-> sudo apt install libavformat58

Error.txt


ImportError: libswscale.so.5: cannot open shared object file: No such file or director

-> sudo apt install libswscale5

Qt related

Error.txt


ImportError: libQtGui.so.4: cannot open shared object file: No such file or directory

-> sudo apt install libqtgui4

Error.txt


ImportError: libQtTest.so.4: cannot open shared object file: No such file or directory

-> sudo apt install libqt4-test

Errors caused by something other than missing packages

[This article](https://qiita.com/XM03/items/48463fd910470b226f22#%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E7%AA%81 % E7% A0% B4% E6% 96% B9% E6% B3% 95), and the problem was solved.

Error.txt


ImportError: /home/pi/work/OpencvPythonExperiment/.venv/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8

Before modifying .bashrc, verify that the solution in the above article works.

$ poetry shell  #Activate the virtual environment
(.venv) $ export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1  #The solution described in the article above
(.venv) $ python  #Operation check
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1
>>> exit()  #Exit the interpreter

When I checked the version of OpenCV as a trial, I was able to confirm it safely. So, add the following to .bashrc.

$ echo "export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1" >> ~/.bashrc
$ poetry shell  #Activate the virtual environment
(.venv) $ python  #Operation check
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1'

Again, I was able to confirm that no error occurred.

Try using OpenCV

Try to display the image using OpenCV. The image file (lenna.png) is placed in the directory where .venv is located in advance.

$ ls -a
.  ..  lenna.png  poetry.lock  pyproject.toml  .venv
$ poetry shell  #Activate the virtual environment
(.venv) $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import cv2
>>> img = cv2.imread("lenna.png ")
>>> cv2.imshow("color",img)
>>> cv2.waitKey(0)  #Select the image window and press any key to re-enter the interpreter
235
>>> cv2.destroyAllWindows()

It was displayed successfully as below (using X transfer). opencv-on-the-raspberry-py02.PNG

Supplement

[Referenced articles](https://qiita.com/atuyosi/items/5f73baa08c3408f248e8#%E3%82%A8%E3%] in [Installing packages corresponding to errors](#Installing packages corresponding to errors) 83% A9% E3% 83% BC% E3% 81% A8% E5% AF% BE% E5% BF% 9C% E3% 81% 99% E3% 82% 8B% E3% 83% 91% E3% 83% 83% E3% 82% B1% E3% 83% BC% E3% 82% B8% E5% 90% 8D% E3% 81% AE% E3% 83% AA% E3% 82% B9% E3% 83% 88) Then, I also installed Gtk3 as follows.

ImportError: libgtk-3.so.0: cannot open shared object file: No such file or directory

-> libgtk-3-0 (libgtk-3-dev)

I didn't get any Gtk3 related errors in my environment, but it might be helpful if I get Gtk3 related errors.

References

Recommended Posts

Build an OpenCV4 environment on Raspberry Pi using Poetry
Build an Arch Linux environment on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Build a Django environment on Raspberry Pi (MySQL)
Build a Python development environment on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Build an environment for machine learning using Python on MacOSX
Detect "brightness" using python on Raspberry Pi 3!
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a Tensorflow environment with Raspberry Pi [2020]
Build an LNPP environment on Amazon Linux 2
Build and try an OpenCV & Python environment in minutes using Docker
Run servomotor on Raspberry Pi 3 using python
Create an OpenCV3 + python3 environment on OSX
Detect temperature using python on Raspberry Pi 3!
Build a Python + OpenCV environment on Cloud9
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Detect slide switches using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Sound the buzzer using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
How to build an environment for using multiple versions of Python on Mac
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
Build a Django development environment using pyenv-virtualenv on Mac
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Build a Python environment on your Mac using pyenv
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Build a Python development environment using pyenv on MacOS
Output to "7-segment LED" using python on Raspberry Pi 3!
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Indoor monitoring using Raspberry Pi
Build an Anaconda virtual environment
Try using OpenCV on Windows
Build Python environment on Windows
Build python environment on windows
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install TensorFlow 1.15.0 on Raspberry Pi
Build a machine learning environment on mac (pyenv, deeplearning, opencv)
Build an Ubuntu python development environment on Google Cloud Platform
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Access google spreadsheet using python on raspberry pi (for myself)
Introducing Kaggle's Docker Image on Windows to build an environment
Build an environment on windows10 where you can try MXNet
Construction of Cortex-M development environment for TOPPERS using Raspberry Pi
Testing uart communication on Raspberry Pi
Build a Chainer environment using CUDA and cuDNN on a p2 instance
Control the motor with a motor driver using python on Raspberry Pi 3!
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Resolved an error when putting pygame in python3 on raspberry pi
Build a go environment using Docker
Install ghoto2 on Raspberry Pi (memo)
Build and install OpenCV on Windows