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.imread('background.png')
cap = cv2.VideoCapture('car video.mp4')
cap.open('car video.mp4')
print cap.isOpened()
while 1:
ret,img=cap.read()
cv2.imshow('a',img)
print img.shape
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It shows this error
cv2.imshow('a',img)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:266: error: (-215) size.width>0 && size.height>0 in function cv::imshow
my opencv version 3.0.0, python 2.7, windows10 32 bit
The VideoCapture class returns a video capture object which we can use to display the video. The class has several methods, for example, there is the get() method which takes a property identifier from cv2.
isOpened() Returns true if video capturing has been initialized already. If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true.
while(True): ret, frame = cap. read() This code initiates an infinite loop (to be broken later by a break statement), where we have ret and frame being defined as the cap. read(). Basically, ret is a boolean regarding whether or not there was a return at all, at the frame is each frame that is returned.
VideoCapture is a function of openCV library(used for computer vision, machine learning, and image processing) which allows working with video either by capturing via live webcam or by a video file.
You need the ffmpeg codec to be able to read the video.
try
pip install opencv-contrib-python
It worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With