-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmulti_camera_multi_person_tracking.py
More file actions
251 lines (204 loc) · 9.8 KB
/
multi_camera_multi_person_tracking.py
File metadata and controls
251 lines (204 loc) · 9.8 KB
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
import argparse
import time
import queue
from threading import Thread
import json
import logging as log
import os
import random
import sys
import cv2 as cv
from utils.network_wrappers import Detector, VectorCNN, MaskRCNN, DetectionsFromFileReader
from mc_tracker.mct import MultiCameraTracker
from utils.analyzer import save_embeddings
from utils.misc import read_py_config, check_pressed_keys, AverageEstimator, set_log_config
from utils.video import MulticamCapture, NormalizerCLAHE
from utils.visualization import visualize_multicam_detections, get_target_size
from openvino.inference_engine import IECore # pylint: disable=import-error,E0611
set_log_config()
def check_detectors(args):
detectors = {
'--m_detector': args.m_detector,
'--m_segmentation': args.m_segmentation,
'--detections': args.detections
}
non_empty_detectors = [(det, value) for det, value in detectors.items() if value]
det_number = len(non_empty_detectors)
if det_number == 0:
log.error('No detector specified, please specify one of the following parameters: '
'\'--m_detector\', \'--m_segmentation\' or \'--detections\'')
elif det_number > 1:
det_string = ''.join('\n\t{}={}'.format(det[0], det[1]) for det in non_empty_detectors)
log.error('Only one detector expected but got {}, please specify one of them:{}'
.format(len(non_empty_detectors), det_string))
return det_number
def update_detections(output, detections, frame_number):
for i, detection in enumerate(detections):
entry = {'frame_id': frame_number, 'scores': [], 'boxes': []}
for det in detection:
entry['boxes'].append(det[0])
entry['scores'].append(float(det[1]))
output[i].append(entry)
def save_json_file(save_path, data, description=''):
save_dir = os.path.dirname(save_path)
if save_dir and not os.path.exists(save_dir):
os.makedirs(save_dir)
with open(save_path, 'w') as outfile:
json.dump(data, outfile)
if description:
log.info('{} saved to {}'.format(description, save_path))
class FramesThreadBody:
def __init__(self, capture, max_queue_length=2):
self.process = True
self.frames_queue = queue.Queue()
self.capture = capture
self.max_queue_length = max_queue_length
# print(self.frames_queue)
def __call__(self):
while self.process:
if self.frames_queue.qsize() > self.max_queue_length:
time.sleep(0.1)
has_frames, frames, timestamps = self.capture.get_frames()
print(timestamps)
if not has_frames and self.frames_queue.empty():
self.process = False
break
if has_frames:
self.frames_queue.put(frames)
def run(params, config, capture, detector, reid):
win_name = 'Multi camera tracking'
frame_number = 0
avg_latency = AverageEstimator()
output_detections = [[] for _ in range(capture.get_num_sources())]
key = -1
if config['normalizer_config']['enabled']:
capture.add_transform(
NormalizerCLAHE(
config['normalizer_config']['clip_limit'],
config['normalizer_config']['tile_size'],
)
)
tracker = MultiCameraTracker(capture.get_num_sources(), reid, config['sct_config'], **config['mct_config'],
visual_analyze=config['analyzer'])
thread_body = FramesThreadBody(capture, max_queue_length=len(capture.captures) * 2)
frames_thread = Thread(target=thread_body)
frames_thread.start()
# print(tracker, thread_body, frames_thread)
# TODO: Study this more
if len(params.output_video):
frame_size, fps = capture.get_source_parameters()
target_width, target_height = get_target_size(frame_size, None, **config['visualization_config'])
video_output_size = (target_width, target_height)
fourcc = cv.VideoWriter_fourcc(*'XVID')
output_video = cv.VideoWriter(params.output_video, fourcc, min(fps), video_output_size)
else:
output_video = None
prev_frames = thread_body.frames_queue.get()
detector.run_async(prev_frames, frame_number)
while thread_body.process:
if not params.no_show:
key = check_pressed_keys(key)
if key == 27:
break
start = time.time()
try:
frames = thread_body.frames_queue.get_nowait()
except queue.Empty:
frames = None
if frames is None:
continue
all_detections = detector.wait_and_grab()
if params.save_detections:
update_detections(output_detections, all_detections, frame_number)
frame_number += 1
detector.run_async(frames, frame_number)
all_masks = [[] for _ in range(len(all_detections))]
for i, detections in enumerate(all_detections):
all_detections[i] = [det[0] for det in detections]
all_masks[i] = [det[2] for det in detections if len(det) == 3]
tracker.process(prev_frames, all_detections, all_masks)
tracked_objects = tracker.get_tracked_objects()
latency = time.time() - start
avg_latency.update(latency)
fps = round(1. / latency, 1)
vis = visualize_multicam_detections(capture.get_num_sources(), prev_frames, tracked_objects, fps, **config['visualization_config'])
if not params.no_show:
cv.imshow(win_name, vis)
if output_video:
output_video.write(cv.resize(vis, video_output_size))
print('\rProcessing frame: {}, fps = {} (avg_fps = {:.3})'.format(
frame_number, fps, 1. / avg_latency.get()), end="")
prev_frames, frames = frames, prev_frames
print('')
thread_body.process = False
frames_thread.join()
if len(params.history_file):
save_json_file(params.history_file, tracker.get_all_tracks_history(), description='History file')
if len(params.save_detections):
save_json_file(params.save_detections, output_detections, description='Detections')
if len(config['embeddings']['save_path']):
save_embeddings(tracker.scts, **config['embeddings'])
def main():
current_dir = os.path.dirname(os.path.abspath(__file__))
"""Prepares data for the person recognition demo"""
parser = argparse.ArgumentParser(description='Multi camera multi person \
tracking live demo script')
parser.add_argument('-i', type=str, nargs='+', help='Input sources (indexes \
of cameras or paths to video files)', required=True)
parser.add_argument('--config', type=str, default=os.path.join(current_dir, 'config.py'), required=False,
help='Configuration file')
parser.add_argument('--detections', type=str, help='JSON file with bounding boxes')
parser.add_argument('-m', '--m_detector', type=str, required=False,
help='Path to the person detection model')
parser.add_argument('--t_detector', type=float, default=0.6,
help='Threshold for the person detection model')
parser.add_argument('--m_segmentation', type=str, required=False,
help='Path to the person instance segmentation model')
parser.add_argument('--t_segmentation', type=float, default=0.6,
help='Threshold for person instance segmentation model')
parser.add_argument('--m_reid', type=str, required=True,
help='Path to the person re-identification model')
parser.add_argument('--output_video', type=str, default='', required=False,
help='Optional. Path to output video')
parser.add_argument('--history_file', type=str, default='', required=False,
help='Optional. Path to file in JSON format to save results of the demo')
parser.add_argument('--save_detections', type=str, default='', required=False,
help='Optional. Path to file in JSON format to save bounding boxes')
parser.add_argument("--no_show", help="Optional. Don't show output", action='store_true')
parser.add_argument('-d', '--device', type=str, default='CPU')
parser.add_argument('-l', '--cpu_extension',
help='MKLDNN (CPU)-targeted custom layers.Absolute \
path to a shared library with the kernels impl.',
type=str, default=None)
args = parser.parse_args()
print(args)
if check_detectors(args) != 1:
sys.exit(1)
if len(args.config):
log.info('Reading configuration file {}'.format(args.config))
config = read_py_config(args.config)
else:
log.error('No configuration file specified. Please specify parameter \'--config\'')
sys.exit(1)
random.seed(config['random_seed'])
capture = MulticamCapture(args.i)
log.info("Creating Inference Engine")
ie = IECore()
if args.detections:
person_detector = DetectionsFromFileReader(args.detections, args.t_detector)
elif args.m_segmentation:
person_detector = MaskRCNN(ie, args.m_segmentation, args.t_segmentation,
args.device, args.cpu_extension,
capture.get_num_sources())
else:
person_detector = Detector(ie, args.m_detector, args.t_detector,
args.device, args.cpu_extension,
capture.get_num_sources())
if args.m_reid:
person_recognizer = VectorCNN(ie, args.m_reid, args.device, args.cpu_extension)
else:
person_recognizer = None
run(args, config, capture, person_detector, person_recognizer)
log.info('Demo finished successfully')
if __name__ == '__main__':
main()