Pip install opencv-python conflict with Jetson docker container

Stephen Cow Chau
3 min readMar 28, 2024

Background

This is a journey to install anomalib from openvino into Jetson AGX Orin using docker container approach, the journey is sadly yet to be success, but the major part of problem seems related to opencv and is kind of “through”, so here I am sharing my experience.

Symptom

There could be several symptoms depends on the base image being used, like, when running:

from anomalib.models import EfficientAd

(or later identified it would happen when running anything need to import cv2):

  1. partially initialized module ‘cv2’ has no attribute ‘gapi_wip_gst_GStreamerPipeline’ (most likely due to a circular import)
  2. ImportError: /lib/aarch64-linux-gnu/libgstreamer-1.0.so.0: file too short

Observations 1 — Numpy issue

Depends on the target anomalib version (0.7 or 1.0.0.dev0), it would complain on the numpy version being 1.17.4, and during pip install, it cannot be uninstalled as it’s “outside environment /usr”

Observation 2 — Python-opencv preinstalled

With the base docker image from Nvidia or other jetson centric images, which I tried:

  1. nvcr.io/nvidia/pytorch:24.03-py3-igpu
  2. nvcr.io/nvidia/l4t-pytorch:r35.2.1-pth2.0-py3
  3. ultralytics/ultralytics:latest-jetson
  4. dustynv/l4t-pytorch:r35.4.1

Before installing anomalib (which have dependency opencv-python and opencv-python-headless), the import cv2 have no issue, while after pip install anomalib, the issue occurs.

Cause

The issue occurs because the opencv in the docker image was pre-installed:

When we install opencv from pip, it does not aware the opencv being installed already from apt and overwrite some files in the package with different version (note that pypi package opencv-python does not have version 4.5.0, so it would potentially introduce conflict)

[Does not work] Some article mentioned to pip uninstall the opencv-python and reinstall, but that seems does not work.

Solution

My solution ended up is to identify which package depends on opencv-python and opencv-python-headless, and install those package with — no-deps and then their dependences one by one, which is tedious

The helper is a python package called pipdeptree, after installing it with pip install pipdeptree, one can use command line to check what packages depends on the packages:

After identifying the dependencies are “anomalib”, “imgaug” (for opencv-python) and “albumentation” (for opencv-python-headless)

We can then identify the dependency of these 3 packages and then setup the new docker image as follow (which I am aiming at another version of anomalib), with the 3 packages identified using --no-deps option:

Conclusion

This is just part one of the story (the anomalib still not run due to pytorch-lighting version conflict which should not happen as the requirement.txt from anomalib say the paticular version…)

In nowadays, docker container prepared by others make live so much easier compare to years ago, so I am here to thank you so much for effort people helped to build great docker images.

--

--