Path: blob/master/Background-Subtraction/background_subtr_bgslib.py
3119 views
import argparse12import cv23import pybgs as bgs456def get_bgslib_result(video_to_process):7# create VideoCapture object for further video processing8captured_video = cv2.VideoCapture(video_to_process)9# check video capture status10if not captured_video.isOpened:11print("Unable to open: " + video_to_process)12exit(0)1314# instantiate background subtraction15background_subtr_method = bgs.SuBSENSE()1617while True:18# read video frames19retval, frame = captured_video.read()2021# check whether the frames have been grabbed22if not retval:23break2425# resize video frames26frame = cv2.resize(frame, (640, 360))2728# pass the frame to the background subtractor29foreground_mask = background_subtr_method.apply(frame)30# obtain the background without foreground mask31img_bgmodel = background_subtr_method.getBackgroundModel()3233# show the current frame, foreground mask, subtracted result34cv2.imshow("Initial Frames", frame)35cv2.imshow("Foreground Masks", foreground_mask)36cv2.imshow("Subtraction result", img_bgmodel)3738keyboard = cv2.waitKey(10)39if keyboard == 27:40break414243if __name__ == "__main__":44parser = argparse.ArgumentParser()45parser.add_argument(46"--input_video",47type=str,48help="Define the full input video path",49default="space_traffic.mp4",50)5152# parse script arguments53args = parser.parse_args()5455# start BS-pipeline56get_bgslib_result(args.input_video)5758596061