-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrun_tracker.py
executable file
·321 lines (217 loc) · 12.6 KB
/
run_tracker.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
from utilities.python_path_utility import append_to_pythonpath
append_to_pythonpath(['feature_extractors/reid_strong_baseline'
,'feature_extractors/ABD_Net'
,'detectors/mmdetection'
,'evaluation/py_motmetrics'
,'trackers/fair/src/lib'], __file__)
import argparse
import mmcv
import numpy as np
from trackers.deep_sort import DeepSort
from util import draw_bboxes
from tqdm import tqdm
from utilities.helper import xtylwh_to_xyxy
import warnings
import cv2
import logging
import json
from utilities.track_result_statistics import count_tracks
from utilities.python_path_utility import *
import pandas as pd
from feature_extractors.reid_strong_baseline.utils.logger import setup_logger
from datasets.mta_dataset_cam_iterator import get_cam_iterators
from tracking_utils.timer import Timer
class Run_tracker:
def __init__(self,args):
self.cfg = mmcv.Config.fromfile(args.config).root
self.cfg.general.config_basename = os.path.basename(args.config).replace(".py","")
self.use_original_wda = self.cfg.general.config_basename[:4] != "fair"
self.cfg.general.repository_root = os.path.abspath(os.path.dirname(__file__))
self.set_tracker_config_run_path()
self.set_track_features_folder()
#mmdetection does not put everything to the device that is being set in its function calls
#E.g. With torch.cuda.set_device(4) it will run without errors. But still using GPU 0
#With os.environ['CUDA_VISIBLE_DEVICES'] = '4' the visibility will be restricted to only the named GPUS starting internatlly from zero
os.environ['CUDA_VISIBLE_DEVICES'] = self.cfg.general.cuda_visible_devices
#Initializes the detector class by calling the constructor and creating the object
if self.use_original_wda:
from detectors.mmdetection_detector import Mmdetection_detector
self.detector = Mmdetection_detector(self.cfg)
self.deep_sort = DeepSort(self.cfg, use_original_wda=self.use_original_wda)
#Set up the logger
logger = setup_logger("mtmct", self.cfg.general.config_run_path, 0)
logger.info(args)
logger.info(json.dumps(self.cfg,sort_keys=True, indent=4))
def get_detections_path(self,cam_id):
detections_path_folder = os.path.join(self.cfg.general.config_run_path
, "detections")
os.makedirs(detections_path_folder, exist_ok=True)
detections_path = os.path.join(detections_path_folder, "detections_cam_{}.csv".format(cam_id))
return detections_path
def get_detections_frame_nos_path(self, cam_id):
detections_path_folder = os.path.join(self.cfg.general.config_run_path
, "detections")
os.makedirs(detections_path_folder, exist_ok=True)
detections_path = os.path.join(detections_path_folder, "detections_frame_nos_cam_{}.csv".format(cam_id))
return detections_path
def load_detections(self,cam_iterator):
self.detections_to_store = []
self.detections_frame_nos = []
detections_frame_nos_loaded = pd.DataFrame({ "frame_no_cam" : [] })
self.detections_loaded = pd.DataFrame()
self.detections_path = self.get_detections_path(cam_id=cam_iterator.cam_id)
self.detections_frame_nos_path = self.get_detections_frame_nos_path(cam_id=cam_iterator.cam_id)
if os.path.exists(self.detections_frame_nos_path):
detections_frame_nos_loaded = pd.read_csv(self.detections_frame_nos_path)
#This will assure that if the selection of frame nos is changed new detections have to be generated
cam_iterator_frame_nos = cam_iterator.get_all_frame_nos()
frame_nos_union_length = len(set(detections_frame_nos_loaded["frame_no_cam"]).intersection(set(cam_iterator_frame_nos)))
cam_iterator_frame_nos_length = len(cam_iterator_frame_nos)
if frame_nos_union_length == cam_iterator_frame_nos_length and os.path.exists(self.detections_path):
self.detections_loaded = pd.read_csv(self.detections_path)
def set_track_features_folder(self):
# Build the path where transformed track features will be stored for preparation of clustering.
self.cfg.general.track_features_folder = os.path.join(self.cfg.general.repository_root
, "work_dirs"
, "clustering"
, "config_runs"
, self.cfg.general.config_basename
, "pickled_appearance_features"
, "test")
os.makedirs(self.cfg.general.track_features_folder, exist_ok=True)
def set_tracker_config_run_path(self):
# Build the path where results and logging files etc. should be stored
self.cfg.general.config_run_path = os.path.join(self.cfg.general.repository_root
,"work_dirs"
, "tracker"
, "config_runs"
, self.cfg.general.config_basename)
os.makedirs(self.cfg.general.config_run_path, exist_ok=True)
def save_detections(self):
if len(self.detections_to_store) > 0:
detections_to_store_df = pd.DataFrame(self.detections_to_store)
detections_to_store_df = detections_to_store_df.astype({"frame_no_cam": int
, "id": int
, "x": int
, "y": int
, "w": int
, "h": int
, "score": float})
detections_to_store_df.to_csv(self.detections_path, index=False)
detections_frame_nos = pd.DataFrame(self.detections_frame_nos)
detections_frame_nos.to_csv(self.detections_frame_nos_path, index=False)
def store_detections_one_frame(self, frame_no_cam, xywh_bboxes, scores):
'''
"frame_no_cam,id,x,y,w,h,score"
:param xywh_bboxes:
:return:
'''
for index, xywh_bbox_score in enumerate(zip(xywh_bboxes,scores)):
xywh_bbox, score = xywh_bbox_score
self.detections_to_store.append({ "frame_no_cam" : frame_no_cam
, "id" : index
, "x" : xywh_bbox[0]
, "y" : xywh_bbox[1]
, "w" : xywh_bbox[2]
, "h" : xywh_bbox[3]
, "score" : score })
def img_callback_original_wda(self, dataset_img):
if len(self.detections_loaded) > 0:
detections_current_frame = self.detections_loaded[self.detections_loaded["frame_no_cam"] == dataset_img.frame_no_cam]
scores = detections_current_frame["score"].tolist()
bboxes_xtlytlwh = list(zip(detections_current_frame["x"], detections_current_frame["y"],detections_current_frame["w"],detections_current_frame["h"]))
else:
bboxes_xtlytlwh, scores = self.detector.detect(dataset_img.img)
self.store_detections_one_frame(dataset_img.frame_no_cam, bboxes_xtlytlwh, scores)
self.detections_frame_nos.append({ "frame_no_cam" : dataset_img.frame_no_cam })
draw_img = dataset_img.img
if bboxes_xtlytlwh is not None:
outputs = self.deep_sort.update(bboxes_xtlytlwh, scores, dataset_img)
if len(outputs) > 0:
bboxes_xtylwh = outputs[:, :4]
bboxes_xyxy = [ xtylwh_to_xyxy(bbox_xtylwh,dataset_img.img_dims) for bbox_xtylwh in bboxes_xtylwh ]
identities = outputs[:, -2]
detection_idxs = outputs[:, -1]
draw_img = draw_bboxes(dataset_img.img, bboxes_xyxy, identities)
for detection_idx, person_id, bbox in zip(detection_idxs,identities,bboxes_xyxy):
print('%d,%d,%d,%d,%d,%d,%d,%d' % (
dataset_img.frame_no_cam, dataset_img.cam_id, person_id, detection_idx,int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])), file=self.track_results_file)
if self.cfg.general.display_viewer:
cv2.imshow("Annotation Viewer", draw_img)
cv2.waitKey(1)
def img_callback(self, dataset_img):
draw_img = dataset_img.img
outputs = self.deep_sort.update_with_fair_detections(dataset_img)
if len(outputs) > 0:
bboxes_xtylwh = outputs[:, :4]
bboxes_xyxy = [ xtylwh_to_xyxy(bbox_xtylwh,dataset_img.img_dims) for bbox_xtylwh in bboxes_xtylwh ]
identities = outputs[:, -2]
detection_idxs = outputs[:, -1]
draw_img = draw_bboxes(dataset_img.img, bboxes_xyxy, identities)
for detection_idx, person_id, bbox in zip(detection_idxs,identities,bboxes_xyxy):
print('%d,%d,%d,%d,%d,%d,%d,%d' % (
dataset_img.frame_no_cam, dataset_img.cam_id, person_id, detection_idx,int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])), file=self.track_results_file)
if self.cfg.general.display_viewer:
cv2.imshow("Annotation Viewer", draw_img)
cv2.waitKey(1)
def run_on_cam_images(self,cam_iterator):
timer = Timer()
for image in cam_iterator:
self.pbar_tracker.update()
timer.tic()
if self.use_original_wda:
self.img_callback_original_wda(image)
else:
self.img_callback(image)
timer.toc()
return timer.average_time, timer.calls
@staticmethod
def get_cam_iterator_len_sum(cam_image_iterators):
overall_len = 0
for cam_iterator in cam_image_iterators:
overall_len += len(cam_iterator)
return overall_len
def get_track_results_path(self,cam_id):
tracker_results_folder = os.path.join(self.cfg.general.config_run_path,"tracker_results")
os.makedirs(tracker_results_folder,exist_ok=True)
return os.path.join(tracker_results_folder,"track_results_{}.txt".format(cam_id))
def run_on_dataset(self):
logger = logging.getLogger("mtmct")
logger.info("Starting tracking on dataset.")
cam_iterators = get_cam_iterators(self.cfg, self.cfg.data.source.cam_ids)
frame_count_all_cams = self.get_cam_iterator_len_sum(cam_iterators)
self.pbar_tracker = tqdm(total=frame_count_all_cams)
timer_avgs, timer_calls = [], []
for cam_iterator in cam_iterators:
logger.info("Processing cam {}".format(cam_iterator.cam_id))
self.track_results_path = self.get_track_results_path(cam_iterator.cam_id)
logger.info(self.track_results_path)
self.track_results_file = open(self.track_results_path, 'w')
print("frame_no_cam,cam_id,person_id,detection_idx,xtl,ytl,xbr,ybr", file=self.track_results_file)
if self.use_original_wda:
self.load_detections(cam_iterator)
ta, tc = self.run_on_cam_images(cam_iterator)
timer_avgs.append(ta)
timer_calls.append(tc)
self.track_results_file.close()
if self.use_original_wda:
self.save_detections()
track_count_string = count_tracks(self.track_results_path)
logger.info(track_count_string)
timer_avgs = np.asarray(timer_avgs)
timer_calls = np.asarray(timer_calls)
all_time = np.dot(timer_avgs, timer_calls)
avg_time = all_time / np.sum(timer_calls)
logger.info('Time elapsed: {:.2f} seconds, FPS: {:.2f}'.format(all_time, 1.0 / avg_time))
def run(self):
self.run_on_dataset()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str)
return parser.parse_args()
if __name__=="__main__":
if not sys.warnoptions:
warnings.simplefilter("ignore")
args = parse_args()
run_tracker = Run_tracker(args)
run_tracker.run()