-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolation.py
98 lines (83 loc) · 3.84 KB
/
interpolation.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
import sys
import argparse
import numpy as np
import os
import glob
sys.path.append('.')
def make_parser():
parser = argparse.ArgumentParser("Interpolation!")
parser.add_argument("--txt_path", default="", help="path to tracking result path in MOTChallenge format")
parser.add_argument("--save_path", default=None, help="save result path, none for override")
parser.add_argument("--n_min", type=int, default=5, help="minimum")
parser.add_argument("--n_dti", type=int, default=20, help="dti")
return parser
def mkdir_if_missing(d):
if not os.path.exists(d):
os.makedirs(d)
def write_results_score(filename, results):
save_format = '{frame},{id},{x1},{y1},{w},{h},{s},-1,-1,-1\n'
with open(filename, 'w') as f:
for i in range(results.shape[0]):
frame_data = results[i]
frame_id = int(frame_data[0])
track_id = int(frame_data[1])
x1, y1, w, h = frame_data[2:6]
score = frame_data[6]
line = save_format.format(frame=frame_id, id=track_id, x1=x1, y1=y1, w=w, h=h, s=-1)
f.write(line)
def dti(txt_path, save_path, n_min=25, n_dti=20):
seq_txts = sorted(glob.glob(os.path.join(txt_path, '*.txt')))
for seq_txt in seq_txts:
seq_name = seq_txt.split('/')[-1]
print(seq_name)
seq_data = np.loadtxt(seq_txt, dtype=np.float64, delimiter=',')
min_id = int(np.min(seq_data[:, 1]))
max_id = int(np.max(seq_data[:, 1]))
seq_results = np.zeros((1, 10), dtype=np.float64)
for track_id in range(min_id, max_id + 1):
index = (seq_data[:, 1] == track_id)
tracklet = seq_data[index]
tracklet_dti = tracklet
if tracklet.shape[0] == 0:
continue
n_frame = tracklet.shape[0]
n_conf = np.sum(tracklet[:, 6] > 0.5)
if n_frame > n_min:
frames = tracklet[:, 0]
frames_dti = {}
for i in range(0, n_frame):
right_frame = frames[i]
if i > 0:
left_frame = frames[i - 1]
else:
left_frame = frames[i]
# disconnected track interpolation
if 1 < right_frame - left_frame < n_dti:
num_bi = int(right_frame - left_frame - 1)
right_bbox = tracklet[i, 2:6]
left_bbox = tracklet[i - 1, 2:6]
for j in range(1, num_bi + 1):
curr_frame = j + left_frame
curr_bbox = (curr_frame - left_frame) * (right_bbox - left_bbox) / \
(right_frame - left_frame) + left_bbox
frames_dti[curr_frame] = curr_bbox
num_dti = len(frames_dti.keys())
if num_dti > 0:
data_dti = np.zeros((num_dti, 10), dtype=np.float64)
for n in range(num_dti):
data_dti[n, 0] = list(frames_dti.keys())[n]
data_dti[n, 1] = track_id
data_dti[n, 2:6] = frames_dti[list(frames_dti.keys())[n]]
data_dti[n, 6:] = [1, -1, -1, -1]
tracklet_dti = np.vstack((tracklet, data_dti))
seq_results = np.vstack((seq_results, tracklet_dti))
save_seq_txt = os.path.join(save_path, seq_name)
seq_results = seq_results[1:]
seq_results = seq_results[seq_results[:, 0].argsort()]
write_results_score(save_seq_txt, seq_results)
if __name__ == '__main__':
args = make_parser().parse_args()
if args.save_path is None:
args.save_path = args.txt_path
mkdir_if_missing(args.save_path)
dti(args.txt_path, args.save_path, n_min=args.n_min, n_dti=args.n_dti)