-
Notifications
You must be signed in to change notification settings - Fork 1
/
motion_detect.py
413 lines (328 loc) · 14.1 KB
/
motion_detect.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# import the necessary packages
import argparse
import datetime
import time
import imutils
import numpy as np
import cv2
from os import path
def process(video_path, output_path=None):
# must provide a valid path to a video
if not path.isfile(video_path):
raise RuntimeError("Incorrect path to video file")
process_frame_diff(video_path, output_path)
# process_optical_LK(video_path)
# process_optical_flow(video_path)
# process_frame_diff_optical(video_path)
# process_MOG(video_path)
def get_video_name(video_path):
return str.rsplit(path.basename(path.normpath(video_path)), '.', 1)[0]
def get_video_writer(cap, video_path, output_path=None):
if output_path is None:
output_path = ""
output_path = path.join(output_path, get_video_name(video_path))
output_path += "_" + str(time.strftime("%d-%m-%Y-%H-%M-%S")) + '.avi'
fourcc = cv2.cv.CV_FOURCC(*'XVID')
# See this link for what each index corresponds to:
# http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get
return cv2.VideoWriter(output_path, fourcc, cap.get(5), (int(cap.get(3)), int(cap.get(4))))
def grab_and_convert_frame(cap):
ret, orig = cap.read()
# No more frames left to grab or something went wrong
if not ret:
print('No frame could be grabbed.')
return None, None
frame = imutils.resize(orig, width=600)
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), orig
def process_frame_diff(video_path, output_path=None):
cap = cv2.VideoCapture(video_path)
(background_model, _) = grab_and_convert_frame(cap)
# No more frames left to grab or something went wrong
if background_model is None:
return
dilation_kernel = np.ones((3, 3), np.uint8)
writer = get_video_writer(cap, video_path, output_path)
while True:
frame, orig = grab_and_convert_frame(cap)
if frame is None:
break
# calculate the difference
delta = cv2.absdiff(frame, background_model)
thresh = cv2.threshold(delta, 50, 255, cv2.THRESH_BINARY)[1]
dilation = cv2.dilate(thresh, dilation_kernel, iterations=1)
# found foreground so write to video
if cv2.countNonZero(dilation) > 0:
print("writing frame")
writer.write(orig)
# display frames
cv2.imshow("Current frame", frame)
# cv2.imshow("Background model", background_model)
cv2.imshow("Diff", dilation)
# current frame becomes background model
background_model = frame
# break loop on user input
k = cv2.waitKey(1) & 0xff
if k == ord("q"):
break
elif k == ord('p'):
cv2.imwrite("test_frame_" + str(time.strftime("%d-%m-%Y-%H-%M-%S")) + ".png", frame)
cv2.imwrite("test_thresh_" + str(time.strftime("%d-%m-%Y-%H-%M-%S")) + ".png", dilation)
writer.release()
cap.release()
cv2.destroyAllWindows()
def process_demo(video_path):
"""process the video provided by the video_path to extract portions with motion"""
camera = cv2.VideoCapture(video_path)
# initialize the first frame in the video stream. Note, we assume the first frame is the background
firstFrame = None
# loop over the frames of the video
while True:
# grab the current frame and initialize the occupied/unoccupied text
(grabbed, frame) = camera.read()
text = "Unoccupied"
# if the frame could not be grabbed, then we have reached the end of the video
if not grabbed:
print('No frame could be grabbed. Exiting video processing...')
break
# resize the frame, convert it to grayscale, and blur it a little for noise reduction
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
continue
# compute the absolute difference between the current frame and first frame
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
# dilate the thresholded image to fill in holes, then find contours on thresholded image
thresh = cv2.dilate(thresh, None, iterations=2)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) < args["min_area"]:
continue
# compute the bounding box for the contour, draw it on the frame and update the text
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied"
# draw the text and timestamp on the frame
cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
# show the frame and record if the user presses a key
cv2.imshow("Security Feed", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Delta", frameDelta)
key = cv2.waitKey(1) & 0xFF
# if the `q` key is pressed, break from the lop
if key == ord("q"):
break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
def process_MOG(video_path):
cap = cv2.VideoCapture(video_path)
fgbg = cv2.BackgroundSubtractorMOG(5, 5, 0.01)
while (1):
grabbed, frame = cap.read()
text = "Unoccupied"
# if the frame could not be grabbed, then we have reached the end of the video
if not grabbed:
print('No frame could be grabbed. Exiting video processing...')
break
frame = imutils.resize(frame, width=600)
# gray = frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
fgmask = fgbg.apply(gray)
cv2.imshow('mask', fgmask)
cv2.imshow('frame', frame)
k = cv2.waitKey(30) & 0xff
if k == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
def process_GMG(video_path):
cap = cv2.VideoCapture(video_path)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
fgbg = cv2.BackgroundSubtractorMOG2()
while (1):
grabbed, frame = cap.read()
text = "Unoccupied"
# if the frame could not be grabbed, then we have reached the end of the video
if not grabbed:
print('No frame could be grabbed. Exiting video processing...')
break
frame = imutils.resize(frame, width=800)
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# gray = cv2.GaussianBlur(frame, (21, 21), 0)
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
if k == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
def process_optical_flow(video_path):
cap = cv2.VideoCapture(video_path)
ret, frame1 = cap.read()
frame1 = imutils.resize(frame1, width=800)
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
prvs = cv2.GaussianBlur(prvs, (21, 21), 0)
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
while True:
ret, frame2 = cap.read()
frame2 = imutils.resize(frame2, width=800)
next_f = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
next_f = cv2.GaussianBlur(next_f, (21, 21), 0)
# flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 1, 3, 15, 3, 5, 1)
flow = cv2.calcOpticalFlowFarneback(prvs, next_f, 0.5, 3, 15, 3, 5, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('frame2', rgb)
k = cv2.waitKey(30) & 0xff
if k == ord('q'):
break
# elif k == ord('s'):
# cv2.imwrite('opticalfb.png', frame2)
# cv2.imwrite('opticalhsv.png', rgb)
prvs = next_f
cap.release()
cv2.destroyAllWindows()
def process_frame_diff_optical(video_path):
cap = cv2.VideoCapture(video_path)
(background_model, _) = grab_and_convert_frame(cap)
# No more frames left to grab or something went wrong
if background_model is None:
return
dilation_kernel = np.ones((3, 3), np.uint8)
# Parameters for lucas kanade optical flow
lk_params = dict(winSize=(15, 15),
maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0, 255, (100, 3))
feature_params = dict(maxCorners=100,
qualityLevel=0.3,
minDistance=7,
blockSize=7)
while True:
frame, orig = grab_and_convert_frame(cap)
if frame is None:
break
# calculate the difference
delta = cv2.absdiff(frame, background_model)
thresh = cv2.threshold(delta, 50, 255, cv2.THRESH_BINARY)[1]
dilation = cv2.dilate(thresh, dilation_kernel, iterations=1)
nonzeros = cv2.findNonZero(dilation)
frame_changed = frame.copy();
# Create a mask image for drawing purposes
mask = np.zeros_like(background_model)
if nonzeros is not None and len(nonzeros) > 0:
nonzeros = np.float32(nonzeros)
p1, st, err = cv2.calcOpticalFlowPyrLK(background_model, frame_changed, nonzeros, None, **lk_params)
# Select good points
good_new = p1[st == 1]
good_old = nonzeros[st == 1]
# draw the tracks
for i, (new, old) in enumerate(zip(good_new, good_old)):
# print(i, (new, old))
a, b = new.ravel()
c, d = old.ravel()
cv2.line(mask, (a, b), (c, d), color[i % 100].tolist(), 2)
cv2.circle(frame_changed, (a, b), 5, color[i % 100].tolist(), -1)
frame_changed = cv2.add(frame_changed, mask)
# display frames
cv2.imshow("Current frame", frame_changed)
# cv2.imshow("Background model", background_model)
cv2.imshow("Diff", dilation)
# current frame becomes background model
background_model = frame
# break loop on user input
k = cv2.waitKey(1) & 0xff
if k == ord("q"):
break
elif k == ord('p'):
cv2.imwrite("test_frame_" + str(time.strftime("%d-%m-%Y-%H-%M-%S")) + ".png", frame)
cv2.imwrite("test_thresh_" + str(time.strftime("%d-%m-%Y-%H-%M-%S")) + ".png", dilation)
cap.release()
cv2.destroyAllWindows()
def process_optical_LK(video_path):
cap = cv2.VideoCapture(video_path)
# params for ShiTomasi corner detection
feature_params = dict(maxCorners=100,
qualityLevel=0.3,
minDistance=7,
blockSize=7)
# Parameters for lucas kanade optical flow
lk_params = dict(winSize=(15, 15),
maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0, 255, (100, 3))
# Take first frame and find corners in it
ret, old_frame = cap.read()
if not ret:
return
old_frame = imutils.resize(old_frame, width=800)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
# old_gray, old_orig = grab_and_convert_frame(cap)
# if old_gray is None:
# return
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
while True:
# frame_gray, orig = grab_and_convert_frame(cap)
ret, frame = cap.read()
if not ret:
print('No frame could be grabbed. Exiting video processing...')
break
frame = imutils.resize(frame, width=800)
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# if the frame could not be grabbed, then we have reached the end of the video
# if frame_gray is None:
# print('No frame could be grabbed. Exiting video processing...')
# break
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st == 1]
good_old = p0[st == 1]
# draw the tracks
for i, (new, old) in enumerate(zip(good_new, good_old)):
# print(i, (new, old))
a, b = new.ravel()
c, d = old.ravel()
cv2.line(mask, (a, b), (c, d), color[i % 100].tolist(), 2)
cv2.circle(frame_gray, (a, b), 5, color[i % 100].tolist(), -1)
#
# cv2.imshow('mask', mask)
img = cv2.add(frame, mask)
cv2.imshow('frame', img)
k = cv2.waitKey(30) & 0xff
if k == ord('q'):
break
# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1, 1, 2)
cv2.destroyAllWindows()
cap.release()
def create_and_parse_args():
"""Create the args for the program"""
ap = argparse.ArgumentParser()
ap.add_argument("video", type=str, help="path to the video file")
ap.add_argument("-o", "--out_dir", type=str, help="path to output directory")
ap.add_argument("-a", "--min_area", type=int, default=500, help="minimum area size")
return vars(ap.parse_args())
if __name__ == '__main__':
args = create_and_parse_args()
process(args.get('video', None), args.get('out_dir', None))