Skip to content

Instantly share code, notes, and snippets.

@NickShargan
Created March 28, 2018 17:54
Show Gist options
  • Save NickShargan/925db35499ef6b5f8e421ae74a4adf35 to your computer and use it in GitHub Desktop.
Save NickShargan/925db35499ef6b5f8e421ae74a4adf35 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
video_path = "./vid.mp4"
cap = cv2.VideoCapture(video_path)
target_shape = (640, 360)
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
if not cap.isOpened():
print("error opening video stream")
while (cap.isOpened()):
ret, frame = cap.read()
print(frame.shape)
if ret:
frame = cv2.resize(frame, target_shape, interpolation=cv2.INTER_CUBIC)
fg_mask = fgbg.apply(frame)
fg_mask = cv2.cvtColor(fg_mask, cv2.COLOR_GRAY2BGR)
stacked_frame = np.concatenate((frame, fg_mask), axis=0)
cv2.imshow('frame', stacked_frame)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cv2.destroyAllWindows()
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment