6

I am a begginer in OpenCV and Python. I tried to load a video and displaying it using code given below:

import cv2
cap = cv2.VideoCapture('G:\3d scanner\2.mmv')
while(1):
    _ , img2=cap.read()
    cv2.namedWindow('video',cv2.WINDOW_NORMAL)
    cv2.imshow('video',img2)            
    k=cv2.waitKey(1) & 0xFF
    if k==27:
        break
cap.release()
cv2.destroyAllWindows()

But it showing the following error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ..\..\..\..\opencv\modules\highgui\src\window.cpp, line 261
File "test3.py", line 8, in <module>
cv2.imshow('video',img2)
cv2.error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

There are previous questions on this site regarding this issue but the answers given were using cv library but not cv2.

Any idea of what is wrong in this?

3
  • Which version of OpenCV and Python are you using? Commented Apr 9, 2016 at 19:15
  • use a debugger. step through the code. inspect variables. Commented Jul 31 at 23:32
  • img2 is None. could be due to various reasons. impossible to say which. official docs show you how to handle errors. the piece of code presented in the question lacks all error checking, even dismissing information that the user is explicitly told to handle. this very same question used to be asked all the time, but now it seems that useful Q&A pairs on this issue exist, which give people the solution rather than making them ask these questions anew every time. Commented Aug 30 at 11:40

5 Answers 5

9

This might help you:

import numpy as np
import cv2

cap = cv2.VideoCapture('linusi.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

If it doesn't work, there are a lot of useful explanations in their documentation: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html

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

1 Comment

this code skips the loop if not cap.isOpened(). this is good. however, ret must not be ignored.
1

There are a couple important differences when using VideoCapture on a video file. First off, there's no built in frame delays like when you're capturing from a webcam. Since most computers are very powerful these days, unless you deliberately introduce a delay between frames, the video will be displayed in a blink of an eye.

Secondly, the video has an end unlike input from your webcam, so you need to explicitly handle that case. I suspect what's happening in your case is that the video is completing in a matter of milliseconds, and then the final cap.read() is returning an empty matrix which imshow() subsequently complains about.

See Opening video with openCV +python. One of the answers in there is directly applicable to your situation.

2 Comments

a webcam produces frames at a fixed rate. not even theoretically is there a way to display pictures before the camera has taken them. in the case of a video file, sure, the frames are decoded and displayed as quickly as possible. practically that is still not going to be "a blink of an eye". the requirement of waitKey() imposes a minimum delay of theoretically at least 1 millisecond, in practice (implied by the operating system) typically 10-20 milliseconds, no matter the argument. this answer here does not actually say HOW to handle the situation of the video file ending.
the "mmv" video file is an obscure format. OpenCV uses ffmpeg to read video files. perhaps even ffmpeg does not know what to do with such a file. most likely, the VideoCapture did not even succeed in demuxing the container or decoding the video stream in the first place.
1

I think you have no cascade file that helps it determine what to compare with and when to exit. Try the following code and see.

import cv2 
    cap = cv2.VideoCapture("video.mp4")
    Cascade = cv2.CascadeClassifier("haarCasade.xml")
    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()

Comments

0

use ("%s" %parameter) and do not use ("%s",parameter)

import cv2
import numpy as np
import os
import glob
outputFolder = "picture_output"
videoList = glob.glob("video/*.mp4")
video=videoList[0].split('\\')[1]
cap=cv2.VideoCapture("video/%s" %video)

Comments

0

This error message indicates that the cv::Mat that you tried to access was not created. In this case, since you are trying to grab frames from a video, the reason could be one of the following:

  1. The path to the video is not correct (on some platforms you might want to try cap = cv2.VideoCapture('G:/3d scanner/2.mmv'))

  2. OpenCV could not retrieve frames from the video due to the lack of support for this format.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.