The Error “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ “, is an error that is invoked when you are working on python and are trying to use the tensorflow library to work on machine learning projects.
Given below is the snippet of the error you might get:
ImportError: cannot import name '_registerMatType' from 'cv2.cv2'
I would like to share with you the steps I took to fix the “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ ”
Why “ImportError cannot import name ‘_registerMatType’ from ‘cv2.cv2’ ” Error is Seen?
The error, “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ ” is seen because, It’s possible that opencv-version python’s (4.1.2.30) does not correspond to opencv-python-headless (4.5.5.62). Or perhaps there’s a problem with the most recent version 4.5.5 opencv-python-headless.
To find out which library is the culprit and which version is the correct one to use for your project you have to run !pip list
and check the versions of open-cv related packages. Then you will be able to see that version of opencv-python-headless does not match with others.
The detailed solution to fix the error “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ “, is given below:
How to Fix “ImportError cannot import name ‘_registerMatType’ from ‘cv2.cv2’ ” Error?
To fix the error, you have to run the command pip list|findstr opencv
and check if both opencv-version python’s and opencv-python-headless have the same version, if not then you will have to uninstall and re-install your headless package using pip install.
Once both opencv-version python’s and opencv-python-headless versions are compatible, it would fix ImportError cannot import name ‘_registerMatType’ from ‘cv2.cv2’ error.
To fix the error, “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ “, you will have to follow one the steps mentioned below:
First you will have to run pip list |findstr opencv
to show you which versions of the packages you are using. Then you be confirmed for sure if it a versioning issue or something else. Once you run the your output should look something like this:
opencv-python 4.5.2.52
opencv-python-headless 4.5.5.62
In the this example we are taking the example of version 4.5.2.52 and it could be different in your system.
You can notice in the output that the versions are not the same.
So we will first have to uninstall headless by following the below mentioned command:
pip uninstall opencv-python==4.5.2.52
pip uninstall opencv-python==4.5.2.52
Once you have run the above mentioned command then you will have to reinstall opencv-python 4.5.5.62 with the opencv-python package using the below mentioned command:
pip install opencv-python-headless==4.5.5.62
This should fix the error “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ “.
Conclusion
To fix the error “ImportError: cannot import name ‘_registerMatType’ from ‘cv2.cv2’ “, you will have to run the command pip list |findstr opencv
and check if both have the same version, if not then you will have to uninstall and re-install your headless package using pip install.