Chuyên Hoàng
Chuyên Hoàng

Reputation: 148

AttributeError: module 'cv2' has no attribute 'VideoCapture'

I had some problems with Opencv in Python.

This attribute problem also happens with imread

I tried to uninstall and reinstall with contrib-Opencv,but it did not work.

About 2 months ago, my opencv file still worked well, but I don't know why it doesn't work now.

In the next reinstallations,this command always sastifies but no good results

import cv2
cap = cv2.VideoCapture(0)
...
cap.release()

My error command:

Traceback (most recent call last):
  File "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py", line 4, in <module>
    cap = cv2.VideoCapture(0)
AttributeError: module 'cv2' has no attribute 'VideoCapture'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py"]

Upvotes: 5

Views: 35429

Answers (8)

Sharoon Honey
Sharoon Honey

Reputation: 1

Same problem occurs with me. And I fix it completely, by reading this resource: https://pypi.org/project/opencv-python/#installation-and-usage,

In FAQ section it is mentioned, that if you are on Windows 10 ProN, then install Windows Media Pack Features and Visual C++ redistributable 2015 is must.

Upvotes: 0

Biman Pal
Biman Pal

Reputation: 429

I have faced issue in Python 3.9.2 with opencv-python-4.7.0.72.

Remove all opencv related libraries then installed it again. If need remove virtual environment then create virtual environment and install opencv.

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

install

pip install opencv-python

Upvotes: 2

hemanth.official279
hemanth.official279

Reputation: 11

The best way to solve all the problems of OPENCV-PYTHON is by uninstalling it and reinstalling it.

Even I faced the same problem.

I fixed it by:

python -m pip uninstall Opencv-python

Then I reinstalled it by using a lower version. But unfortunately, I did not know the versions of opencv; So by using a small trick you can get it by running:

python -m pip install opencv-python==

and you will get an error similar to this:

.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.
4.60, 4.5.5.62, 4.5.5.64)
ERROR: No matching distribution found for opencv-python==

Here you can see all the versions of opencv-python; choose any one (but not the latest as the error occurs due the latest version of opencv-python. install it by using:

pip install opencv-python==3.4.17.61 (You can choose your version, but this version solved the issue for me)

then enjoy your coding....

Even AUTO-COMPLETE error in opencv-python gets solved.

Upvotes: 0

Tobi Z
Tobi Z

Reputation: 29

I had the same problem and was searching for ages. Finally I found a way that is (at least currently) working for me.

When installing opencv there will be a folder 'cv2' which again includes the package 'cv2'.

Installation directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2 Package directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2\cv2

Directory after installation


Probably this leads to confusing for python (maybe some circular import or so). Therefore I just renamed the installation directory to something else, e.g. C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2_

Directory after renaming

That is the solution that is currently (and hopefully in the future as well) working for me).

Upvotes: 0

Nick
Nick

Reputation: 313

The problem is that your file is called cv2.py so it imports itself and an error occurs, this can be understood from this line.

Most likely due to a circular import

Which translates as:

Most likely due to circular import

To fix the problem, rename your file

Upvotes: 3

VIKRANT PATIL
VIKRANT PATIL

Reputation: 21

Add these two lines:

from cv2 import VideoCapture
from cv2 import waitKey

Upvotes: 2

Chuy&#234;n Ho&#224;ng
Chuy&#234;n Ho&#224;ng

Reputation: 148

After asking my friend,i had the solution.It is very easy because i saved this file in Documents path in user which my private name Hoang Cao Chuyen without '_'.I changed into another path,and it was OK.

Upvotes: 0

seraph
seraph

Reputation: 350

One of the issue I noticed is that you used cv11 as the main.py name.

It is very easy for PyCharm to get confused if you have a file saved as cv2.py. Please check if you have any other similar files with the name cv2.

Else, try to do this:

  1. remove OpenCV
  2. reinstall using command pip install opencv-contrib-python

OR

try reinstalling ffmpeg as it might be one of the issue

pip install ffmpeg-python

Upvotes: 7

Related Questions