Python cv2.Videocapture () not working, cap.isOpened () returns false

cv2.Videocapture () works fine when using a webcam, but when trying to read from the hard drive, the 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() 

He 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

+5
source share
2 answers

you need ffmpeg codec to run video

+3
source

I'm not sure if you spell the file name correctly. I have never seen a file directory, for example 'car video.mp4' . When you use a zero-based index, the webcam and cv2.VideoCapture work fine; however, VideoCapture cannot read a file like 'car (space) video.mp4'. The working code looks 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() 
+1
source

Source: https://habr.com/ru/post/1264178/


All Articles