{ "cells": [ { "cell_type": "markdown", "id": "6b98a089-5b10-48b3-a62d-703a45c92290", "metadata": {}, "source": [ "# Background Subtraction" ] }, { "cell_type": "code", "execution_count": 1, "id": "d45a3370-5107-4034-9222-0239b4d4585d", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "\n", "backSub = cv2.createBackgroundSubtractorMOG2()\n", "#backSub = cv2.createBackgroundSubtractorKNN()\n", "\n", "capture = cv2.VideoCapture('videos/vtest.avi')\n", "\n", "while True:\n", " ret, frame = capture.read()\n", " if frame is None:\n", " break\n", " \n", " fgMask = backSub.apply(frame)\n", " \n", " \n", " cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)\n", " cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15),\n", " cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))\n", " \n", " \n", " cv2.imshow('Frame', frame)\n", " cv2.imshow('FG Mask', fgMask)\n", " \n", " keyboard = cv2.waitKey(30)\n", " if keyboard == 'q' or keyboard == 27:\n", " break\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "156bd64d-d61b-4c50-b1ad-9d0beb656545", "metadata": {}, "source": [ "