I have working code for applying background subtraction on a still video but it won't properly write the frames of subtracted background to its output file. I get the .avi file & filename which I specified in cv2.VideoWriter
, but it doesn't seem to write each frame that I pass:
import cv2
import numpy as np
cap = cv2.VideoCapture('traffic-mini.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()
cv2.startWindowThread()
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter('test_output.avi',fourcc, 20.0, (640,480))
while True:
ret, frame = cap.read()
if ret == True:
frame = fgbg.apply(frame)
out.write(frame)
cv2.imshow('fg',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
for i in range (1,5):
cv2.waitKey(1)
The output video test_output.avi
is always 6KB and has no frames passed in. What am I missing? Thanks in advance