Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' #591

Closed
alexjbusch opened this issue Nov 29, 2021 · 20 comments
Closed

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' #591

alexjbusch opened this issue Nov 29, 2021 · 20 comments

Comments

@alexjbusch
Copy link

alexjbusch commented Nov 29, 2021

Trying to import cv2 in python 3.9.2 gives me the following error:

File "C:\Users\My Name\AppData\Local\Programs\Python\Python39\lib\site-packages\cv2_init_.py", line 9, in
from .cv2 import _registerMatType
ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (C:\Users\My Name\AppData\Local\Programs\Python\Python39\lib\site-packages\cv2\cv2.cp39-win_amd64.pyd)

This is on Windows 10, with x86 architecture using opencv version (4.5.1.48)

@alalek
Copy link
Member

alalek commented Nov 29, 2021

There is mess with versions:

  • _registerMatType is a part of OpenCV 4.5.4
  • but you mention 4.5.1

Please ensure that you have installed one version of OpenCV only.

@alexjbusch
Copy link
Author

yeah, uninstalling opencv-python and then reinstalling it with pip fixed this issue

@aQwus
Copy link

aQwus commented Jan 5, 2022

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:

!pip install "opencv-python-headless<4.3"

Here's a thread from stack overflow on the same issue.

@Ahteshammm
Copy link

Thank you aQwus . It worked with !pip install "opencv-python-headless<4.3"

@EdjeElectronics
Copy link

EdjeElectronics commented Jan 9, 2022

Thanks @aQwus , that fixed it for me too! It's a little scary when your Colab code works one day and then stops working the next day 😬.

@benisalla
Copy link

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:

!pip install "opencv-python-headless<4.3"

Here's a thread from stack overflow on the same issue.

@aQwus , thanks man that saved me to

@amiramandaa
Copy link

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:

!pip install "opencv-python-headless<4.3"

Here's a thread from stack overflow on the same issue.

thank you very much, you save me!

