Skip to content

Instantly share code, notes, and snippets.

@weathon
Created October 9, 2024 02:58
Show Gist options
  • Select an option

  • Save weathon/8b9d549d892cb62974ae2fe3fdb8edb7 to your computer and use it in GitHub Desktop.

Select an option

Save weathon/8b9d549d892cb62974ae2fe3fdb8edb7 to your computer and use it in GitHub Desktop.
import cv2 as cv
import argparse
import time
import numpy as np
backSub = cv.createBackgroundSubtractorMOG2(history=20)
capture = cv.VideoCapture(cv.samples.findFileOrKeep("/home/wg25r/Downloads/Gasvid_short_video/MOV_1473_short.mp4"))
last_frame = None
while True:
ret, frame = capture.read()
if frame is None:
break
if last_frame is None:
last_frame = frame
frame = np.mean([frame, last_frame], axis=0).astype(np.uint8)
backSub.apply(frame)
fgMask = (1e-6 + np.abs((backSub.getBackgroundImage().astype(float)-frame.astype(float))/255))
fgMask = (fgMask - np.min(fgMask))/(np.max(fgMask) - np.min(fgMask))
# cv.imshow("bakground",backSub.getBackgroundImage())
cv.rectangle(frame, (10, 2), (100, 20), (255, 255, 255), -1)
cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
cv.imshow('Frame', frame)
cv.imshow('FG Mask', fgMask)
keyboard = cv.waitKey(30)
if keyboard == 'q' or keyboard == 27:
break
last_frame = frame
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment