-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoDe.py
285 lines (258 loc) · 9.67 KB
/
MoDe.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
################################################################################
# #
# (Mo)tion (De)tect #
# Detects Motion in a YouTube streams or video files #
# #
# Usage: #
# $ ./python MoDe.py -s https://www.youtube.com/watch?v=<ID> #
# $ ./python MoDe.py -s file.mp4 #
# #
# Notes: #
# Sensitivity is VERY HIGH. #
# increase -G, --gaussian-blur to something higher like 25 (Default 15) #
# increase -C, --contourarea to something HUGE like 10000 (Default 201) #
# #
# Hitting "r" while in screen will reset all values to normal defaults #
# G - 25 , D - 5, C - 10000 #
# # #
# Dependencies: #
# pip install pafy #
# pip install youtube-dl #
# pip install opencv-python #
# #
# backend-youtube-dl will need to be modified to disable likes #
# #
################################################################################
from modules.video_stream import VideoStream
from modules.key_clip_writer import KeyClipWriter
from modules.draw_contours import draw
import modules.osd as osd
import argparse
import cv2
import datetime
import os
import pafy
import time
ap = argparse.ArgumentParser()
#ap.add_argument("-h", "--help", help="This message")
ap.add_argument("-c", "--codec", type=str, default='avc1', help="avc1 x264 mpv4 divx")
ap.add_argument("-q", "--quad", type=int, default=None, help="Enable Quadrant Splitting")
ap.add_argument("-s", "--source", help="http YouTube URL or File Path")
ap.add_argument("-t", "--threading", type=int, default=None, help="Enable Threading")
ap.add_argument("-v", "--verbose", type=int, default=None, help="Enable Verbose")
ap.add_argument("-C", "--contourarea", type=int, default=201, help="Define contourArea")
ap.add_argument("-D", "--delta", type=int, default=25, help="Define Sensitivity Delta")
ap.add_argument("-g", "--gaussianblur", type=int, default=11, help="Define gaussianBlur value")
ap.add_argument("-o", "--outdir", type=str, default="./saved", help="Directory to save clips/captures")
ap.add_argument("-m", "--mode", type=int, default=None, help="Enable Motion Detection")
ap.add_argument("-d", "--debug", type=int, default=None, help="Show Debug Video Frames")
args = vars(ap.parse_args())
if args.get('verbose', None) is None:
verbose = False
else:
verbose = True
if args.get('threading', None) is None:
use_threading = False
else:
use_threading = True
if args.get('quad', None) is None:
show_quadrants = False
else:
show_quadrants = True
if args.get('mode', None) is None:
motion_detect = False
else:
motion_detect = True
if args.get('debug', None) is None:
debug_show = False
else:
debug_show = True
# Set codec variable from CLI Option/Defaults
codec = args["codec"]
if verbose: print("(Mo)tion (De)tect Started...")
buffer_size = 684
kcw = KeyClipWriter(bufSize = buffer_size)
consecFrames = 0
BaseconsecFrames = 0
out_dir = args["outdir"]
# Sensitivity Settings
gnum = args["gaussianblur"]
cnum = args["contourarea"]
dnum = args["delta"]
# Initialize count and show_status
count = 0
show_status = 1
# Stream or Local File
if 'http' in args["source"]:
url = args["source"]
video = pafy.new(url)
if verbose:
for stream in video.streams:
print(stream)
v_title = video.title
best = video.getbest(preftype="mp4")
if verbose: print("Selected:", best)
path = best.url
else:
path = args["source"]
v_title = os.path.basename(path)
# Baseline and setting status_list
baseline_image = None
status_list = [None,None]
if use_threading:
vs = VideoStream(path).start()
time.sleep(5) # Get a chance to buffer some frames
else:
video = cv2.VideoCapture(path)
if verbose: print("Video Capture Started")
# while vs.more():
while True:
if use_threading:
frame = vs.read()
else:
check, frame = video.read()
if frame is None:
if kcw.recording:
kcw.finish()
time.sleep(5)
print("Unable to get frame")
quit()
(frameHeight, frameWidth) = frame.shape[:2]
(frameHalfHeight, frameHalfWidth) = (frameHeight // 2, frameWidth // 2)
status=0
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_frame = cv2.GaussianBlur(gray_frame, (gnum, gnum), 0)
updateConsecFrames = True
resetBaselineFrame = True
if baseline_image is None:
baseline_image = gray_frame
continue
if resetBaselineFrame:
BaseconsecFrames += 1
if BaseconsecFrames == 1000:
baseline_image = gray_frame
BaseconsecFrames = 0
delta = cv2.absdiff(baseline_image, gray_frame)
threshold = cv2.threshold(delta, dnum, 255, cv2.THRESH_BINARY)[1]
(contours, _) = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if motion_detect:
for contour in contours:
draw(frame, contour, cnum)
status=1
else:
show_status=0
status=1
status_list.append(status)
if show_status == 1:
osd.display_status(frame, gnum, cnum, dnum)
if debug_show:
cv2.imshow("gray_frame Frame", gray_frame)
cv2.imshow("Delta Frame", delta)
cv2.imshow("Threshold Frame", threshold)
if show_quadrants:
cv2.namedWindow(v_title + " Q1", cv2.WINDOW_NORMAL)
cv2.imshow(v_title + " Q1",frame[0:frameHalfHeight, 0:frameHalfWidth])
cv2.namedWindow(v_title + " Q2", cv2.WINDOW_NORMAL)
cv2.imshow(v_title + " Q2",frame[0:frameHalfHeight, frameHalfWidth:frameWidth])
cv2.namedWindow(v_title + " Q3", cv2.WINDOW_NORMAL)
cv2.imshow(v_title + " Q3",frame[frameHalfHeight:frameHeight, 0:frameHalfWidth])
cv2.namedWindow(v_title + " Q4", cv2.WINDOW_NORMAL)
cv2.imshow(v_title + " Q4",frame[frameHalfHeight:frameHeight, frameHalfWidth:frameWidth])
else:
cv2.namedWindow(v_title, cv2.WINDOW_NORMAL)
cv2.imshow(v_title, frame)
# Key input jive
key = cv2.waitKey(1)
if key == ord('Q'):
if status == 1:
break
if key == ord('h'):
if status == 1 and show_status == 0:
show_status = 1
else:
show_status = 0
if key == ord('m'):
if status == 1 and motion_detect:
motion_detect = False
else:
motion_detect = True
if key == ord('G'):
if status == 1:
gnum = (gnum + 2)
if key == ord('g'):
if status == 1:
if gnum == 1:
gnum = 1
else:
gnum = (gnum - 2)
if key == ord('C'):
if status == 1:
cnum = (cnum + 1)
if key == ord('c'):
if status == 1:
if cnum == 1:
cnum = 1
else:
cnum = (cnum - 1)
if key == ord('>'):
if status == 1:
cnum = (cnum + 200)
if key == ord('<'):
if status == 1:
if cnum < 201:
cnum = 1
else:
cnum = (cnum - 200)
if key == ord('D'):
if status == 1:
dnum = (dnum + 1)
if key == ord('d'):
if status == 1:
if dnum == 1:
dnum = 1
else:
dnum = (dnum - 1)
if key == ord('r'):
if status == 1:
baseline_image = gray_frame
# Reset settings
#gnum = 21
#cnum = 500
#dnum = 25
if key == ord('s'):
timestamp = datetime.datetime.now()
img_name = "{}/{}.png".format(out_dir,
timestamp.strftime("%Y%m%d-%H%M%S"))
cv2.imwrite(img_name, frame)
count += 1
if key == ord('S'):
if not kcw.recording:
timestamp = datetime.datetime.now()
p = "{}/{}.mp4".format(out_dir,
timestamp.strftime("%Y%m%d-%H%M%S"))
print("Recording...")
kcw.start(p, cv2.VideoWriter_fourcc(*codec), 20, frameWidth, frameHeight)
if updateConsecFrames:
consecFrames += 1
# update the key frame clip buffer
kcw.update(frame)
# if we are recording and reached a threshold on consecutive
# number of frames with no action, stop recording the clip
#if kcw.recording and consecFrames == buffer_size:
# kcw.finish()
# Stop Recording by pressing 'x'
if key == ord('x'):
if kcw.recording:
kcw.finish()
# Pause with 'p'
if key == ord('p'):
cv2.waitKey(-1)
if use_threading:
time.sleep(0.1)
if kcw.recording:
kcw.finish()
time.sleep(5)
#Clean up, Free memory
if use_threading:
vs.stop()
cv2.destroyAllWindows