-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert_ods_tracks.py
161 lines (128 loc) · 5.68 KB
/
convert_ods_tracks.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
'''
Convert ground truth .ods files to '.npy' file containing tensor with shape (id, nframe, [x, y, w, h, v])
'''
import ezodf
import numpy as np
import pandas as pd
data_dir = '/mnt/fileserver/shared/datasets/cameras/Odessa/Duke_on_the_left/experiments/detectron_and_siammask_center_tracking/tracks/'
data_file = data_dir + 'result_meeting_set004_00:59:45-00:59:45.mp4.csv'
ODS_TRACKS = './gt_tracks_example/Duke_Tracks.ods'
ODS_VISIBILITY = './gt_tracks_example/Duke_Visibility.ods'
outfile = data_file[:-4] + '.npy'
doc = ezodf.opendoc(ODS_TRACKS)
print("Spreadsheet contains %d sheet(s)." % len(doc.sheets))
for sheet in doc.sheets:
print("-"*40)
print(" Sheet name : '%s'" % sheet.name)
print("Size of Sheet : (rows=%d, cols=%d)" % (sheet.nrows(), sheet.ncols()) )
# convert the first sheet to a pandas.DataFrame
sheet = doc.sheets[0]
df_dict = {}
for i, row in enumerate(sheet.rows()):
# row is a list of cells
# assume the header is on the first row
if i == 0:
# columns as lists in a dictionary
df_dict = {cell.value:[] for cell in row}
# create index for the column headers
col_index = {j:cell.value for j, cell in enumerate(row)}
continue
for j, cell in enumerate(row):
# use header instead of column index
df_dict[col_index[j]].append(cell.value)
# and convert to a DataFrame
df_id = pd.DataFrame(df_dict)
df_id = df_id.dropna(axis='index', how='all')
df_id = df_id.dropna(axis='columns', how='all')
def assign_visibility(ods_path, tracks):
doc = ezodf.opendoc(ods_path)
print("Spreadsheet contains %d sheet(s)." % len(doc.sheets))
for sheet in doc.sheets:
print("-" * 40)
print(" Sheet name : '%s'" % sheet.name)
print("Size of Sheet : (rows=%d, cols=%d)" % (sheet.nrows(), sheet.ncols()))
# convert the first sheet to a pandas.DataFrame
sheet = doc.sheets[0]
df_dict = {}
for i, row in enumerate(sheet.rows()):
# row is a list of cells
# assume the header is on the first row
if i == 0:
# columns as lists in a dictionary
df_dict = {cell.value: [] for cell in row}
# create index for the column headers
col_index = {j: cell.value for j, cell in enumerate(row)}
continue
for j, cell in enumerate(row):
# use header instead of column index
df_dict[col_index[j]].append(cell.value)
# and convert to a DataFrame
df_vis = pd.DataFrame(df_dict)
df_vis = df_vis.dropna(axis='index', how='all')
df_vis = df_vis.dropna(axis='columns', how='all')
trv = np.zeros([tracks.shape[0], tracks.shape[1], tracks.shape[2]+1])
trv[:,:, :-1] = tracks
for index, row in df_vis.iterrows():
j = 2
while (j < df_vis.shape[1]) and (row[j] is not None):
frame_start = int(row[j].split('[')[1].split('-')[0]) - 1
frame_end = int(row[j].split('-')[1].split(']')[0])
trv[index,frame_start:frame_end,-1] = 1
j += 1
return trv
df = pd.read_csv(data_file, delimiter=",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
df = df.drop(df.columns[6:], axis=1)
nframes = df["FrameId"].max() + 1
tracks_by_obj = -1e8*np.ones([df_id.shape[0], nframes, 4])
for index, row in df_id.iterrows():
print("%i/%i"%(index, df_id.shape[0]))
# if index < 22:
# continue
j = 2
frame_start = []
frame_end = []
id = []
while (j < df_id.shape[1]) and (row[j] is not None):
id.append( int(row[j].split('[')[0]) )
frame_start.append( int(row[j].split('[')[1].split('-')[0])-1 )
frame_end.append(int(row[j].split('-')[1].split(']')[0])-1)
j += 1
# i = frame_start[0]
# while i <= frame_end[0]:
# dfi = df[ df['FrameId'] == i ]
# dfi = dfi[ dfi['Id'] == id ]
# tracks_by_obj[id, i, :] = np.asarray( [dfi['X'], dfi['Y'], dfi['Width'], dfi['Height'] ])[:, 0]
# i += 1
for n in range(len(frame_start)):
# print(n)
i = frame_start[n]
while i <= frame_end[n]:
dfi = df[df['FrameId'] == i]
dfi = dfi[dfi['Id'] == id[n]]
# if dfi.shape[0] < 1:
# print(dfi.shape)
tracks_by_obj[index, i, 0] = float(dfi['X'])
tracks_by_obj[index, i, 1] = float(dfi['Y'])
tracks_by_obj[index, i, 2] = float(dfi['Width'])
tracks_by_obj[index, i, 3] = float(dfi['Height'])
# np.asarray([dfi['X'], dfi['Y'], dfi['Width'], dfi['Height']])[:, 0]
i += 1
if n > 0:
nmissed = frame_start[n] - frame_end[n-1] - 1
if nmissed > 0:
dx = (tracks_by_obj[index, frame_start[n], 0] - tracks_by_obj[index, frame_end[n-1], 0])/nmissed
dy = (tracks_by_obj[index, frame_start[n], 1] - tracks_by_obj[index, frame_end[n - 1], 1])/nmissed
dw = (tracks_by_obj[index, frame_start[n], 2] - tracks_by_obj[index, frame_end[n-1], 2])/nmissed
dh = (tracks_by_obj[index, frame_start[n], 3] - tracks_by_obj[index, frame_end[n - 1], 3])/nmissed
k = 1
for i in range(frame_end[n-1] + 1, frame_start[n]):
x = tracks_by_obj[index, frame_end[n - 1], 0] + k*dx
y = tracks_by_obj[index, frame_end[n - 1], 1] + k*dy
w = tracks_by_obj[index, frame_end[n - 1], 2] + k*dw
h = tracks_by_obj[index, frame_end[n - 1], 3] + k*dh
tracks_by_obj[index, i, :] = np.asarray([x, y, w, h])
k+=1
tracks_by_obj = assign_visibility(ODS_VISIBILITY, tracks_by_obj)
np.save(outfile, tracks_by_obj)
print('')