Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Background-Subtraction/background_subtr_bgslib.py
3119 views
1
import argparse
2
3
import cv2
4
import pybgs as bgs
5
6
7
def get_bgslib_result(video_to_process):
8
# create VideoCapture object for further video processing
9
captured_video = cv2.VideoCapture(video_to_process)
10
# check video capture status
11
if not captured_video.isOpened:
12
print("Unable to open: " + video_to_process)
13
exit(0)
14
15
# instantiate background subtraction
16
background_subtr_method = bgs.SuBSENSE()
17
18
while True:
19
# read video frames
20
retval, frame = captured_video.read()
21
22
# check whether the frames have been grabbed
23
if not retval:
24
break
25
26
# resize video frames
27
frame = cv2.resize(frame, (640, 360))
28
29
# pass the frame to the background subtractor
30
foreground_mask = background_subtr_method.apply(frame)
31
# obtain the background without foreground mask
32
img_bgmodel = background_subtr_method.getBackgroundModel()
33
34
# show the current frame, foreground mask, subtracted result
35
cv2.imshow("Initial Frames", frame)
36
cv2.imshow("Foreground Masks", foreground_mask)
37
cv2.imshow("Subtraction result", img_bgmodel)
38
39
keyboard = cv2.waitKey(10)
40
if keyboard == 27:
41
break
42
43
44
if __name__ == "__main__":
45
parser = argparse.ArgumentParser()
46
parser.add_argument(
47
"--input_video",
48
type=str,
49
help="Define the full input video path",
50
default="space_traffic.mp4",
51
)
52
53
# parse script arguments
54
args = parser.parse_args()
55
56
# start BS-pipeline
57
get_bgslib_result(args.input_video)
58
59
60
61