forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_subtr_opencv.py
56 lines (42 loc) · 1.57 KB
/
background_subtr_opencv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import argparse
import cv2
def get_opencv_result(video_to_process):
# create VideoCapture object for further video processing
captured_video = cv2.VideoCapture(video_to_process)
# check video capture status
if not captured_video.isOpened:
print("Unable to open: " + video_to_process)
exit(0)
# instantiate background subtraction
background_subtr_method = cv2.bgsegm.createBackgroundSubtractorGSOC()
while True:
# read video frames
retval, frame = captured_video.read()
# check whether the frames have been grabbed
if not retval:
break
# resize video frames
frame = cv2.resize(frame, (640, 360))
# pass the frame to the background subtractor
foreground_mask = background_subtr_method.apply(frame)
# obtain the background without foreground mask
background_img = background_subtr_method.getBackgroundImage()
# show the current frame, foreground mask, subtracted result
cv2.imshow("Initial Frames", frame)
cv2.imshow("Foreground Masks", foreground_mask)
cv2.imshow("Subtraction Result", background_img)
keyboard = cv2.waitKey(10)
if keyboard == 27:
break
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_video",
type=str,
help="Define the full input video path",
default="space_traffic.mp4",
)
# parse script arguments
args = parser.parse_args()
# start BS-pipeline
get_opencv_result(args.input_video)