-
Notifications
You must be signed in to change notification settings - Fork 72
/
demo.py
335 lines (287 loc) · 12.3 KB
/
demo.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
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import os
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF
from timeit import time
import warnings
import argparse
import sys
import cv2
import numpy as np
import base64
import requests
import urllib
from urllib import parse
import json
import random
import time
from PIL import Image
from collections import Counter
import operator
from yolo_v3 import YOLO3
from yolo_v4 import YOLO4
from deep_sort import preprocessing
from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
from deep_sort.detection import Detection as ddet
from reid import REID
import copy
parser = argparse.ArgumentParser()
parser.add_argument('--version', help='Model(yolo_v3 or yolo_v4)', default='yolo_v4')
parser.add_argument('--videos', nargs='+', help='List of videos', required=True)
parser.add_argument('-all', help='Combine all videos into one', default=True)
args = parser.parse_args() # vars(parser.parse_args())
class LoadVideo: # for inference
def __init__(self, path, img_size=(1088, 608)):
if not os.path.isfile(path):
raise FileExistsError
self.cap = cv2.VideoCapture(path)
self.frame_rate = int(round(self.cap.get(cv2.CAP_PROP_FPS)))
self.vw = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.vh = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.vn = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.width = img_size[0]
self.height = img_size[1]
self.count = 0
print('Length of {}: {:d} frames'.format(path, self.vn))
def get_VideoLabels(self):
return self.cap, self.frame_rate, self.vw, self.vh
def main(yolo):
print(f'Using {yolo} model')
# Definition of the parameters
max_cosine_distance = 0.2
nn_budget = None
nms_max_overlap = 0.4
# deep_sort
model_filename = 'model_data/models/mars-small128.pb'
encoder = gdet.create_box_encoder(model_filename, batch_size=1) # use to get feature
metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
tracker = Tracker(metric, max_age=100)
output_frames = []
output_rectanger = []
output_areas = []
output_wh_ratio = []
is_vis = True
out_dir = 'videos/output/'
print('The output folder is', out_dir)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
all_frames = []
for video in args.videos:
loadvideo = LoadVideo(video)
video_capture, frame_rate, w, h = loadvideo.get_VideoLabels()
while True:
ret, frame = video_capture.read()
if ret is not True:
video_capture.release()
break
all_frames.append(frame)
frame_nums = len(all_frames)
tracking_path = out_dir + 'tracking' + '.avi'
combined_path = out_dir + 'allVideos' + '.avi'
if is_vis:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(tracking_path, fourcc, frame_rate, (w, h))
out2 = cv2.VideoWriter(combined_path, fourcc, frame_rate, (w, h))
# Combine all videos
for frame in all_frames:
out2.write(frame)
out2.release()
# Initialize tracking file
filename = out_dir + '/tracking.txt'
open(filename, 'w')
fps = 0.0
frame_cnt = 0
t1 = time.time()
track_cnt = dict()
images_by_id = dict()
ids_per_frame = []
for frame in all_frames:
image = Image.fromarray(frame[..., ::-1]) # bgr to rgb
boxs = yolo.detect_image(image) # n * [topleft_x, topleft_y, w, h]
features = encoder(frame, boxs) # n * 128
detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)] # length = n
text_scale, text_thickness, line_thickness = get_FrameLabels(frame)
# Run non-maxima suppression.
boxes = np.array([d.tlwh for d in detections])
scores = np.array([d.confidence for d in detections])
indices = preprocessing.delete_overlap_box(boxes, nms_max_overlap, scores)
# indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
detections = [detections[i] for i in indices] # length = len(indices)
# Call the tracker
tracker.predict()
tracker.update(detections)
tmp_ids = []
for track in tracker.tracks:
if not track.is_confirmed() or track.time_since_update > 1:
continue
bbox = track.to_tlbr()
area = (int(bbox[2]) - int(bbox[0])) * (int(bbox[3]) - int(bbox[1]))
if bbox[0] >= 0 and bbox[1] >= 0 and bbox[3] < h and bbox[2] < w:
tmp_ids.append(track.track_id)
if track.track_id not in track_cnt:
track_cnt[track.track_id] = [
[frame_cnt, int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]), area]
]
images_by_id[track.track_id] = [frame[int(bbox[1]):int(bbox[3]), int(bbox[0]):int(bbox[2])]]
else:
track_cnt[track.track_id].append([
frame_cnt,
int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]),
area
])
images_by_id[track.track_id].append(frame[int(bbox[1]):int(bbox[3]), int(bbox[0]):int(bbox[2])])
cv2_addBox(
track.track_id,
frame,
int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]),
line_thickness,
text_thickness,
text_scale
)
write_results(
filename,
'mot',
frame_cnt + 1,
str(track.track_id),
int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]),
w, h
)
ids_per_frame.append(set(tmp_ids))
# save a frame
if is_vis:
out.write(frame)
t2 = time.time()
frame_cnt += 1
print(frame_cnt, '/', frame_nums)
if is_vis:
out.release()
print('Tracking finished in {} seconds'.format(int(time.time() - t1)))
print('Tracked video : {}'.format(tracking_path))
print('Combined video : {}'.format(combined_path))
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
reid = REID()
threshold = 320
exist_ids = set()
final_fuse_id = dict()
print(f'Total IDs = {len(images_by_id)}')
feats = dict()
for i in images_by_id:
print(f'ID number {i} -> Number of frames {len(images_by_id[i])}')
feats[i] = reid._features(images_by_id[i]) # reid._features(images_by_id[i][:min(len(images_by_id[i]),100)])
for f in ids_per_frame:
if f:
if len(exist_ids) == 0:
for i in f:
final_fuse_id[i] = [i]
exist_ids = exist_ids or f
else:
new_ids = f - exist_ids
for nid in new_ids:
dis = []
if len(images_by_id[nid]) < 10:
exist_ids.add(nid)
continue
unpickable = []
for i in f:
for key, item in final_fuse_id.items():
if i in item:
unpickable += final_fuse_id[key]
print('exist_ids {} unpickable {}'.format(exist_ids, unpickable))
for oid in (exist_ids - set(unpickable)) & set(final_fuse_id.keys()):
tmp = np.mean(reid.compute_distance(feats[nid], feats[oid]))
print('nid {}, oid {}, tmp {}'.format(nid, oid, tmp))
dis.append([oid, tmp])
exist_ids.add(nid)
if not dis:
final_fuse_id[nid] = [nid]
continue
dis.sort(key=operator.itemgetter(1))
if dis[0][1] < threshold:
combined_id = dis[0][0]
images_by_id[combined_id] += images_by_id[nid]
final_fuse_id[combined_id].append(nid)
else:
final_fuse_id[nid] = [nid]
print('Final ids and their sub-ids:', final_fuse_id)
print('MOT took {} seconds'.format(int(time.time() - t1)))
t2 = time.time()
# To generate MOT for each person, declare 'is_vis' to True
is_vis = False
if is_vis:
print('Writing videos for each ID...')
output_dir = 'videos/output/tracklets/'
if not os.path.exists(output_dir):
os.mkdir(output_dir)
loadvideo = LoadVideo(combined_path)
video_capture, frame_rate, w, h = loadvideo.get_VideoLabels()
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
for idx in final_fuse_id:
tracking_path = os.path.join(output_dir, str(idx)+'.avi')
out = cv2.VideoWriter(tracking_path, fourcc, frame_rate, (w, h))
for i in final_fuse_id[idx]:
for f in track_cnt[i]:
video_capture.set(cv2.CAP_PROP_POS_FRAMES, f[0])
_, frame = video_capture.read()
text_scale, text_thickness, line_thickness = get_FrameLabels(frame)
cv2_addBox(idx, frame, f[1], f[2], f[3], f[4], line_thickness, text_thickness, text_scale)
out.write(frame)
out.release()
video_capture.release()
# Generate a single video with complete MOT/ReID
if args.all:
loadvideo = LoadVideo(combined_path)
video_capture, frame_rate, w, h = loadvideo.get_VideoLabels()
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
complete_path = out_dir+'/Complete'+'.avi'
out = cv2.VideoWriter(complete_path, fourcc, frame_rate, (w, h))
for frame in range(len(all_frames)):
frame2 = all_frames[frame]
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame)
_, frame2 = video_capture.read()
for idx in final_fuse_id:
for i in final_fuse_id[idx]:
for f in track_cnt[i]:
# print('frame {} f0 {}'.format(frame,f[0]))
if frame == f[0]:
text_scale, text_thickness, line_thickness = get_FrameLabels(frame2)
cv2_addBox(idx, frame2, f[1], f[2], f[3], f[4], line_thickness, text_thickness, text_scale)
out.write(frame2)
out.release()
video_capture.release()
os.remove(combined_path)
print('\nWriting videos took {} seconds'.format(int(time.time() - t2)))
print('Final video at {}'.format(complete_path))
print('Total: {} seconds'.format(int(time.time() - t1)))
def get_FrameLabels(frame):
text_scale = max(1, frame.shape[1] / 1600.)
text_thickness = 1 if text_scale > 1.1 else 1
line_thickness = max(1, int(frame.shape[1] / 500.))
return text_scale, text_thickness, line_thickness
def cv2_addBox(track_id, frame, x1, y1, x2, y2, line_thickness, text_thickness, text_scale):
color = get_color(abs(track_id))
cv2.rectangle(frame, (x1, y1), (x2, y2), color=color, thickness=line_thickness)
cv2.putText(
frame, str(track_id), (x1, y1 + 30), cv2.FONT_HERSHEY_PLAIN, text_scale, (0, 0, 255), thickness=text_thickness)
def write_results(filename, data_type, w_frame_id, w_track_id, w_x1, w_y1, w_x2, w_y2, w_wid, w_hgt):
if data_type == 'mot':
save_format = '{frame},{id},{x1},{y1},{x2},{y2},{w},{h}\n'
else:
raise ValueError(data_type)
with open(filename, 'a') as f:
line = save_format.format(frame=w_frame_id, id=w_track_id, x1=w_x1, y1=w_y1, x2=w_x2, y2=w_y2, w=w_wid, h=w_hgt)
f.write(line)
# print('save results to {}'.format(filename))
warnings.filterwarnings('ignore')
def get_color(idx):
idx = idx * 3
color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
return color
if __name__ == '__main__':
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.3)
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
main(yolo=YOLO3() if args.version == 'v3' else YOLO4())