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?

MANDY
  • 71
  • 1
  • 1
  • 6

5 Answers5

8

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

Lum Ramabaja
  • 316
  • 1
  • 4
  • 12
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.

Community
  • 1
  • 1
Aenimated1
  • 1,594
  • 1
  • 9
  • 10
  • Thank you so much! I have both webcam and video file inputs, in different threads, and because the absence of `imshow`, `waitKey` doesn't work. I didn't understand why my code broke until read your answer: video run so fast, that it ended before the beginning of the program. – j.moustafa May 14 '20 at 07:45
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()
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.

Totoro
  • 3,398
  • 1
  • 24
  • 39
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)
陳明傑
  • 1
  • 1