Skip to content

Instantly share code, notes, and snippets.

@daniellopez0708
Created April 6, 2020 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniellopez0708/46ebcc67de00cf140d92ecf876790a78 to your computer and use it in GitHub Desktop.
Save daniellopez0708/46ebcc67de00cf140d92ecf876790a78 to your computer and use it in GitHub Desktop.
def train_bg_subtractor(inst, cap, num=500):
'''
BG substractor need process some amount of frames to start giving result
'''
print ('Training BG Subtractor...')
i = 0
for frame in cap:
inst.apply(frame, None, 0.001)
i += 1
if i >= num:
return cap
VIDEO_SOURCE = "road.mp4"
bg_subtractor = cv2.createBackgroundSubtractorMOG2(
history=500, detectShadows=True)
# Set up image source
cap = skvideo.io.vreader(VIDEO_SOURCE)
# skipping 500 frames to train bg subtractor
train_bg_subtractor(bg_subtractor, cap, num=500)
frame = next(cap)
fg_mask = bg_subtractor.apply(frame, None, 0.001)
plt.figure(figsize=(12,12))
plt.imshow(fg_mask)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment