Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.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

like image 909
Joy Mazumder Avatar asked Feb 13 '17 18:02

Joy Mazumder


People also ask

What does cv2 VideoCapture return?

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.

What is isOpened () in Python?

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.

What is ret frame cap read ()?

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.

What does VideoCapture read do?

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.


2 Answers

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

like image 162
Mohammed Awney Avatar answered Oct 16 '22 14:10

Mohammed Awney


try

pip install opencv-contrib-python

It worked for me

like image 24
L.xp Avatar answered Oct 16 '22 14:10

L.xp