-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_detector.py
64 lines (49 loc) · 1.86 KB
/
motion_detector.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import print_function
import argparse
import datetime
import time
import cv2
import imutils
parser = argparse.ArgumentParser()
parser.add_argument('video', metavar='VIDEO', nargs='?', default='None', help='Path to the video file')
parser.add_argument('-a', '--min-area', type=int, default=300, help='minimum area size')
parser.add_argument('-s', '--start', metavar='SEC', type=int, default=0, help='start time in seconds')
args = parser.parse_args()
if args.video is None:
camera = cv2.VideoCapture(0)
time.sleep(0.25)
else:
camera = cv2.VideoCapture(args.video)
camera.set(cv2.cv.CV_CAP_PROP_POS_MSEC, args.start*1000)
firstFrame = None
status = 'Unoccupied'
while True:
(grabbed, frame) = camera.read()
if not grabbed:
break
frame = imutils.resize(frame, width=480)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if firstFrame is None:
firstFrame = gray
continue
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
if cv2.contourArea(c) < args.min_area:
if status == 'Occupied':
status = 'Unoccupied'
print(camera.get(cv2.cv.CV_CAP_PROP_POS_MSEC), ':', status)
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if status == 'Unoccupied':
status = 'Occupied'
print(camera.get(cv2.cv.CV_CAP_PROP_POS_MSEC), ':', status)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
camera.release()
cv2.destroyAllWindows()