Ask Your Question
0

I can't call the video that is stored in my laptop using VideoCapture() method of opencv.

asked 2015-02-18 01:42:40 -0600

updated 2015-02-18 08:53:34 -0600

thdrksdfthmn gravatar image

If I write: cap = cv2.VideoCapture(0), my code works properly. but whenever I write cap = cv2.VideoCapture('C:\Users\Anjali\Documents\Python_Files\Video_tracking\slow.mp4'), it does't work.

Below is my code:

import cv2
import numpy as np


cap = cv2.VideoCapture('C:\Users\Anjali\Documents\Python_Files\Video_tracking\slow.mp4')

while (True):

ret, frame = cap.read()

#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('Frame', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

It gives the following error, please help me in this:

cv2.imshow('Frame', frame)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
edit retag flag offensive close merge delete

Comments

i guess, your capture did not open (it did not like the codec), thus your frame was empty, thus imshow() complains.

please, be wise and add checks:

if not cap.isOpened(): raise Exception("it did not find or did not like your movie")

if frame==None: raise Exception("empty image detected")

berak gravatar imageberak ( 2015-02-18 01:52:37 -0600 )edit

Yeah, it is not capturing my video. So, what to do with this? My path is correct I have checked it.

AnjaliChanglani gravatar imageAnjaliChanglani ( 2015-02-18 02:15:25 -0600 )edit

so, then attack the codec problem. mp4 is only the container, there will be some variant of divx xvid or such codec used. e.g. you could go here and try some package, like the LAV one.

berak gravatar imageberak ( 2015-02-18 02:21:33 -0600 )edit

also make sure, that python can find your opencv_ffmpegxxx.dll

berak gravatar imageberak ( 2015-02-18 02:33:42 -0600 )edit

hey berak, I saw FOURCC.. and downloaded and installed 3IV1 for my windows and mp4 video. now what? how to use this? please help me.

AnjaliChanglani gravatar imageAnjaliChanglani ( 2015-02-18 06:22:06 -0600 )edit

Please anyone help me in this...

AnjaliChanglani gravatar imageAnjaliChanglani ( 2015-02-19 03:55:32 -0600 )edit

Have you tried to display the only if the frame is not empty? Maybe there are some empty frames for some reason

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-19 05:15:07 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-03-23 04:11:12 -0600

TJ gravatar image

updated 2015-03-23 04:29:26 -0600

instead of

'C:\Users\Anjali\Documents\Python_Files\Video_tracking\slow.mp4'

use

'C:\\Users\\Anjali\\Documents\\Python_Files\\Video_tracking\\slow.mp4'

use double slashes

edit flag offensive delete link more
0

answered 2015-02-19 06:35:27 -0600

That happened to me in a previous Python version with some videos, my partial solution was reading video with

i=1 
while(i< NUMBER_FRAMES)
     ret, frame = cap.read()
     i=i+1

if you realize, this code read frames to discard them. According the size of your video, NUMBER_FRAMES could vary between 10 and 400, you can set your own number. Hope this hint works for you.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-02-18 01:42:40 -0600

Seen: 386 times

Last updated: Mar 23 '15