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

Question:

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:UsersHoang Cao ChuyenDocumentspymlcv11.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:UsersHoang Cao ChuyenDocumentspymlcv11.py"]
Asked By: Chuyên Hoàng

||

Answers:

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
Answered By: seraph

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.

Answered By: Chuyên Hoàng

Add these two lines:

from cv2 import VideoCapture
from cv2 import waitKey
Answered By: VIKRANT PATIL

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

Answered By: Nick

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:UsersuserAppDataLocalProgramsPythonPython39Libcv2
Package directory: C:UsersuserAppDataLocalProgramsPythonPython39Libcv2cv2

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:UsersuserAppDataLocalProgramsPythonPython39Libcv2_

Directory after renaming

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

Answered By: Tobi Z

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.

Answered By: hemanth.official279

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
Answered By: Biman Pal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.