10

I am reading an avi file usinh python 2.7 and opencv2.4.I am using windows 10.My sample code is

import numpy as np
import cv2
cap = cv2.VideoCapture('videos/wa.avi')
while(cap.isOpened()):
  ret, frame = cap.read()
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  cv2.imshow('frame',gray)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

When i run video is shown But the program ends with no Error

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ..\..\..\..\opencv\modules\highgui\src\window.cpp, line 261
Traceback (most recent call last):
File "C:/Users/Emmanu/PycharmProjects/VideoEventDetection/test.py", line 11, in <module>
cv2.imshow('frame',frame)
cv2.error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

What i am doing wrong?How can i correct it?

0

4 Answers 4

7

The problem is in this line:

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

This line expects frame to be a 3 channel or 4 channel Mat object but instead it got some empty Mat and that is why you are getting this assertion failed. You need to check if the frame exists in video and need to handle end of video properly.

cap.isOpened() will just check if the video file can be opened for reading but it will not return a false when end of video file is reached.

Try this

Sign up to request clarification or add additional context in comments.

2 Comments

FIle doesn't open for me. opencv version 3.0.0 beta. Any suggestions?
Thank you for replying but I don't remember the issue now but I think it was resolved after I installed opencv-ffmpeg again
1

When you put cap.isOpened() it check that, the video is read properly, So the while loop is not working there.

But when you changed to while True it will execute without proper reading that's why it's giving an error.

Make sure than you are properly reading the video file.

1 Comment

you are right,there was a problem with video.When i changed the video and run it with 'cap.isOpened()'.Video shown up thanks for that but program end with an error.I have updated the question
0

You took the tutorial from here: Playing Video from file

This question adresses your error: open cv error: (-215) scn == 3 || scn == 4 in function cvtColor

Two things may work:

  • Make sure the video is found!
  • And try to use cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

2 Comments

Yes there was a problem with video file.I have updated the question
Try to use cv2.COLOR_BGR2RGB instead of cv2.COLOR_BGR2GRAY
0

Well I am guessing the capture is not open. That's why your program ends instantly when you use while(cap.isOpened()):.

As stated in this doc it happens that the capture is not implicitly opened when created.

Sometimes, cap may not have initialized the capture. In that case, this code shows error. You can check whether it is initialized or not by the method cap.isOpened(). If it is True, OK. Otherwise open it using cap.open().

Try to explicitly open the capture like so:

...
cap = cv2.VideoCapture('videos/wa.avi')
cap.open()
while(cap.isOpened()):
...

If this does not work you will have to check for the video file path.

1 Comment

Yes there was a problem with video file.I have updated the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.