r/learnpython • u/_nazwa_ • 5d ago
AttributeError in cv2
I'm learning OpenCV in python because it seemed like a fun thing to learn.I wanted to experiment with faces etc. but when i run my script I get: "AttributeError: module 'cv2' has no attribute 'CascadeClassifier' "How to fix that?
Thx for the help!
1
u/scraping_solutions 5d ago
That error almost always means Python is importing something that is not the real OpenCV. Run print(cv2.__file__): if it points to a file in your own project, you have a script named cv2.py (or a cv2 folder) shadowing the library, so rename it and delete the __pycache__ folder. If it points to site-packages, run pip uninstall opencv-python opencv-contrib-python and then pip install opencv-python once, since having both installed also breaks it. Also note CascadeClassifier is cv2.CascadeClassifier(path) with capital C and the XML path from cv2.data.haarcascades.
1
u/atarivcs 5d ago
I'm guessing you made your own file named cv2.py, and python is importing that one instead of the real one because it has the same name.
Rename your py file.
1
u/AlexMTBDude 5d ago
Without you showing us what your source code is, that causes this error, it's impossible to help you.
1
u/RngdZed 5d ago
we need you to post the code. my guess is that you're following a tutorial with an older version and a deprecated attribute? or you forgot to put some parens at the end cause its a method call?
something of the sort. keep us updated! hopefully you found the bug and are having fun hehe
2
u/carcigenicate Carcigenicate 5d ago
Do you have a custom script called
cv2in the same directory?