Path: blob/master/Background-Subtraction/background_subtr_opencv.py
3118 views
import argparse12import cv2345def get_opencv_result(video_to_process):6# create VideoCapture object for further video processing7captured_video = cv2.VideoCapture(video_to_process)8# check video capture status9if not captured_video.isOpened:10print("Unable to open: " + video_to_process)11exit(0)1213# instantiate background subtraction14background_subtr_method = cv2.bgsegm.createBackgroundSubtractorGSOC()1516while True:17# read video frames18retval, frame = captured_video.read()1920# check whether the frames have been grabbed21if not retval:22break2324# resize video frames25frame = cv2.resize(frame, (640, 360))2627# pass the frame to the background subtractor28foreground_mask = background_subtr_method.apply(frame)29# obtain the background without foreground mask30background_img = background_subtr_method.getBackgroundImage()3132# show the current frame, foreground mask, subtracted result33cv2.imshow("Initial Frames", frame)34cv2.imshow("Foreground Masks", foreground_mask)35cv2.imshow("Subtraction Result", background_img)3637keyboard = cv2.waitKey(10)38if keyboard == 27:39break404142if __name__ == "__main__":43parser = argparse.ArgumentParser()44parser.add_argument(45"--input_video",46type=str,47help="Define the full input video path",48default="space_traffic.mp4",49)5051# parse script arguments52args = parser.parse_args()5354# start BS-pipeline55get_opencv_result(args.input_video)565758