-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionLoop.py
379 lines (324 loc) · 16.9 KB
/
motionLoop.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
#!/usr/bin/env python
"""
===============================================================================================
Sensor for detecting motion using Open CV
Author: Lee Matthews 2018
Open computer vision (Open CV) libraries required
Motion sensing will only run if ENVIRON['motion'] == True. ENVIRON['security'] determines whether
motion raises an alarm or not. If not true then detected motion might only trigger a random event
or conversation for example.
The directory path "/static/images" must exist under the robotAI root directory
===============================================================================================
"""
import datetime
import imutils
import time
import cv2
import os
import logging
import json
#allow for running sensor either in isolation or via robotAI.py
try:
from client.app_utils import getConfig, getConfigData, sendToRobotAPI, busyOn, busyCheck
from client.objectDetect.objectDetector import detectorAPI
except:
from app_utils import getConfig, getConfigData, sendToRobotAPI, busyOn, busyCheck
from objectDetect.objectDetector import detectorAPI
class motionLoop(object):
def __init__(self, ENVIRON, SENSORQ, MIC):
self.Mic = MIC
self.ENVIRON = ENVIRON
self.SENSORQ = SENSORQ
self.TOPDIR = ENVIRON["topdir"]
filename = os.path.join(self.TOPDIR, "static/sqlite/robotAI.sqlite")
if os.path.isfile(filename):
config = getConfigData(self.TOPDIR, "Motion")
if "ERROR" in config:
print ("MotionLoop: Error getting Config: " + config["ERROR"])
debugFlag = 'TRUE'
else:
debugFlag = getConfig(config, "Motion_2debug")
#Set debug level based on details in config DB
self.logger = logging.getLogger(__name__)
logging.basicConfig()
if debugFlag=='TRUE':
self.logger.level = logging.DEBUG
else:
self.logger.level = logging.INFO
# setup variables for motion detection process
#-------------------------------------------------
self.detector = getConfig(config, "Motion_detectMethod")
self.framesCheck = 10
self.motionChat = getConfig(config, "Motion_motionChat")
self.securitychat = getConfig(config, "Motion_securityChat")
self.imagePath = os.path.join(self.TOPDIR, "static/images/")
# try to get the integer values from config
try:
self.chatDelay = int(getConfig(config, "Motion_waitChat"))
self.delay = int(getConfig(config, "Motion_waitDetect"))
self.min_area = int(getConfig(config, "Motion_minarea"))
self.detectPin = int(getConfig(config, "Motion_detectPin"))
except:
self.Mic.say("There is a problem with one of the configuration values, so I am using my defaults.")
self.chatDelay = 0
self.delay = 10
self.min_area = 500
self.detectPin = 0
# variables required to pre-fetch the security chat JSON
self.api_token = ENVIRON["api_token"]
self.api_login = ENVIRON["api_login"]
self.api_url = ENVIRON["api_url"]
self.chatfile = os.path.join(self.TOPDIR, "static/securityChat.json")
# set lastChat so it triggers a chat if motion detected after startup
self.lastChat = datetime.datetime.today() - datetime.timedelta(minutes=self.chatDelay)
# set non to blank as we handle that below using len(self.motionChat)
if not self.motionChat:
self.motionChat = ''
self.logger.debug("Chat triggered by motion is %s" % self.motionChat)
self.logger.debug("Minimum area for movement is %s" % self.min_area)
self.logger.debug("Delay between detection events %s seconds" % self.delay)
self.logger.debug("Delay between chat events %s minutes" % self.chatDelay)
self.logger.debug("Path for saving images is %s" % self.imagePath)
# delete jpg files from images directory when starting sensor
#self.logger.debug("Deleting old files in %s" % self.imagePath)
#filelist = [ f for f in os.listdir(self.imagePath) ]
#for f in filelist:
# os.remove(os.path.join(self.imagePath, f))
# Get security chat json and save as file. This speeds up response for security alert
#============================================================================================
def createFile(self):
chatid = self.securitychat
self.logger.debug("Pre-fetching new security chat file using %s" % chatid)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# temporary fix for 2 part chat IDs (until API fixed)
arr = chatid.split('-')
if len(arr) == 2:
chatid = '0-' + chatid
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if len(self.securitychat) > 0:
jsonpkg = {'subscriberID': self.api_login,
'token': self.api_token,
'chatid' : chatid,
}
api_url = os.path.join(self.api_url, 'chat')
response = sendToRobotAPI('GET', api_url, jsonpkg, self.Mic, self.logger, self.ENVIRON)
with open(self.chatfile, 'w') as outfile:
json.dump(response, outfile)
# Loop to keep checking every 5 seconds whether we should turn motion detection on
#============================================================================================
def runLoop(self):
self.logger.debug("Starting Motion Sensor Loop")
# pre-fetch the security chat text
self.createFile()
# zzzz hard code settings to allow testing
self.ENVIRON["motion"] = True
self.detector = 'Camera'
while True:
self.logger.debug("ENVIRON for Motion and Security : %s and %s" % (self.ENVIRON["motion"], self.ENVIRON["security"]))
if self.ENVIRON["motion"]:
# let the user know security mode is coming
if self.ENVIRON["security"]:
self.Mic.say("Security camera will be enabled in %s seconds. Any detected motion will raise an alarm." % str(self.delay))
# run the motion detection logic based on detector chosen
if self.detector == 'Camera':
self.detectWithCam()
else:
self.detectWithPIR()
else:
time.sleep(5)
# Steps to perform when Security Alert Triggered
# ============================================================================================
def securityWarn(self, camera):
# function to save single image when motion detected
def snapshot(camera, num):
file = time.strftime("%y%m%d%H%M%S") + '-' + str(num) + '.jpg'
fullpath = self.imagePath + file
self.logger.debug("Saving image to %s" % fullpath)
(grabbed, frame) = camera.read()
cv2.imwrite(fullpath, frame)
time.sleep(0.5)
return file
# function to save video when motion detected
def recordVideo(camera, sec):
file = time.strftime("%y%m%d%H%M%S") + '.avi'
fullpath = self.imagePath + file
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(fullpath, fourcc, 20.0, (640,480))
start_time = time.time()
while( int(time.time() - start_time) < sec):
ret, frame = camera.read()
if ret==True:
#frame = cv2.flip(frame, 0)
out.write(frame)
#cv2.imshow('frame',frame)
else:
break
return file
#old code to take 3 snapshots
#f1 = snapshot(camera, 1)
#f2 = snapshot(camera, 2)
#f3 = snapshot(camera, 3)
#command = 'SECURITYCAM ,%s,%s,%s' % (f1, f2, f3)
file = recordVideo(camera, 3)
command = 'SECURITYWARN ,%s' % (file)
self.logger.debug("Posting %s to Queue" % command)
# set system to indicate things are busy
busyOn(self.ENVIRON, self.logger)
self.SENSORQ.put(['brain', command])
# trigger Chat then record more video for reaction (if chat ID configured)
if len(self.securitychat) > 0:
command = 'CHATFILE:%s:%s' % (self.chatfile, self.securitychat)
self.logger.debug("Posting %s to Queue" % command)
# set system to indicate things are busy
busyOn(self.ENVIRON, self.logger)
self.SENSORQ.put(['brain', command])
#record more video while chat is being executed
file = recordVideo(camera, 10)
command = 'SECURITYVIDEO ,%s' % (file)
self.logger.debug("Posting %s to Queue" % command)
# set system to indicate things are busy
busyOn(self.ENVIRON, self.logger)
self.SENSORQ.put(['brain', command])
# Work out what we need to do when motion detected
# ============================================================================================
def detectionEvent(self, lastAlert, camera):
# Check lastAlert to see if we need to trigger a new Alert for the motion
curDTime = datetime.datetime.today()
self.logger.debug("Motion detected at %s " % curDTime)
diff = curDTime - lastAlert
if (diff.seconds) < self.delay:
self.logger.debug("Motion delay has not expired. %s seconds remaining." % diff.seconds)
else:
lastAlert = curDTime
self.logger.debug("Motion detected at %s and motion delay has expired" % curDTime)
# Check listen loop to ensure brain is not busy with an event
#if self.ENVIRON["listen"] == True:
if busyCheck(self.ENVIRON, self.logger) == False:
command = None
# if in security camera mode then capture video
#----------------------------------------------
if self.ENVIRON["security"] == True:
self.logger.debug("Security mode is enabled so trigger security alert")
self.securityWarn(camera)
# else check whether we should begin a chat loop
#----------------------------------------------
else:
self.logger.debug("Checking if we should trigger a chat")
diff = 0
# only trigger a chat if we have a delay and a chat ID
if self.chatDelay > 0 and len(self.motionChat) > 0:
diff = curDTime - self.lastChat
if diff > datetime.timedelta(minutes=self.chatDelay):
self.lastChat = curDTime
command = 'CHATBOT:%s' % self.motionChat
# set system to indicate things are busy
busyOn(self.ENVIRON, self.logger)
self.logger.debug("Posting %s to Queue" % command)
self.SENSORQ.put(['brain', command])
# pre-fetch the security chat text to regenerate file
self.createFile()
return lastAlert
# Loop to detect motion using the PIR Sensor
# ============================================================================================
def detectWithPIR(self):
self.logger.debug("Using PIR sensor on pin %s to detect motion" % self.detectPin)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.detectPin, GPIO.IN, GPIO.PUD_UP)
camera = cv2.VideoCapture(0)
lastAlert = datetime.datetime.today()
frames = 0
# loop continuously cnd check GPIO Pin for motion
while True:
if GPIO.input(self.detectPin) == GPIO.LOW:
self.logger.debug("No motion detected yet.")
else:
lastAlert = self.detectionEvent(lastAlert, camera)
frames += 1
# check the ENVIRON when frame count reaches check point
if frames > self.framesCheck:
self.logger.debug("Checking to see if we should stop detecting motion")
frames = 0
if not self.ENVIRON["motion"]:
self.logger.debug("Time to stop detecting motion")
# cleanup the camera quit function
camera.release()
break
#pause before next loop
time.sleep(0.3)
# Loop to detect motion using the camera
# ============================================================================================
def detectWithCam(self):
self.logger.debug("Starting to detect Motion")
#variables for TensorFlow human detection
model_path = os.path.join(self.TOPDIR, "client/objectDetect/ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb")
detector = detectorAPI(self.ENVIRON, path_to_ckpt=model_path)
# define feed from camera
camera = cv2.VideoCapture(0)
time.sleep(1)
# initialize variables used by the motion sensing
firstFrame = None
lastAlert = datetime.datetime.today()
frames = 0
# loop over the frames of the video feed and detect motion
while True:
# if we are busy processing a job then skip motion until we are done
#if self.ENVIRON["listen"] == False:
if busyCheck(self.ENVIRON, self.logger) == True:
continue
# grab the current frame and initialize the occupied/unoccupied text
self.logger.debug("Getting another frame. ENVIRON listen = %s" % self.ENVIRON["listen"])
(grabbed, frame) = camera.read()
frames += 1
# resize the frame, convert it to grayscale, and blur it
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]
# Update the reference frame
firstFrame = gray
# 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) < self.min_area:
continue
#motion detected, see if a person was detected by objectDetect
#------------------------------------------------------------
objDict = detector.objectCount(frame)
if 'person' in objDict:
lastAlert = self.detectionEvent(lastAlert, camera)
# check the ENVIRON when frame count reaches check point
if frames > self.framesCheck:
self.logger.debug("Checking to see if we should stop detecting motion")
frames = 0
if not self.ENVIRON["motion"]:
self.logger.debug("Time to stop detecting motion")
# cleanup the camera quit function
camera.release()
break
#---------------------------------------------------------------------------
# Function called by main robotAI procedure to start this sensor
#---------------------------------------------------------------------------
def doSensor(ENVIRON, SENSORQ, MIC):
loop = motionLoop(ENVIRON, SENSORQ, MIC)
loop.runLoop()
# **************************************************************************
# This will only be executed when we run the sensor on its own for debugging
# **************************************************************************
if __name__ == "__main__":
print("******** WARNING ********** Starting Sensor from __main__ procedure")
from multiprocessing import Queue
SENSORQ = Queue()
import testSensor
ENVIRON = testSensor.createEnviron()
MIC = testSensor.createMic(ENVIRON, 'pico-tts')
doSensor(ENVIRON, SENSORQ, MIC)