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 nump
You need the ffmpeg codec to be able to read the video.
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'):
break
else:
break
# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()
try
pip install opencv-contrib-python
It worked for me
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
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
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.