Python Forum

Full Version: Capturing a snapshot from the video
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from imutils.perspective import four_point_transform
import numpy as np
import argparse
import cv2
 
scaling_factorx = 0.8
scaling_factory = 0.8

cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()
count = 0

while(1):

  ret, frame = cap.read()

  cv2.imshow('window-name', frame)
  cv2.imwrite("frame%d.jpg" %count, frame)
  count = count + 1

  frame = cv2.resize(frame, None, fx = scaling_factorx, fy = scaling_factory, interpolation = cv2.INTER_AREA)

  fgmask = fgbg.apply(frame)
  cv2.imshow('frame', fgmask)

  gray_vid = cv2.cvtColor(frame, cv2.IMREAD_GRAYSCALE)
  cv2.imshow('Original', frame)

  edged_frame = cv2.Canny(frame,100, 200)
  cv2.imshow('Edges', edged_frame)
  
  k= cv2.waitKey(30);
  if k==27:
    break

cap.release()
cv2.destroyAllWindows()
While running the code i get the error :

C:\Users\Sreeram Prakash\Desktop\Project Files>python task1.py
Traceback (most recent call last):
File "task1.py", line 17, in <module>
cv2.imshow('window-name', frame)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
Your program runs well on my PC with Debian 10,
except it opens a lot of windows and writes many files.

After 10 seconds I have more than than 500 files and open windows.

Maybe you get the error because too many windows.

You should reuse the window or
sleep one second before the next step in loop.