Fixing Python OpenCV AttributeError: ‘module’ object has no attribute ‘xfeatures2d’

Python OpenCV AttributeError: ‘module’ object has no attribute ‘xfeatures2d’ is an error which occurs when you install a module which is not supported in opencv-python.

Today I try to explain why this error takes place and how to solve it, I will also add other solutions that could solve the error if possible.

Exploring the Python OpenCV AttributeError: ‘module’ object has no attribute ‘xfeatures2d’

This is an error which occurs when you install a module which is not supported in opencv-python.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
AttributeError: 'module' object has no attribute 'xfeatures2d'
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : install opencv-contrib-python

We have already established the fact that when you install a module which is not supported in opencv-python. Just like SIFT. You will run to this error.

The first solution is to install opencv and its related contrib at the same time. To do that you can use the command bellow

                                                                       #
pip install opencv-python==v.v.v opencv-contrib-python==v.v.v
                                                                       #

v.v.v are the desired versions of both which should be the same version by the way

You can also try installing opencv-contrib-python on its own

                                                                       #
pip install opencv-contrib-python
                                                                       #

Try the method bellow if this method did not work.

Solution 2 : install opencv-contrib-python

This is not a proper method independent of the method above, it is kid of a follow up for those who have conda.

After trying to install opencv-contrib-python , you might get an error. To solve the new error plus the original error you can try the command bellow.

                                                                       #
conda install -c menpo opencv
                                                                       #

If that did not work, you can head to site-packages , remove cv2 from there and then. Try installing opencv-contrib-python . Again, just like what we did in the first method.

Summing-up : 

We arrived at the end of this quest to solve this annoying error Python OpenCV AttributeError module object has no attribute xfeatures2d , I hope me sharing my experience with you helped, I hope the other solutions helped, If you like this website support us on Kofi and keep browsing, thank you and good luck with your Python Journey, Cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/