/
motion_driven_recorder.py
334 lines (244 loc) · 10.4 KB
/
motion_driven_recorder.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
import os
import time
import cv2 as cv
from system.motion_detection import MotionDetector
import imutils
import datetime as dts
import numpy as np
from system.camera_support import CameraConnectionSupport
import config
from system.shared import mkdir_p
import Queue
import uuid
class QueueCommand:
CMD_QUIT_THREAD = "quit"
def __init__(self, cmd):
self.cmd = cmd
self.uid = str(uuid.uuid4())
class MotionDrivenRecorder(CameraConnectionSupport):
def __init__(self, camConnectionString, logger):
CameraConnectionSupport.__init__(self, camConnectionString, logger)
# initializing motion detector
self.detector = MotionDetector()
self.detector.resizeBeforeDetect = False
self.detector.multiFrameDetection = False
self.inMotionDetectedState = False
self._camConnectionDts = None
self._canDetectMotion = False
self.camFps = None
self.preAlarmRecordingSecondsQty = 0
self._savedFrames = []
self._isRecording = False
# output writer
self.outputDirectory = None
self._output = None
self.subFolderNameGeneratorFunc = None
self._prevSubFolder = None
self.scaleFrameTo = None
self._messages_queue = Queue.Queue()
self._quit = False
def add_stop_request(self):
cmd = QueueCommand(QueueCommand.CMD_QUIT_THREAD)
self.logger.info("adding quit command with uid = {}".format(cmd.uid))
self._messages_queue.put(cmd)
def _addPreAlarmFrame(self, frame):
if self.preAlarmRecordingSecondsQty == 0:
return
if self.camFps is None:
return
totalQty = int(self.preAlarmRecordingSecondsQty * self.camFps)
if len(self._savedFrames) < totalQty:
self._savedFrames.append(frame)
return
if len(self._savedFrames) > 0:
self._savedFrames.pop(0)
self._savedFrames.append(frame)
def canDetectMotion(self):
if self._canDetectMotion:
return True
if self._camConnectionDts is None:
return False
minDts = self._camConnectionDts + dts.timedelta(seconds=config.INITIAL_WAIT_INTERVAL_BEFORE_MOTION_DETECTION_SECS)
if minDts > self.utcNow():
return False
self._canDetectMotion = True
return True
def setError(self, errorText):
self.logger.error(errorText)
return CameraConnectionSupport.setError(self, errorText)
def _writeOutFrame(self, frame):
assert self._output is not None
self._output.write(frame)
def _stopRecording(self):
if self._output is None:
return
self._output.release()
self._output = None
self._isRecording = False
def _getSubFolderName(self, dts):
if self.subFolderNameGeneratorFunc is None:
return None
return self.subFolderNameGeneratorFunc(dts)
def _startRecording(self):
if self.outputDirectory is None:
return self.setError("output directory is not specified")
if None in [self.frameWidth, self.frameHeight]:
return self.setError("resolution is't specified")
fourcc = cv.VideoWriter_fourcc(*config.FOURCC_CODEC)
videoSize = (self.frameWidth, self.frameHeight)
# calculation output filename
now = dts.datetime.utcnow()
fileName = "video_{}{}".format(now.strftime("%Y%m%dT%H%M%S"), config.OUTPUT_FILES_EXTENSION)
subFolder = self._getSubFolderName(now)
if subFolder is not None:
needCreate = ((self._prevSubFolder is not None) or (subFolder != self._prevSubFolder))
dirName = os.path.join(self.outputDirectory, subFolder)
dirName = os.path.normpath(dirName)
if (needCreate) and (not os.path.exists(dirName)):
self.logger.info("adding new directory: {}".format(dirName))
if not mkdir_p(dirName):
return self.setError("can't create sub-directory: {}".format(dirName))
fileName = os.path.join(dirName, fileName)
else:
fileName = os.path.join(self.outputDirectory, fileName)
self._output = cv.VideoWriter(fileName, fourcc, config.OUTPUT_FRAME_RATE, videoSize)
self._isRecording = True
return True
def _flushPreRecordingFrames(self):
"""
Writes pre-alarm frames to output file
:return:
"""
assert self._output is not None
for frame in self._savedFrames:
self._output.write(frame)
self._savedFrames = []
return True
def _detect_motion(self, current_frame, instant):
# detection motion if can do it now
if not self.canDetectMotion():
return False
if not self.detector.motionDetected(current_frame):
return False
self.trigger_time = instant # Update the trigger_time
if not self.inMotionDetectedState:
self.logger.info("something moved!")
self.inMotionDetectedState = True
return True
def _process_queue_commands(self):
if self._messages_queue.empty():
return
cmd = self._messages_queue.get()
self.logger.info("got new command - {} [{}]".format(cmd.cmd, cmd.uid))
if cmd.cmd == QueueCommand.CMD_QUIT_THREAD:
self._quit = True
return
self.logger.error("unknown command: {} [{}]".format(cmd.cmd, cmd.uid))
raise Exception("Unknown command")
def start(self): # noqa
"""
Main loop for motion detection tester
:return:
"""
self.logger.info("main loop started")
emptyFrame = None
prev_logged_left_seconds = None
bad_frames = 0
while not self._quit:
self._process_queue_commands()
if self._quit:
break
if bad_frames > 100:
if self.cap is not None:
self.cap.release()
self.cap = None
bad_frames = 0
# initializing connection to camera
if self.cap is None:
self.logger.info("initializing connection to camera")
if self._initCamera() is None:
self.logger.error("can't initialize connection to camera")
continue
self._camConnectionDts = self.utcNow()
ret, current_frame = self.cap.read()
# if can't read current frame - going to the next loop
if (not ret) or (current_frame is None): # the connection broke, or the stream came to an end
self.logger.warning("bad frame")
bad_frames += 1
continue
else:
bad_frames = 0
if self.scaleFrameTo is not None:
current_frame = imutils.resize(current_frame, width=self.scaleFrameTo[0], height=self.scaleFrameTo[1])
# get timestamp of the frame
instant = time.time()
frameHeight = np.size(current_frame, 0)
frameWidth = np.size(current_frame, 1)
if self.camFps is None:
self.camFps = self.cap.get(cv.CAP_PROP_FPS)
self.logger.info("FPS = {}".format(self.camFps))
# adding frame to pre-recording buffer
if self.preAlarmRecordingSecondsQty > 0:
self._addPreAlarmFrame(current_frame)
if emptyFrame is None:
emptyFrame = np.zeros((frameHeight, frameWidth, 3), np.uint8)
resolutionChanged = False
if None in [self.frameWidth, self.frameHeight]:
self.frameWidth = frameWidth
self.frameHeight = frameHeight
self.nb_pixels = self.frameWidth * self.frameHeight
self.logger.info("self.width = {}".format(self.frameWidth))
self.logger.info("self.height = {}".format(self.frameHeight))
resolutionChanged = True
else:
resolutionChanged = ((self.frameWidth != frameWidth) or (self.frameHeight != frameHeight))
if resolutionChanged:
# TODO: need process when recording now, or will be exception!
self.onFrameSizeUpdate(frameWidth, frameHeight)
# detecting motion
motionDetected = self._detect_motion(current_frame, instant)
now = self.utcNow()
# prolongating motion for minimal motion duration
if (not motionDetected) and (self.detector.motionDetectionDts is not None):
minDuration = self.detector.motionDetectionDts + dts.timedelta(seconds=config.MINIMAL_MOTION_DURATION)
if minDuration > now:
motionDetected = True
# clearing motion detection flag when needed
if not motionDetected:
self.inMotionDetectedState = False
if self._isRecording:
self.logger.info("stopping recording...")
self._stopRecording()
elif not self._isRecording:
self.logger.info("starting recording...")
self._startRecording()
self._flushPreRecordingFrames()
# calculating left seconds for motion (for further use in label)
dx = 0
if motionDetected:
dx = now - self.detector.motionDetectionDts
dx = config.MINIMAL_MOTION_DURATION - dx.seconds
if (prev_logged_left_seconds != dx):
self.logger.info("left seconds for motion recording: {}".format(dx))
prev_logged_left_seconds = dx
# adding label for frame with detected motion
if motionDetected:
text = "MOTION DETECTED [{}]".format(dx)
cv.putText(
current_frame,
text,
(10, 20),
cv.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 255), # b g r
2
)
if self._isRecording:
self._writeOutFrame(current_frame)
# stop recording if now recording
if self._isRecording:
self._stopRecording()
if self.cap is not None:
self.cap.release()
cv.destroyAllWindows()
self.logger.info("main loop finished")