Opencv: AttributeError: module 'cv2' has no attribute 'dnn'
Asked Answered
P

4

16

I am just starting with computer vision. While running code I got the following error.

[INFO] loading model... Traceback (most recent call last): File "detect_faces_video.py", line 24, in net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) AttributeError: module 'cv2' has no attribute 'dnn'

I believe that the error is because my opencv version is 3.1.0 and for this tutorial to work I need at least the 3.3.0 version.

So the better question would be, How can I upgrade my opencv to 3.3.0 from 3.1.0. Do I need to delete the 3.1.0 version first?

I have installed my opencv in a virtual environment.

Thanks

Pembroke answered 30/9, 2018 at 7:47 Comment(1)
How did you install it in the first place? just....do that again with the new version?Tangential
P
23

I got it to work by making another virtual environment. Then Installing the latest version of opencv.

$ mkvirtualenv cv

$ sudo pip3 install opencv-contrib-python

Pembroke answered 30/9, 2018 at 8:17 Comment(0)
B
3

Similar to other answers, I've had multiple successes (for reasons I'm not sure of) by just uninstalling opencv and re-installing in the exact same environment and same opencv version:

pip3 uninstall opencv-python-headless
pip3 install opencv-python-headless
Blimey answered 21/10, 2022 at 14:29 Comment(1)
In a brand new venv the order seems to make a different. I started multiple times installing only hand full of libraries. I found that installing opencv last, or uninstalling it and reinstalling it last fixes it every time. weird.Scraggy
P
0

I think the easiest way is to open an new venv.

conda create --name myEnv python=3.9
conda activate myEnv

Then install the library you need. This works for me.

Pareto answered 11/11, 2023 at 1:15 Comment(0)
K
0

I uninstall and reinstalled opencv-python and opencv-python-headless using pip. And it worked fine. The issue I think was that, the version of opencv-python was different from opencv-python-headless. But after reinstallation, their versions were the same. Maybe this solved the issue with import cv2.

Kirkendall answered 26/6, 2024 at 7:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.