Raspberry Pi Tips

Install OpenCV on PyEnv (Failed as of 26 April)

Raspberry Pi 3 Model B V1.2

  $ cat /etc/debian_version
  9.11

Pre-requisite

Python versions on pyenv that I tried (failed):

  • 3.7.7
  • 3.7.5
  • 3.8.2
    $ pyenv install 3.7.7
    
    Installed Python-3.7.7 to /home/pi/.pyenv/versions/3.7.7
  
    $ pyenv local 3.7.7
  
    $ pip --version
    pip 19.2.3 from /home/pi/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pip (python 3.7)
  
    $ pip install opencv-python
    ~
    Successfully installed numpy-1.18.3 opencv-python-4.1.1.26
    WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
  
    $ pip install --upgrade pip
    ~
    Successfully installed pip-20.0.2
  
    $ pip install opencv-contrib-python
    ~
    Successfully installed opencv-contrib-python-4.1.1.26

Actually, I tried same on python 3.8.2. It successfully installed as above, but failed when I run import cv2. Seeing a thread at stackoverflow forum written in 16 Nov 2019, OpenCV suppots python 3.8 but it requires packages that is not uploaded to PyPi as of 26 April 2020. Then I tried python 3.7.7, the last version of python 3.7.


What I did (example for python 3.7.5) - failed

  $ pip install --upgrade pip
  Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
  Requirement already up-to-date: pip in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (20.0.2)

  $ pip install numpy
  Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
  Requirement already satisfied: numpy in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (1.18.3)

  $ pip install opencv-python
  Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
  Requirement already satisfied: opencv-python in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (4.1.1.26)
  Requirement already satisfied: numpy>=1.16.2 in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (from opencv-python) (1.18.3)

  $ pip install opencv-contrib-python
  Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
  Requirement already satisfied: opencv-contrib-python in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (4.1.0.25)
  Requirement already satisfied: numpy>=1.16.2 in /home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages (from opencv-contrib-python) (1.18.3)

Run opencv on pyenv 3.7.5 (Failed)

  $ python
  Python 3.7.5 (default, Apr 26 2020, 03:21:43) 
  [GCC 6.3.0 20170516] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import cv2
  Traceback (most recent call last):
    File "", line 1, in 
    File "/home/pi/.pyenv/versions/3.7.5/lib/python3.7/site-packages/cv2/__init__.py", line 3, in 
      from .cv2 import *
  ImportError: libImath-2_2.so.23: cannot open shared object file: No such file or directory

Search library and apt-get install it

  $ apt-cache search libImath
  libilmbase12 - several utility libraries from ILM used by OpenEXR

  $ sudo apt-get install libilmbase12
  パッケージリストを読み込んでいます... 完了
  依存関係ツリーを作成しています                
  状態情報を読み取っています... 完了
  libilmbase12 はすでに最新バージョン (2.2.0-12) です。
  アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 0 個。

However, import error was not solved.


What I did (example for python 3.7.7) - failed

Though opencv and related package on python 3.7.7 is successfully installed, following installation of required library could not be completed. This is because the library of "libhdf5-103" does not exist in apt package list.

$ sudo apt install libhdf5-103 libqtgui4 libatlas3-base libjasper1 libqt4-test
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
E: パッケージ libhdf5-103 が見つかりません

When I tried to run open cv, ImportError happens by the same reason.

$ python3
Python 3.7.7 (default, Apr 26 2020, 02:08:31) 
[GCC 6.3.0 20170516] 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/.pyenv/versions/3.7.7/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

I tried some to intall some packages like aboves, I could not figure out how I can fix this.

For example of python3.7.7, I searched for the package by apt-cache search

  $ apt-cache search libhdf5-serial
  libhdf5-dev - Hierarchical Data Format 5 (HDF5) - development files - serial version
  libhdf5-serial-dev - transitional dummy package

Then run apt-cache search again after sudo apt-get update

  $ sudo apt-get update
  $ sudo apt-get install libhdf5-dev
  $ sudo apt-get update
  $ sudo apt-get install libhdf5-serial-dev

However, "ImportError: libhdf5_serial.so.103" was not solved.


Conclusion

Missing library is different between python versions:

  • Python 3.7.5 - "ImportError: libImath-2_2.so.23: cannot open shared object file: No such file or directory"
  • Python 3.7.7, 3.8.2 - "ImportError: libhdf5_serial.so.103: cannot open shared object file: No such file or directory"

For there error of missing library for OpenCV, there are some issues like:

Alternative

At opencv.org, there is a tutorial to build OpenCV (instead of install pre-built package from pip. I'm trying it.

I could build OpenCV by my own on Rapsberry Pi, the outcome was the same (Import Error happens when I run cv2.)

Afterall, when I tried by python 3.5.3 on my Rapsberry Pi, the open cv worked successfully.