-
Notifications
You must be signed in to change notification settings - Fork 6
/
RT_OD.py
282 lines (245 loc) · 11.3 KB
/
RT_OD.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
# import the necessary packages
import sys
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time as t
import cv2
from datetime import datetime, time
import datetime
#import from my python files
from SMS import Send_SMS
from MMS import Send_MMS
from Email import Send_Mail
from DropBox import UploadToDropBox
from AlertTime import is_time_between
import ConfigValues
import FaceDetection
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", default="rtsp://192.168.1.10:7447/5cc263bde4b0b09f7c6923cc_2", help="path to video file or url to RTSP")
ap.add_argument("-n", "--name", default="Front Door", help="camera Name")
ap.add_argument("-rsy", "--ROISY", type=int, default=0, help="ROI Start Y")
ap.add_argument("-rsx", "--ROISX", type=int, default=2, help="ROI Start X")
ap.add_argument("-rey", "--ROIEY", type=int, default=358, help="ROI End Y")
ap.add_argument("-rex", "--ROIEX", type=int, default=638, help="ROI End X")
ap.add_argument("-u", "--use-gpu", type=bool, default=False,help="boolean indicating if CUDA GPU should be used")
ap.add_argument("-ac", "--Alert", type=int, default=5,help="Alert Cool Down Period in minutes")
ap.add_argument("-asph", "--AlertStartPeriodHour", type=int, default=0,help="Alerting Period Start Hour")
ap.add_argument("-aspm", "--AlertStartPeriodMin", type=int, default='00',help="Alerting Period Start Min")
ap.add_argument("-aeph", "--AlertEndPeriodHour", type=int, default=23,help="Alerting Period End Hour")
ap.add_argument("-aepm", "--AlertEndPeriodMin", type=int, default=59,help="Alerting Period End Min")
args = vars(ap.parse_args())
#Path to Temp Save Alert Images
Image_Path = ".\Alert_" + args["name"] + ".jpeg" #Use this line on Windows OS
#Image_Path = "Alert_" + args["name"] + ".jpeg" #Use this line on Linux OS
Image_Name = "Alert_" + args["name"] + ".jpeg"
#Alert Times - Only alert between these times
StartAlerts = time(args["AlertStartPeriodHour"],args["AlertStartPeriodMin"])
EndAlerts = time(args["AlertEndPeriodHour"],args["AlertEndPeriodMin"])
#Detection Models
Prototxt = ConfigValues.ReturnRDODProtoTXT()
Model = ConfigValues.ReturnRTODModel()
SystemConfidence = ConfigValues.ReturnRTODSystemConfidence()
#ROI - Area of the image to monitor for object detections. Pixel Co-ordinates
ROIStartY = args["ROISY"] #80 when the trailer isn't home
ROIStartX = args["ROISX"]
ROIEndY = args["ROIEY"]
ROIEndX = args["ROIEX"]
#Time to Sleep between frames - Limites CPU Time Usage by artifically lowering the frame rate
FrameSleep = ConfigValues.ReturnRTODFrameSleep()
AlertSleepPeriod = args["Alert"] #minutes to sleep after sending an alert to prevent alert flooding
# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
#only display this list of classes on the image windows
ClassesToDisplay =["bicycle", "car", "cat", "dog", "motorbike", "person", "pottedplant", "boat"]
#COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
StreamLoaded = "False"
while StreamLoaded == "False":
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(Prototxt, Model)
# check if we are going to use GPU
if args["use_gpu"]:
# set CUDA as the preferable backend and target
print("[INFO] setting preferable backend and target to CUDA...")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter
#print("[INFO] starting video stream..." + args["name"])
print("[INFO] starting video stream..." + args["name"])
try:
RTSP_URL=args["video"]
vs = VideoStream(RTSP_URL).start()
fps = FPS().start()
#Set Date to prevent SMS Flood
#import datetime
SMSAlertDelay = datetime.datetime.now()
FaceDetectionDelay = datetime.datetime.now()
# loop over the frames from the video stream
print("[INFO] attempting video stream loading")
#print("[INFO] starting object detection processing")
while True:
t.sleep(FrameSleep)
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 800 pixels
frame = vs.read()
#frame = imutils.resize(frame, width=800) #defines the size of the picture
#Grab ROI
#[startY:endY, startX:endX]
roi = frame[ROIStartY:ROIEndY, ROIStartX:ROIEndX]
#frame = roi
# grab the frame dimensions and convert it to a blob
(h, w) = roi.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(roi, (300, 300)),0.007843, (300, 300), 127.5)
#blob = cv2.dnn.blobFromImage(roi,1(300, 300), 127.5)
# pass the blob through the network and obtain the detections and
# predictions
net.setInput(blob)
detections = net.forward()
#Draw a box around the area being monitor for Objects
color = (255, 0, 0) # Blue color in BGR
start_point = (ROIStartX, ROIStartY)
end_point = (ROIEndX, ROIEndY)
thickness = 2
frame = cv2.rectangle(frame, start_point, end_point, color, thickness)
y = ROIStartY - 15 if ROIStartY - 15 > 15 else ROIStartY + 15
cv2.putText(frame, "Detection Zone", (ROIStartX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# loop over the detections
for i in np.arange(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence > SystemConfidence:
# extract the index of the class label from the
# `detections`, then compute the (x, y)-coordinates of
# the bounding box for the object
idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the prediction on the frame
label = "{}: {:.2f}%".format(CLASSES[idx],
confidence * 100)
#Only draw the detection clases I am interested in
for o in ClassesToDisplay:
if o in label:
#print(label)
cv2.rectangle(frame, (startX + ROIStartX, startY + ROIStartY), (endX + ROIStartX, endY + ROIStartY),
COLORS[idx], 2)
y = startY + ROIStartY - 15 if startY + ROIStartY - 15 > 15 else startY + ROIStartY + 15
cv2.putText(frame, label, (startX + ROIStartX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
#alert for detection
#print(label)
ValidObjectDetected = 'false'
for vod in ConfigValues.ReturnAlertObjectType():
if(vod in label):
print(label + " Detected")
ValidObjectDetected = 'true'
if(ValidObjectDetected == 'true' and is_time_between(StartAlerts, EndAlerts)):
print(label + " @ " + str(datetime.datetime.now()))
#Test for False Alarm by checking X Lenght of the detection area against the detection
print("")
print("Check for False Detection")
DetectionAreaXLength = args["ROIEX"] - args["ROISX"]
DetectionEventXLength = endX - startX
DectectionPercentage = DetectionEventXLength/DetectionAreaXLength*100
#End Test
if(DectectionPercentage < ConfigValues.ReturnDectectionPercentage()): #The closer to 100% the detection is the more likely it's a false alert.
#Save Image to Disk
cv2.imwrite(Image_Path, frame)
SkipFaceDetection = 'false'
if(SMSAlertDelay < datetime.datetime.now()):
#Send SMS
if(ConfigValues.ReturnTwilioSMSEnabled() == 'true'):
PhoneDestination = ConfigValues.ReturnAlertPhoneDestination()
for PD in PhoneDestination:
Send_SMS(label + " at: " + args["name"], PD)
print("SMS Alerts Sent")
#Send MMS
#Upload image to dropbox and generate a public sharing URL
URL = UploadToDropBox(Image_Name)
print("DropBox Image Public Share URL: " + str(URL))
#Send MMS
if(ConfigValues.ReturnTwilioMMSEnabled() == 'true'):
PhoneDestination = ConfigValues.ReturnAlertPhoneDestination()
for PD in PhoneDestination:
Send_MMS(label + " at: " + args["name"], PD, URL)
print("MMS Alerts Sent")
#Email image of the detection
if(ConfigValues.ReturnMailEnabled() == 'true'):
Send_Mail(Image_Path, args["name"], label)
#Set Alert Delay Period before a new alert can be sent
SMSAlertDelay = datetime.datetime.now() + datetime.timedelta(minutes=AlertSleepPeriod)
loop = 0
print("Alerts Sleeping " + str(AlertSleepPeriod) + " minutes")
SkipFaceDetection = 'true'
else:
SkipFaceDetection = 'false'
#FaceDetection
if(ConfigValues.ReturnFaceDetectionEnabled()=='true' and FaceDetectionDelay < datetime.datetime.now() and SkipFaceDetection=='false'):
print('FaceDetection Enabled')
#[startY:endY, startX:endX]
roiDetected = frame[(startY + ROIStartY):(endY + ROIStartY), (startX + ROIStartX):(endX + ROIStartX)]
cv2.imshow("FaceFrame", roiDetected)
key = cv2.waitKey(1) & 0xFF
#Save Image to Disk
cv2.imwrite(Image_Path, roiDetected)
#Upload image to dropbox and generate a public sharing URL
URL = UploadToDropBox(Image_Name)
print("DropBox Image Public Share URL: " + str(URL))
FaceFound = FaceDetection.FaceDetection(roiDetected)
if(FaceFound=='true'):
FaceDetectionDelay = SMSAlertDelay
else:
print("")
print("False Detection Occured Detection Percentage: " + str(DectectionPercentage))
print("")
else:
if(ValidObjectDetected == 'true'):
#if camera is in cool down period after a detection and the person remains in frame extend the cool down to prevent further alarms
SMSAlertDelay = SMSAlertDelay + datetime.timedelta(seconds=0.5)
print("Object still in frame, Extending Cool Down")
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# update the FPS counter
fps.update()
#stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
#Set StreamLoaded to True after to allow the loop to exit. Loop is only to allow the stream to be restarted if it errors
print("Video Stream & Detection Stopped")
StreamLoaded = "True"
except:
print("")
print("Oops!, ", sys.exc_info()[0])
StreamLoaded = "False"
#stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()
vs.stop()
print ("Attempting to re-aquire Stream")
print("")
#t.sleep(5)
#sys.modules[__name__].__dict__.clear()
# do a bit of cleanup
cv2.destroyAllWindows()
#vs.stop()
print(args["name"] + " has exited. This window is safe to close" )