albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.16k stars 1.64k forks source link

Library failed to import with opencv 4.5.5 #1100

Closed leo2tigers closed 7 months ago

leo2tigers commented 2 years ago

🐛 Bug

When using opencv (opencv-python-headless) 4.5.5, the library failed to be imported

To Reproduce

Steps to reproduce the behavior:

  1. pip install --upgrade opencv_python_headless
  2. import albumentations as A

Stack traces are as below

ImportError                               Traceback (most recent call last)

<ipython-input-1-f29cfa920b9e> in <module>()
      8 from tensorflow.keras import backend as K
      9 from functools import partial
---> 10 import albumentations as A
     11 from albumentations import Compose, RandomBrightness, RandomContrast, Rotate, GaussNoise
     12 

/usr/local/lib/python3.7/dist-packages/albumentations/__init__.py in <module>()
      3 __version__ = "1.1.0"
      4 
----> 5 from .core.composition import *
      6 from .core.transforms_interface import *
      7 from .core.serialization import *

/usr/local/lib/python3.7/dist-packages/albumentations/core/composition.py in <module>()
      6 import numpy as np
      7 
----> 8 from albumentations.augmentations.keypoints_utils import KeypointsProcessor
      9 from albumentations.core.serialization import SerializableMeta, get_shortest_class_fullname
     10 from albumentations.core.six import add_metaclass

/usr/local/lib/python3.7/dist-packages/albumentations/augmentations/__init__.py in <module>()
      2 from .keypoints_utils import *
      3 from .bbox_utils import *
----> 4 from .functional import *
      5 from .transforms import *
      6 

/usr/local/lib/python3.7/dist-packages/albumentations/augmentations/functional.py in <module>()
      5 from warnings import warn
      6 from itertools import product
----> 7 import cv2
      8 import numpy as np
      9 import skimage

/usr/local/lib/python3.7/dist-packages/google/colab/_import_hooks/_cv2.py in load_module(self, name)
     79 
     80     module_info = imp.find_module(name, self.path)
---> 81     cv_module = imp.load_module(name, *module_info)
     82 
     83     if not previously_loaded:

/usr/lib/python3.7/imp.py in load_module(name, file, filename, details)
    242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:
--> 244         return load_package(name, filename)
    245     elif type_ == C_BUILTIN:
    246         return init_builtin(name)

/usr/lib/python3.7/imp.py in load_package(name, path)
    214         return _exec(spec, sys.modules[name])
    215     else:
--> 216         return _load(spec)
    217 
    218 

/usr/local/lib/python3.7/dist-packages/cv2/__init__.py in <module>()
      7 
      8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
     10 from . import mat_wrapper
     11 from . import gapi

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so)

Expected behavior

The library could be loaded and used normally

Environment

Additional context

The problem doesn't occur if using opencv 4.5.4 or below (I've tested with all opencv version from 4.4.0 and only found 4.5.5 (which was released around 5 hours ago as of this writing) to be problematic.)

BloodAxe commented 2 years ago

Could you just import cv2 in Colab using v4.5.5.62 and get success call on, say, cv2.getBuildInformation() ? This issue does not seem to be related to Albumentations package itself, but rather to OpenCV.

leo2tigers commented 2 years ago

I've tried importing cv2 v4.5.5.62 and trying cv2.getBuildInformation() and both appear to be successful.

Dipet commented 2 years ago

Correct package name is opencv-python not opencv

Dipet commented 2 years ago

Try these commands:

pip uninstall opencv
pip install --upgrade opencv-python
leo2tigers commented 2 years ago

Correct package name is opencv-python not opencv

Sorry, seems like it's a typo, the package I'm referencing should be opencv-python-headless, not opencv (according to albumentations pip installation logging)

For uninstalling and then reinstall method, I've tried but it doesn't help.

alexheat commented 2 years ago

I found the solution in this stack overflow thread https://stackoverflow.com/questions/70537488/cannot-import-name-registermattype-from-cv2-cv2.

%pip uninstall opencv-python-headless
%pip install opencv-python-headless==4.1.2.30

Note that Colab version of the notebook here is currently broken because of this https://colab.research.google.com/github/albumentations-team/albumentations_examples/blob/colab/example_bboxes.ipynb