Skip to content Skip to sidebar Skip to footer

Python Cv2.videocapture() Does Not Work, Cap.isopened() Returns False

cv2.Videocapture() works fine while using webcam but while trying to read from hard drive it shows error cap.isOpened() returns false import cv2 import numpy as np background=cv2.

Solution 1:

You need the ffmpeg codec to be able to read the video.

Solution 2:

try

pip install opencv-contrib-python

It worked for me

Solution 3:

I was getting the same error while using opencv in anaconda3 virtual environment. I checked the buildinformation for current opencv version and ffmpeg was marked "no".

To resolve this

  1. I uninstalled opencv from my conda environment (conda uninstall opencv)
  2. Installed latest ffmpeg using conda-forge channel (conda install -c conda-forge ffmpeg)

    Name Version Build Channel

    ffmpeg 4.0.2 ha6a6e2b_0 conda-forge

  3. Then installed opencv again using conda-forge channel (conda install -c conda-forge opencv)

    Name Version Build Channel

opencv 3.4.1 py36_blas_openblash829a850_201 [blas_openblas] conda-forge

Restart the python console after doing this and import cv2.

Solution 4:

I am not sure that you are writing your file name correctly. I've never seen a file directory like 'car video.mp4'. When you are using the zero based index your webcam and cv2.VideoCapture works fine; however VideoCapture cannot read a file like 'car(space)video.mp4' A working code is something like this;

import numpy as np
import cv2

cap = cv2.VideoCapture('video.mp4')

while(cap.isOpened()):

    ret, frame = cap.read()

    if ret==True:

        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            breakelse:
        break# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()

Post a Comment for "Python Cv2.videocapture() Does Not Work, Cap.isopened() Returns False"