2

# Below piece of code is to read a video file called v1.mp4(size about 14MB), perform background subtraction on it, rotate the frame and save it.

import cv2
import argparse
import datetime
import imutils
import time
import numpy as np
cap = cv2.VideoCapture("C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\v1.mp4") #Open video file

fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor

size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

# now creating video writer object  
bgsubvideo = cv2.VideoWriter('C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\bgsubvideo.avi', -1, 20.0,size)
#using any other codec like divx or xvid or mpeg for fourcc did create and video #file and write it, but i couldn't play the file. hence using -1

perform background subtraction frame after frame

while True:

    ret, frame = cap.read()#read a frame

    if ret==True:

        fgmask = fgbg.apply(frame) #Use the substractor
        cv.Flip(fgmask, flipMode=-1)
        bgsubvideo.write(fgmask)

    else:
        break

cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows
bgsubvideo.release()
a_jelly_fish
  • 478
  • 6
  • 21
  • 1
    You might want to look at [this](https://www.pyimagesearch.com/2016/02/22/writing-to-video-with-opencv/). – Rick M. May 17 '18 at 10:58

0 Answers0