mintar added a commit to mintar/Deep_Object_Pose that referenced this issue Mar 25, 2022
opencv-python-headless<4.3 is necessary to avoid `ImportError: cannot
import name '_registerMatType' from 'cv2.cv2'` (see
opencv/opencv-python#591 (comment)).
@pjbelo
Copy link

pjbelo commented Jun 6, 2022

My problem was related to diferent versions of opencv modules. You can check it with:
pip list | grep opencv

upgrading all modules to the same version solved my error:

pip install --upgrade opencv-python
pip install --upgrade opencv-contrib-python
pip install --upgrade opencv-python-headless

@abhiTronix
Copy link
Contributor

abhiTronix commented Jun 7, 2022

@pjbelo You must not install all opencv-python types at once. You need to chose any one of them.

@pjbelo
Copy link

pjbelo commented Jun 7, 2022

Hi @abhiTronix. Thanks for your comment but I'm not sure if I understood it. I didn't install nothing new. Using pip list | grep opencv I can see the installed versions (that were different). And then I upgraded them. It seems upgraded versions solved the problem. What's wrong with this approach?

@abhiTronix
Copy link
Contributor

Using pip list | grep opencv I can see the installed versions (that were different)

@pjbelo Every OpenCV binary opencv-python, opencv-contrib-python, opencv-python-headless serves different purpose. See docs:

  • Option 1 - Main modules package: pip install opencv-python
  • Option 2 - Full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python
  • Option 3 - Headless main modules without GUI package: pip install opencv-python-headless
  • Option 4 - Headless full without GUI package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python-headless

You don't need all of them at once, just select any one of them according to your application. Installing all of them just going to overwrite previous binaries or cause more problem. Hope that helps.

@pjbelo
Copy link

pjbelo commented Jun 7, 2022

@abhiTronix  I did not install any new packages.

Using Google Colab and after installing TensorFlow Object Detection API I had these three packages installed: opencv-python, opencv-contrib-python, opencv-python-headless. Not sure why Google decided to to have them all installed or if they are not necessary. (you can check this here)

I just updated their versions and that cleared the error. I'm aware that sometimes you need to have packages at a specific version (sometimes older versions) but I guess it was not my case because I got no errors.

Just wanted to share this approach that might help someone else...

@pjbelo
Copy link

pjbelo commented Jun 7, 2022

Hi @abhiTronix , digging a bit deeper:
Google Colab basic setup comes with these packages (at the moment of this comment):
opencv-contrib-python 4.1.2.30
opencv-python 4.1.2.30

After installing TensorFlow Object Detection API, this package is also installed:
opencv-python-headless 4.5.5.64

So the conflict between these packages must be the origin of the error.

TensorFlow Object Detection API does not have a direct opencv dependency, as you can see here, so it must be a dependency of a dependency (is this the so called Hell of Dependencies?).

So, following your reasoning the best solution would be to keep opencv-contrib-python and uninstall the other packages, right?

Anyway, updating all already installed packages to the same version also removed conflicts (and the error).

Sorry about taking so much space here. I think this is not even an opencv issue, but a colab/dependencies config issue...

@pjbelo
Copy link

pjbelo commented Jun 7, 2022

Hi again @abhiTronix, digging a little bit deeper:

This is the reverse tree of dependencies for colab / object-detection on opencv packages (at the moment of this comment):

opencv-contrib-python==4.1.2.30

opencv-python==4.1.2.30

  • albumentations==0.1.12 [requires: opencv-python]
  • dopamine-rl==1.0.5 [requires: opencv-python>=3.4.1.15]
  • imgaug==0.2.9 [requires: opencv-python]
    • albumentations==0.1.12 [requires: imgaug>=0.2.5,<0.2.7]
  • lvis==0.5.3 [requires: opencv-python>=4.1.0.25]
    • object-detection==0.1 [requires: lvis]

opencv-python-headless==4.5.5.64

  • tf-models-official==2.9.2 [requires: opencv-python-headless]
    • object-detection==0.1 [requires: tf-models-official>=2.5.1]

As you can see, only opencv-contrib-python is a direct dependency. This raises another question:

How should a developer manage his list of dependencies? If package A depends on opencv-contrib-python and package B depends on opencv-python they (A and B) can't be used at the same time because their dependencies will be in conflict?

Should OpenCV take this in consideration and allow that all OpenCV packages could be installed at the same time avoiding conflicts? I know this does not make sense as direct dependencies, but considering the above case...

I promise I have stopped my digging :-)

@wasdee
Copy link

wasdee commented Oct 24, 2022

I worked on a Nvidia Jetson container. There might be already cv2 beforehand. You could check with import cv2. and see if there is an error.

After I undo the installation of opencv-python. Things works again.

@inamorom
Copy link

using cv2 on vscode i was having this problem but nothing worked. Turns out that i named my file as "cv2.py" so it was importing itself

"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error."

@sisrfeng
Copy link

Please ensure that you have installed one version of OpenCV only.

pip uninstall opencv-python-headless ; pip uninstall  opencv-contrib-python ; pip uninstall opencv-python

Then install only 1 of those 3.

@TommyLeong
Copy link

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:

!pip install "opencv-python-headless<4.3"

Here's a thread from stack overflow on the same issue.

hats off to you brother!

@fanchuanster
Copy link

Probably you can manually delete the recursively duplidated cv2 foldres
rm -rf /usr/local/lib/python3.8/dist-packages/cv2/cv2

athnzc pushed a commit to athnzc/dope_fork that referenced this issue Oct 26, 2023
opencv-python-headless<4.3 is necessary to avoid `ImportError: cannot
import name '_registerMatType' from 'cv2.cv2'` (see
opencv/opencv-python#591 (comment)).
@foryoubar
Copy link

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:
!pip install "opencv-python-headless<4.3"
Here's a thread from stack overflow on the same issue.

@aQwus , thanks man that saved me to

I solved this error on Google Colab by downloading the headless open-cv and of a version below 4.3 because backward compatibility was dropped since 4.3.0. Try the following command in Google Colab:
!pip install "opencv-python-headless<4.3"
Here's a thread from stack overflow on the same issue.

hats off to you brother!

This way relly work! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests