Sunday 28 March 2021

opencv 25 background subtraction | motion detection



background subtraction detects movement






#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/live cam.mp4")
background_subtract = cv2.createBackgroundSubtractorMOG2()

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

    mask = background_subtract.apply(frame)

    cv2.imshow('frame', frame)
    cv2.imshow('background subtract', mask)

    if cv2.waitKey(1) == ord('q'):
        break

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

No comments:

Post a Comment