question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Library failed to import with opencv 4.5.5

See original GitHub issue

🐛 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

  • Albumentations version (e.g., 0.1.8): 1.1.0
  • Python version (e.g., 3.7): 3.7
  • OS (e.g., Linux): Linux on Google Colab
  • How you installed albumentations (conda, pip, source): pip
  • Any other relevant information: opencv-python-headless v4.5.5.62

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.)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
alexheatcommented, Jan 1, 2022

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

1reaction
Dipetcommented, Dec 29, 2021

Try these commands:

pip uninstall opencv
pip install --upgrade opencv-python
Read more comments on GitHub >

github_iconTop Results From Across the Web

I installed the openCV library but it cannot be used, how can I ...
What I tried to do: Install the library using pip install opencv-python; Read the library using import cv2 , import cv2 as cv2...
Read more >
ImportError: DLL load failed while importing cv2: The specified ...
I did try reinstalling Python (still 3.8.6) and changed OpenCV version to 4.5.0 (from 4.5.1) but still no luck. Perhaps a Windows or...
Read more >
opencv-contrib-python - PyPI
A: If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed. If you are using older Windows version...
Read more >
Install OpenCV 4.5 on Jetson Nano - Q-engineering
On Decemeber 25, 2021, OpenCV released version 4.5.5. Again the larges changes ... The location of the Python libraries is still an issue....
Read more >
ModuleNotFoundError No module named cv2 - Edureka
This error may occur if you didn't install opencv module in your system. So first check this module is available or not. $...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found