#!/usr/bin/env python # coding: utf-8 # In[6]: import cv2 import numpy as np # In[7]: cap = cv2.VideoCapture("video.mp4") knn = cv2.createBackgroundSubtractorKNN() # In[8]: while True : ret, frame = cap.read() fgmask = knn.apply(frame) th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1] dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)), iterations=2) contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: if cv2.contourArea(c) > 1600: (x,y,w,h) = cv2.boundingRect(c) cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 2) cv2.imshow('detection', frame) if cv2.waitKey(100) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() # In[ ]: # In[ ]: