-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_detection.py
34 lines (25 loc) · 1.02 KB
/
motion_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
cap = cv.VideoCapture('Pedestrian on roads_ Being cautious is the key to safety __ Cyberabad Traffic Police.mp4')
ret,frame1 = cap.read()
ret,frame2 = cap.read()
while (cap.isOpened()) :
diff = cv.absdiff(frame1,frame2)
gray = cv.cvtColor(diff,cv.COLOR_BGR2GRAY)
blur = cv.GaussianBlur(gray,(5,5),0)
ret,thresh = cv.threshold(blur,20,255,cv.THRESH_BINARY)
dialated = cv.dilate(thresh,None,iterations=3)
contours,hierachy = cv.findContours(dialated,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
for contour in contours :
if cv.contourArea(contour) < 1000 :
continue
(x,y,h,w) = cv.boundingRect(contour)
#final = cv.drawContours(frame1,contours,-1,(0,0,255),2)
imgf = cv.rectangle(frame1,(x,y),(x+w,y+h),(255,0,0),1)
cv.imshow('motion detection',imgf)
frame1 = frame2
ret,frame2 = cap.read()
if cv.waitKey(10)&0xff == ord('q') :
break
cv.destroyAllWindows()