-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
400 lines (356 loc) · 14.2 KB
/
main.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
""" Video Analytics Pipeline App- All modules running in parallel with Multithreading,
using data coming from KVS and consuming to KDS
"""
import concurrent.futures
from threading import Thread
import os
from time import sleep
from pprint import pprint
import cv2
import json
from model_detections import ModelDetections
from static_object_detection import StaticObjectDetection
from kinesisUtils.KVS.KVSConsumer import Consume
from kinesisUtils.KDS.KDSProduce import Produce
KVS_STREAM = os.environ["KVS_STREAM"]
KDS_STREAM = os.environ["KDS_STREAM"]
# QUIT flag - to stop producer, consumer and inference
# thread safely, whe, there is Keyboard interrupt
QUIT = False
detect = ModelDetections()
consumer = Consume(KVS_STREAM)
producer = Produce(KDS_STREAM)
# Dictionary - To catch data coming from each VA module.
return_response = {}
# List - To catch incoming response from kvs stream.
return_response_kvs = []
# List - To store & send entire VA metadata per frame.
inference_list = []
static_object = StaticObjectDetection(consumer,
float(os.environ
["INTERSECTION_THRESHOLD"]),
int(os.environ["THRESHOLD_TIME"]))
def loop_kvs_consume():
"""
KVS Consumer Loop: This method populates the response
by consuming frames coming from stream.
:return:
"""
global WAIT_FLAG
WAIT_FLAG = False
while True:
try:
consumer.run(return_response_kvs)
if WAIT_FLAG:
WAIT_FLAG = False
except ValueError as value_error:
if str(value_error) == "Tags not found":
# Waiting for 5 secs if producer not producing anything
WAIT_FLAG = True
print("kvs loop sleeping")
sleep(5)
continue
except Exception as kvs_error:
print(str(kvs_error))
continue
if QUIT:
break
def inference_loop():
"""
Method to do inference on KVS
stream with all VA Modules.
:return: Creates metadata of response
from respective VA Modules
"""
# last_tag - used to parse timestamp.
last_tag = ""
# id_count - used been used for static object detection as frame count.
id_count = 0
while True:
if WAIT_FLAG:
print("inference loop sleeping")
sleep(5)
elif QUIT:
break
# Here we get a frame w.r.t time associated with it.
# tags is timestamp of that frame.
try:
tags, frame = return_response_kvs.pop(0)
if last_tag == "":
last_tag = tags[0]
except IndexError:
continue
except Exception as frame_error:
print(str(frame_error))
continue
if last_tag == tags[0]:
continue
# Here we convert numpy image into bytes
_, image_bytes = cv2.imencode(".jpg", frame)
# Threading AWS rekognition tasks together
# 1) Object detection
# 2) Face detection
# 3) PPE detection
# 4) Face Search
# 5) Static Object Detection
# 6) YOLO Object detection
# Incoming list from USER (from producer metadata)
modules_list =[]
# modules_list = ["ppe_detection","face_detection"]
# Dictionary of all VA modules, where -
# keys: "name_of_va_module" &
# values: List of va function & it's params.
modules_dict = {
"object_detection": [detect.object_detection,
image_bytes.tobytes(), 10,
None, return_response],
"face_detection": [detect.detection_faces,
image_bytes.tobytes(),
return_response],
"ppe_detection": [detect.detection_ppe,
image_bytes.tobytes(),
return_response],
"face_search": [detect.face_search,
image_bytes.tobytes(),
80, 2, return_response],
"static_object": [static_object.detect_static_object,
image_bytes.tobytes(),
id_count, 20, "Person",
return_response],
"yolo_detector": [detect.yolo_detector,
image_bytes.tobytes(),
return_response]
}
# Create a threadingpoolexcecuter for tasks given in list from user
if not modules_list:
modules_list = list(modules_dict.keys())
for module in modules_list:
task_to_submit = modules_dict[module][0]
task_args = modules_dict[module][1:]
with concurrent.futures.ThreadPoolExecutor(12) as module_executor:
module_executor.submit(task_to_submit, *task_args)
metadata_schema = {
"frame_data": [],
"timestamp": tags[0]
}
# Create a copy of metadata for each inference on the frame captured
metadata = metadata_schema.copy()
# Converting AWS rekognition response according
# to the requirements for KDS metadata production
# Appending the inferences to frame_data(array) in the metadata
# Object detection metadata parsing
try:
object_detection_response = \
return_response["object_detection"]["Labels"]
metadata["frame_data"].append(
{
"inference_type": "object_detection",
"boxes": [],
}
)
for _, label in enumerate(object_detection_response):
for i in range(len(label["Instances"])):
metadata["frame_data"][-1]["boxes"].append({
"width": label["Instances"][i]["BoundingBox"]["Width"],
"height": label["Instances"][i]["BoundingBox"]["Height"],
"row": label["Instances"][i]["BoundingBox"]["Top"],
"column": label["Instances"][i]["BoundingBox"]["Left"],
"labels": [{"label": label["Name"],
"confidence": label["Confidence"] / 100
}],})
except KeyError:
pass
except Exception as object_detection_error:
print(str(object_detection_error))
# Face detection metadata parsing
try:
face_detection_response = \
return_response["detection_faces"]["FaceDetails"]
metadata["frame_data"].append(
{
"inference_type": "face_detection",
"boxes": [],
}
)
for _, face_detail in enumerate(face_detection_response):
metadata["frame_data"][-1]["boxes"].append({
"width": face_detail["BoundingBox"]["Width"],
"height": face_detail["BoundingBox"]["Height"],
"row": face_detail["BoundingBox"]["Top"],
"column": face_detail["BoundingBox"]["Left"],
"labels": [{"label": "Face",
"confidence": face_detail["Confidence"] / 100
}],})
except KeyError:
pass
except Exception as face_detection_error:
print(str(face_detection_error))
# PPE detection metadata parsing
try:
ppe_detection_response = \
return_response["detection_ppe"]["Persons"]
metadata["frame_data"].append({
"inference_type": "ppe_detection",
"boxes": [],
})
for _, person in enumerate(ppe_detection_response):
metadata["frame_data"][-1]["boxes"].append({
"width": person["BoundingBox"]["Width"],
"height": person["BoundingBox"]["Height"],
"row": person["BoundingBox"]["Top"],
"column": person["BoundingBox"]["Left"],
"labels": [{"label": "PPE",
"confidence": person["Confidence"] / 100
}],})
# Changes are in feature/fix_PPE_box branch, needs to merge.
except KeyError:
pass
except Exception as ppe_detection_error:
print(str(ppe_detection_error))
# Face Search metadata parsing
try:
face_search_response = return_response["face_search"]
metadata["frame_data"].append({
"inference_type": "face_search",
"boxes": []
})
if face_search_response is None:
pass
else:
if face_search_response["FaceMatches"]:
metadata["frame_data"][-1]["boxes"].append({
"width":
face_search_response['SearchedFaceBoundingBox']['Width'],
"height":
face_search_response['SearchedFaceBoundingBox']["Height"],
"row":
face_search_response["SearchedFaceBoundingBox"]["Top"],
"column":
face_search_response["SearchedFaceBoundingBox"]["Left"],
"labels": [
{"label":
face_search_response["FaceMatches"]
[0]['Face']['ExternalImageId'].split('.')[0],
"confidence":
face_search_response['SearchedFaceConfidence'] / 100
}],})
else:
pass
# print("No Match Found!")
except KeyError:
pass
except Exception as face_search_error:
print(str(face_search_error))
# Static Object Detection metadata parsing
try:
static_object_response = return_response["static_object"]
metadata["frame_data"].append({
"inference_type": "static_object",
"boxes": []
})
if static_object_response is None:
pass
else:
for _, label in enumerate(static_object_response):
for i in range(len(label["Instances"])):
metadata["frame_data"][-1]["boxes"].append({
"width":
label["Instances"][i]["BoundingBox"]["Width"],
"height":
label["Instances"][i]["BoundingBox"]["Height"],
"row":
label["Instances"][i]["BoundingBox"]["Top"],
"column":
label["Instances"][i]["BoundingBox"]["Left"],
"labels": [{"label": label["Instances"]
[i]['staticness']['status'],
"confidence":
label["Confidence"] / 100
}],})
id_count = id_count + 1
except KeyError:
pass
except Exception as static_object_error:
print(str(static_object_error))
# YOLO Object detector
try:
yolo_detector_resp = return_response["yolo_detector"]
metadata["frame_data"].append({
"inference_type": "yolo_detector_resp",
"boxes": []
})
if yolo_detector_resp["yolo_detector"] is None:
pass
else:
for _, label in enumerate(yolo_detector_resp):
metadata["frame_data"][-1]["boxes"].append({
"width":
label["xmax"],
"height":
label["ymax"],
"row":
label["xmin"],
"column":
label["ymin"],
"labels": [{"label": label["name"]
}], })
except KeyError:
pass
pprint(metadata)
# sends response from object Detection to producers
last_index = len(inference_list) - 1
if last_index == -1:
pass
elif inference_list[last_index]["timestamp"] < metadata["timestamp"]:
inference_list.insert(last_index, metadata)
inference_list.append(metadata)
last_tag = tags[0]
# uncomment to log the time stamps
# f = open("logs.txt", "a")
# f.write(str(tags) + '\n')
# f.close()
def producer_loop():
"""
Producer loop that puts inference in producer.
:return: None
"""
while True:
if WAIT_FLAG:
print("producer loop sleeping")
sleep(5)
elif inference_list:
#write Detection App to json
with open('output_data.json', 'a', encoding='utf-8') as output_file:
json.dump(inference_list, output_file, ensure_ascii=False, indent=4)
elif QUIT:
break
# try:
# with concurrent.futures.ThreadPoolExecutor(6) as main_thread:
# kvs_consumer_thread = main_thread.submit(loop_kvs_consume)
#
# inference_thread = main_thread.submit(inference_loop)
#
# inference_thread_1 = main_thread.submit(inference_loop)
#
# kds_producer_thread = main_thread.submit(producer_loop)
#
# except KeyboardInterrupt:
# QUIT = True
# main_thread.shutdown(wait=False)
#
# except Exception as error:
# print(str(error))
# QUIT = True
# main_thread.shutdown(wait=False)
KVS_thread = Thread(target=loop_kvs_consume)
KVS_thread.start()
inference_thread_1 = Thread(target=inference_loop)
inference_thread_1.start()
inference_thread_2 = Thread(target=inference_loop)
inference_thread_2.start()
producer_thread = Thread(target=producer_loop)
producer_thread.start()
KVS_thread.join()
inference_thread_1.join()
inference_thread_2.join()
producer_thread.join()