-
Notifications
You must be signed in to change notification settings - Fork 8
/
NNHandler_yolo.py
403 lines (299 loc) · 12.2 KB
/
NNHandler_yolo.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
401
402
403
import argparse
import json
import numpy as np
import os
import sys
from tqdm import tqdm
import matplotlib.pyplot as plt
from collections import defaultdict
from NNHandler import NNHandler
from NNHandler_image import NNHandler_image, cv2
from Node_Person import Person
from suren.util import Json, eprint
# This is only needed if running YOLO / deepsort
# Not needed if the values are loaded from file
try:
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/submodules/yolov4-deepsort")
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from deep_sort import preprocessing, nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
import core.utils as utils
# NOT NEEDED in this code
# from core.yolov4 import filter_boxes
# from core.config import cfg
except Exception as e:
eprint("Cannot run YOLO:", e)
# def import_tracker(name="deepsort"):
# if name == "deepsort":
# try:
#
# from deep_sort.tracker import Tracker, nn_matching
# from deep_sort.detection import Detection
# from deep_sort.tracker import Tracker
# from tools import generate_detections as gdet
# return True
# except:
# eprint("Deepsort not installed.")
# return False
#
# else:
# raise NotImplementedError
class NNHandler_yolo(NNHandler):
yolo_dir = os.path.dirname(os.path.realpath(__file__)) + "/model/yolov4-deepsort"
model_filename = yolo_dir + '/model_data/mars-small128.pb'
weigths_filename = yolo_dir + '/checkpoints/yolov4-416'
class_names = None
# Definition of the parameters
max_cosine_distance = 0.4
nn_budget = None
nms_max_overlap = 1.0
iou_thresh = .45
score_thresh = .5
input_size = 416
@staticmethod
def YOLO_import():
raise NotImplementedError
@staticmethod
def get_parse():
parser = argparse.ArgumentParser()
parser.add_argument("--input_file", "-i", type=str, dest="input_file", default=None)
parser.add_argument("--output_file", "-o", type=str, dest="output_file", default=None)
parser.add_argument("--overwrite", "--ow", action="store_true", dest="overwrite")
parser.add_argument("--visualize", "--vis", action="store_true", dest="visualize")
parser.add_argument("--verbose", "--verb", action="store_true", dest="verbose")
parser.add_argument("--tracked", "-t", type=bool, dest="tracked", default=True)
args = parser.parse_args()
return args
@staticmethod
def plot(img, bb_list:list, colors:list, is_tracked=False):
n_col = len(colors)
for i, bbox in enumerate(bb_list):
x_min, x_max, y_min, y_max = map(int, [bbox["x1"], bbox["x2"], bbox["y1"], bbox["y2"]])
if is_tracked:
p_id = bbox["id"]
cv2.putText(img, str(p_id), (x_min, y_min- 10), 0, 0.75, (0, 0, 0), 3)
cv2.putText(img, str(p_id), (x_min, y_min- 10), 0, 0.75, (255, 255, 255), 2)
else:
p_id = i
col = colors[p_id%n_col]
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), tuple(col), 2)
def __init__(self, json_file=None, is_tracked=True, vis=True, verbose=True, debug=False):
super().__init__()
print("Creating a YOLO handler")
self.json_file = json_file
self.is_tracked = is_tracked
self.visualize = vis
self.verbose = verbose
self.debug = debug
def create_yolo(self, img_handle, temp_name=False):
"""
:param img_handle: NNHandler_image
:return:
"""
# if not import_tracker(): raise Exception("Couldn't create tracker")
if not os.path.exists(self.yolo_dir): raise Exception("Couldn't find yolo_directory : %s" % (self.yolo_dir))
if not os.path.exists(self.model_filename): raise Exception("Couldn't find model : %s" % (self.model_filename))
if not os.path.exists(self.weigths_filename): raise Exception("Couldn't find weights : %s" % (self.weigths_filename))
tracked_person = {}
# Definition of the parameters
nms_max_overlap = self.nms_max_overlap
iou_thresh = self.iou_thresh
score_thresh = self.score_thresh
input_size = self.input_size
# YOLO encoder
encoder = gdet.create_box_encoder(self.model_filename, batch_size=1)
# initialize deep sort
if self.is_tracked:
max_cosine_distance = self.max_cosine_distance
nn_budget = self.nn_budget
# calculate cosine distance metric
metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
# initialize tracker
tracker = Tracker(metric, n_init=3)
else:
tracker = None
saved_model_loaded = tf.saved_model.load(self.weigths_filename, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
frame_num = 0
img_handle.open()
for t in tqdm(range(img_handle.time_series_length)):
frame = img_handle.read_frame(t)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_num += 1
if self.verbose: print('Frame #: ', frame_num)
# if t < 1000: continue
frame_size = frame.shape[:2]
image_data = cv2.resize(frame, (input_size, input_size))
image_data = image_data / 255.
image_data = image_data[np.newaxis, ...].astype(np.float32)
# run detections on tflite if flag is set
batch_data = tf.constant(image_data)
pred_bbox = infer(batch_data)
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
# print(boxes, pred_conf)
# WTF : why a loop above???
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(
pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=iou_thresh,
score_threshold=score_thresh
)
# convert data to numpy arrays and slice out unused elements
num_objects = valid_detections.numpy()[0]
bboxes = boxes.numpy()[0]
bboxes = bboxes[0:int(num_objects)]
scores = scores.numpy()[0]
scores = scores[0:int(num_objects)]
classes = classes.numpy()[0]
classes = classes[0:int(num_objects)]
# print(num_objects, bboxes, scores, classes)
# format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
original_h, original_w, _ = frame.shape
bboxes = utils.format_boxes(bboxes, original_h, original_w)
# store all predictions in one parameter for simplicity when calling functions
pred_bbox = [bboxes, scores, classes, num_objects]
if self.debug:
print("[xx]", pred_bbox)
if self.class_names is None:
# Give class names
names = ["class_%d" % i for i in classes]
eprint("[xx]", classes)
else:
# custom allowed classes (uncomment line below to customize tracker for only people)
allowed_classes = self.class_names
# loop through objects and use class index to get class name, allow only classes in allowed_classes list
names = []
deleted_indx = []
for i, idx in enumerate(classes):
# print(i, idx, len(allowed_classes))
if int(idx) < len(allowed_classes):
names.append(allowed_classes[int(idx)])
else:
deleted_indx.append(i)
names = np.array(names)
count = len(names)
# delete detections that are not in allowed_classes
bboxes = np.delete(bboxes, deleted_indx, axis=0)
scores = np.delete(scores, deleted_indx, axis=0)
# encode yolo detections and feed to tracker
features = encoder(frame, bboxes)
detections = [Detection(bbox, score, class_name, feature) for bbox, score, class_name, feature in
zip(bboxes, scores, names, features)]
# initialize color map
cmap = plt.get_cmap('tab20b')
colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]
# run non-maxima supression
boxs = np.array([d.tlwh for d in detections])
scores = np.array([d.confidence for d in detections])
classes = np.array([d.class_name for d in detections])
indices = preprocessing.non_max_suppression(boxs, classes, nms_max_overlap, scores)
detections = [detections[i] for i in indices]
# print(detections)
# Call the tracker
if self.is_tracked:
tracker.predict()
tracker.update(detections)
detections = tracker.tracks
person_t = []
# update tracks
for track in detections:
# Get confidence (@suren : Don't think this is needed. Just uncomment/delet)
if self.is_tracked and (not track.is_confirmed() or track.time_since_update > 1):
if not track.is_confirmed():
bbox = track.to_tlbr()
person_t.append({
"x1": bbox[0], "y1": bbox[1], "x2": bbox[2], "y2": bbox[3], "id": -1
})
continue
bbox = track.to_tlbr()
class_name = track.get_class()
# draw bbox on screen
if self.is_tracked:
id = track.track_id
txt = class_name + "-" + str(track.track_id)
else:
id = -1
txt = class_name
if self.visualize:
color = colors[int(id) % len(colors)]
color = [i * 255 for i in color]
cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 2)
cv2.rectangle(frame, (int(bbox[0]), int(bbox[1] - 30)),
(int(bbox[0]) + len(txt) * 17, int(bbox[1])), color, -1)
cv2.putText(frame, txt, (int(bbox[0]), int(bbox[1] - 10)), 0, 0.75, (255, 255, 255), 2)
# if enable info flag then print details about each track
if self.verbose:
print("Tracker ID: {}, Class: {}, BBox Coords (xmin, ymin, xmax, ymax): {}".format(
str(id), class_name, (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]))))
dic = {"x1": bbox[0], "y1": bbox[1], "x2": bbox[2], "y2": bbox[3], "id": id}
if temp_name: dic["name"] = class_name
person_t.append(dic)
# result = np.asarray(frame)
result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
if self.visualize:
cv2.imshow("Output Video", result)
if cv2.waitKey(20) & 0xFF == ord('q'): break
if len(person_t) > 0: tracked_person[t] = person_t
if self.visualize:
cv2.destroyAllWindows()
self.time_series_length = frame_num
self.json_data = tracked_person
def init_from_json(self, file_name=None):
if file_name is None: file_name = self.json_file
if file_name is None or not os.path.exists(file_name): raise ValueError("Json File does not exists : %s"%file_name)
if self.verbose:
print("\t[*] Init from file : %s"%file_name)
with open(file_name, 'r') as json_file:
data = json.load(json_file)
self.time_series_length = data.pop("frames")
self.json_data = data
self.ftype = "json"
return self.json_data
def save_json(self, file_name=None):
if file_name is None: file_name = self.json_file
if not os.path.exists(os.path.dirname(file_name)) : os.makedirs(os.path.dirname(file_name))
js = Json(file_name)
dic = {"frames": self.time_series_length}
for i in self.json_data:
dic[i] = self.json_data[i]
self.ftype = "json"
js.write(dic)
'''
if __name__=="__main__":
img_loc = "./suren/temp/seq18.avi"
json_loc = "./data/vid-01-yolo.json"
parser = argparse.ArgumentParser()
parser.add_argument("--nnout_yolo", "-y", type=str, dest="nnout_yolo", default=json_loc)
parser.add_argument("--video_file", "-v", type=str, dest="video_file", default=img_loc)
parser.add_argument("--overwrite", "-ow", action="store_true", dest="overwrite")
parser.add_argument("--visualize", "--vis", action="store_true", dest="visualize")
parser.add_argument("--verbose", "--verb", action="store_true", dest="verbose")
parser.add_argument("--tracked", "-t", type=bool, dest="tracked", default=True)
args = parser.parse_args()
img_loc = args.video_file
json_loc = args.nnout_yolo
# TEST
img_handle = NNHandler_image(format="avi", img_loc=img_loc)
img_handle.runForBatch()
nn_yolo = NNHandler_yolo(vis=args.visualize, is_tracked=args.tracked)
try:
if os.path.exists(json_loc):
if args.overwrite:
raise Exception("Overwriting json : %s"%json_loc)
# To load YOLO + DSORT track from json
nn_yolo.init_from_json(json_loc)
else:
raise Exception("Json does not exists : %s"%json_loc)
except:
# To create YOLO + DSORT track and save to json
nn_yolo.create_yolo(img_handle)
nn_yolo.save_json(json_loc)
'''