-
Notifications
You must be signed in to change notification settings - Fork 0
/
detection.py
272 lines (219 loc) · 8.08 KB
/
detection.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
#!/usr/bin/python3
import subprocess
import os
import cv2
import numpy as np
import sys
import time
import sqlite3
import shutil
from jinja2 import Environment, FileSystemLoader
import configparser
config = configparser.ConfigParser()
def dict_factory(cursor, row):
"""
Helper to create a dict out of the DB rows
"""
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def GeneratePage(conn):
"""
Generate webpage with detections based on a jinja2 template
"""
c = conn.cursor()
c.row_factory = dict_factory
c.execute("SELECT * FROM detection order by TimeStamp desc")
detections = c.fetchmany(int(config['HPD']['WebLastDetections']))
for d in detections:
d["large"] = os.path.basename(d["ImagePath"])
file_loader = FileSystemLoader('.')
env = Environment(loader=file_loader)
template = env.get_template('template.html')
output = template.render(detections=detections)
f = open(os.path.join(config['HPD']['DetectionPath'], "index.html"), "w+")
f.write(output)
f.close()
def SaveDetectionToDB(conn, camName, image, videoFile):
"""
Save detections to database
"""
c = conn.cursor()
c.execute("INSERT INTO detection VALUES(?,?,?,?)",
[camName, image, videoFile, time.strftime("%Y-%m-%d %H:%M:%S")])
conn.commit()
def CreateTables(conn):
"""
Database table creation
"""
c = conn.cursor()
# Table for detections
c.execute('''CREATE TABLE IF NOT EXISTS detection (
camName text,
ImagePath text,
VideoPath text,
Timestamp text
)''')
# Table for processed files
c.execute('''CREATE TABLE IF NOT EXISTS files (
file text
)''')
conn.commit()
def LoadFromDB(conn):
"""
Load previously processed files from DB
"""
c = conn.cursor()
c.row_factory = lambda cursor, row: row[0]
c.execute("SELECT file FROM files")
return c.fetchall()
def SaveToDB(conn, name):
"""
Save processed file to DB
"""
# Save name to DB (video file)
c = conn.cursor()
c.execute("INSERT INTO files VALUES(?)", [name])
conn.commit()
def DetectInVideo(net, classes, video):
"""
Detect persons in given video
"""
count = 0
cap = cv2.VideoCapture(video)
while(True):
count += 1
ret, image = cap.read()
if ret is False:
return False, None
if count % int(config['HPD']['SkipFrames']) != 0:
continue
Width = image.shape[1]
Height = image.shape[0]
net.setInput(cv2.dnn.blobFromImage(image, 0.00392, (224, 224), (0, 0, 0), True, crop=False))
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
outs = net.forward(output_layers)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.1:
center_x = int(detection[0] * Width)
center_y = int(detection[1] * Height)
w = int(detection[2] * Width)
h = int(detection[3] * Height)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.1, 0.1)
# If detection is found, draw a red rectangle around person.
for i in indices:
i = i[0]
box = boxes[i]
if class_ids[i] == 0:
cv2.rectangle(image, (round(box[0]), round(box[1])), (round(box[0]+box[2]), round(box[1]+box[3])), (0, 0, 255), 1)
return True, image
return False, None
def SendAlarm(imagePath):
"""
Send motion alarm on MQTT that is handled by hass
Both image and motion trigger
"""
MQTT_BINARY = config['MQTT']['Binary']
MQTT_HOST = config['MQTT']['Host']
MQTT_PORT = config['MQTT']['Port']
MQTT_USER = config['MQTT']['User']
MQTT_PASS = config['MQTT']['Pass']
MQTT_Q_IMAGE = config['MQTT']['ImageQueue']
MQTT_Q_MOTION = config['MQTT']['MotionQueue']
DETECTION_PATH = config['HPD']['DetectionPath']
subprocess.call(
[MQTT_BINARY, "-h", MQTT_HOST, "-p", MQTT_PORT,
"-u", MQTT_USER, "-P", MQTT_PASS, "-r", "-t", MQTT_Q_IMAGE,
"-f", imagePath],
cwd=DETECTION_PATH,
)
subprocess.call(
[MQTT_BINARY, "-h", MQTT_HOST, "-p", MQTT_PORT,
"-u", MQTT_USER, "-P", MQTT_PASS, "-r", "-t", MQTT_Q_MOTION,
"-m", "ON"],
cwd=DETECTION_PATH,
)
def main():
"""
Loop forever in camera dirs for new files to process
"""
config.read(sys.argv[1])
conn = sqlite3.connect(config['HPD']['DBFile'])
CreateTables(conn)
GeneratePage(conn)
# Keep track of already analysed files
print("Loading existing files...", flush=True)
existingFiles = LoadFromDB(conn)
print("Load complete.", flush=True)
with open(config['YOLO']['CocoFile'], 'r') as f:
classes = [line.strip() for line in f.readlines()]
net = cv2.dnn.readNet(config['YOLO']['YoloWeights'], config['YOLO']['YoloConfig'])
# Loop forever
while(True):
for cam in config['CAMERAS']:
dirName = config['CAMERAS'][cam]
detected = False
# Get any new files in each dir
# Check if file exists in DB dict.
now = time.time()
# 2 days old
maxDays = 2 * 86400
for root, subdirs, files in os.walk(dirName):
skip = False
for name in subdirs:
if (now - os.path.getmtime(os.path.join(root, name))) > maxDays:
skip = True
break
if skip:
continue
for videoFile in files:
if not videoFile.endswith(".mp4"):
continue
videoFile = os.path.join(root, videoFile)
# Skip any previously analyzed file
if videoFile in existingFiles:
continue
# Skip empty files
if os.stat(videoFile).st_size == 0:
continue
# If not exists, run detection and if no detection was already
# found. We only analyse for first finding so we dont
# alarm for every part of the recording(s).
if detected is False:
print("Analyzing video", cam, videoFile, flush=True)
t1 = time.perf_counter()
result, image = DetectInVideo(net, classes, videoFile)
t2 = time.perf_counter()
print(f" - Detection took: {t2 - t1:0.4f} seconds.", flush=True)
if result is True:
print(" - Motion detection", cam, time.strftime("%Y-%m-%d %H:%M:%S"), flush=True)
# save image to disk
largeFile = os.path.join(config['HPD']['DetectionPath'], time.strftime("%Y-%m-%d_%H:%M:%S.jpg"))
cv2.imwrite(largeFile, image)
shutil.copy(largeFile, config['HPD']['LastDetectionFile'])
SaveDetectionToDB(conn, cam, largeFile, videoFile)
SendAlarm(largeFile)
GeneratePage(conn)
detected = True
sys.stdout.flush()
# Save file to DB as analysed.
existingFiles.append(videoFile)
SaveToDB(conn, videoFile)
# Perform check with a bit delay
time.sleep(60)
if __name__ == '__main__':
main()