Skip to content

Installing OpenCV in CI

Romain Hugonnet edited this page Mar 29, 2024 · 1 revision

It often happens that OpenCV breaks in the CI due to package dependencies, and sometimes it is not solved for a while...

To understand what causes the error, raise the exception where OpenCV is optionally imported, for instance in affine.py:

try:
    import cv2

    _has_cv2 = True
except ImportError as e:
    _has_cv2 = False

    # Add this line
    raise e

It usually is a dependency file that is not found, such as: opencv ImportError: libEGL.so.1: cannot open shared object file: No such file or directory.

To solve this, I have found 2 different methods that sometimes work:

  1. Add opencv to be solved with base conda environment instead of updated dev dependencies.

We always test the base and dev environment separately to ensure both work, but while updating the base to get the dev sometimes conda breaks some dependencies (while it works when using all at once in the base directly). To do that, mirror what is done in the python-test CI file at the base environment step using our own :

python .github/scripts/generate_yml_env_fixed_py.py --pyv ${{ matrix.python-version }} --add "opencv" "environment.yml"

  1. Use pip instead, using specifically opencv-contrib-python-headless.

The base package is opencv-python. But that normal pip install assumes other system dependencies are solved independently, which is not our case so we need to add the -headless. Additionally, the python wrapper commands that we use in our code are only included in the package -contrib.

Hopefully, one of those solved the problem! Otherwise, best is likely to @pytest.skip openCV tests and wait until they solve it upstream! (will get easier when we switch to our own ICP routine, right now too many different tests call openCV)