-
Notifications
You must be signed in to change notification settings - Fork 28
/
process_sjtu318.py
360 lines (290 loc) · 12.7 KB
/
process_sjtu318.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -----------------------------------------------------
# Generate Annotations for Person Search Dataset
#
# Author: Liangqi Li
# Creating Date: May 19, 2018
# Latest rectifying: May 20, 2018
# -----------------------------------------------------
import os
import os.path as osp
import shutil
import random
from collections import Counter
import numpy as np
import pandas as pd
def pick_dir(root_dir):
"""Pick out children directions from `root_dir`"""
children_dirs = []
for file_name in os.listdir(root_dir):
abs_file_name = osp.join(root_dir, file_name)
if osp.isdir(abs_file_name):
children_dirs.append(abs_file_name)
return children_dirs
def pick_mp4(root_dir):
"""Pick out mp4 files from `root_dir`"""
videos = []
for file_name in os.listdir(root_dir):
abs_file_name = osp.join(root_dir, file_name)
if abs_file_name[-4:] == '.mp4':
videos.append(abs_file_name)
return videos
def pick_query(root_dir):
"""Pick out jpg or files from `root_dir`"""
queries = []
for file_name in os.listdir(root_dir):
abs_file_name = osp.join(root_dir, file_name)
if abs_file_name[-4:] == '.jpg' or abs_file_name[-4:] == '.png':
queries.append(abs_file_name)
return queries
def pick_txt(root_dir):
"""Pick out txt files from `root_dir`"""
annos = []
for file_name in os.listdir(root_dir):
abs_file_name = osp.join(root_dir, file_name)
if abs_file_name[-4:] == '.txt':
annos.append(abs_file_name)
return annos
def rename_video_dir(root_dir):
"""Rename video dirs that contain jpg images"""
datasets = pick_dir(root_dir)
for dataset in datasets:
persons = pick_dir(dataset)
for person in persons:
video_dirs = pick_dir(person)
for i, video_dir in enumerate(video_dirs):
os.rename(video_dir, os.path.join(person, str(i+1)))
def rename_query(root_dir):
"""Rename query images"""
datasets = pick_dir(root_dir)
for dataset in datasets:
persons = pick_dir(dataset)
for person in persons:
queries = pick_query(person)
for i, query in enumerate(queries):
os.rename(query, os.path.join(
person, 'q_{}.{}'.format(i+1, query[-3:])))
def rename_img_and_txt(root_dir):
"""Rename images and annotation txt files"""
datasets = pick_dir(root_dir)
for dataset in datasets:
persons = pick_dir(dataset)
for person in persons:
video_dirs = pick_dir(person)
for video_dir in video_dirs:
imgs = pick_query(video_dir)
annos = pick_txt(video_dir)
d_name = dataset.split('/')[-1][-1]
p_name = person.split('/')[-1]
v_name = video_dir.split('/')[-1]
for img in imgs:
im_name = img.split('/')[-1][:-4]
os.rename(img, os.path.join(
video_dir, 'd{}_p{}_v{}_{}.jpg'.format(
d_name, p_name, v_name, im_name)))
for anno in annos:
an_name = anno.split('/')[-1][:-4]
os.rename(anno, os.path.join(
video_dir, 'd{}_p{}_v{}_{}.txt'.format(
d_name, p_name, v_name, an_name)))
def collect_files(root_dir, dest_dir):
"""Collect images and annotation txt files"""
img_dest_dir = os.path.join(dest_dir, 'SSM')
anno_dest_dir = os.path.join(dest_dir, 'annotation')
if not os.path.exists(img_dest_dir):
os.mkdir(img_dest_dir)
if not os.path.exists(anno_dest_dir):
os.mkdir(anno_dest_dir)
datasets = pick_dir(root_dir)
for dataset in datasets:
persons = pick_dir(dataset)
for person in persons:
video_dirs = pick_dir(person)
for video_dir in video_dirs:
imgs = pick_query(video_dir)
annos = pick_txt(video_dir)
for img in imgs:
im_name = img.split('/')[-1]
if os.path.join(video_dir, im_name[:-4] + '.txt') in annos:
dest_img = os.path.join(img_dest_dir, im_name)
shutil.copyfile(img, dest_img)
for anno in annos:
an_name = anno.split('/')[-1]
dest_anno = os.path.join(anno_dest_dir, an_name)
shutil.copyfile(anno, dest_anno)
def collect_queries(root_dir, dest_dir):
"""Collect query images"""
query_dest_dir = os.path.join(dest_dir, 'query')
if not os.path.exists(query_dest_dir):
os.mkdir(query_dest_dir)
datasets = pick_dir(root_dir)
for dataset in datasets:
d_name = dataset.split('/')[-1][-1]
persons = pick_dir(dataset)
for person in persons:
p_name = person.split('/')[-1]
queries = pick_query(person)
for query in queries:
q_name = 'd{}_p{}_'.format(d_name, p_name) + \
query.split('/')[-1]
dest_query = os.path.join(query_dest_dir, q_name)
shutil.copyfile(query, dest_query)
def produce_train_and_test(root_dir):
"""Produce `trainAllDF.csv` and `trainImnamesSe.csv`"""
img_dir = os.path.join(root_dir, 'SSM')
anno_dir = os.path.join(root_dir, 'annotation')
txt_dir = os.path.join(anno_dir, 'txt')
train_box_imnames = []
train_boxes = np.zeros((1, 5), dtype=np.int32)
test_box_imnames = []
test_boxes = np.zeros((1, 5), dtype=np.int32)
dataset_to_pid = {1: 0, 2: 75, 3: 145, 4: 235, 5: -30}
txt_annos = pick_txt(txt_dir)
for anno in txt_annos:
im_name = anno.split('/')[-1][:-4] + '.jpg'
d_name = int(anno.split('/')[-1].split('_')[0][1:])
p_name = int(anno.split('/')[-1].split('_')[1][1:])
with open(anno, 'r') as f:
num_box = int(f.readline().rstrip())
for _ in range(num_box):
line = f.readline().rstrip().split(' ')
line = line[0:1] + line[2:]
pid, x1, y1, x2, y2 = [int(i) for i in line]
del_x = x2 - x1
del_y = y2 - y1
if pid == 1:
if d_name != 4:
pid = p_name - 1 + dataset_to_pid[d_name]
else:
if p_name in range(2, 10):
pid = p_name - 2 + dataset_to_pid[d_name]
else:
pid = p_name - 9 + dataset_to_pid[d_name]
if d_name != 5:
if pid > 199:
pid -= 3
elif pid > 182:
pid -= 2
elif pid > 174:
pid -= 1
else:
pid = -1
box = np.array([x1, y1, del_x, del_y, pid])
if d_name != 5:
train_boxes = np.vstack((train_boxes, box))
train_box_imnames.append(im_name)
else:
test_boxes = np.vstack((test_boxes, box))
test_box_imnames.append(im_name)
# Remove the first row
train_boxes = train_boxes[1:]
test_boxes = test_boxes[1:]
# Indicate the order of the column names
ordered_columns = ['imname', 'x1', 'y1', 'del_x', 'del_y', 'cls_id', 'pid']
train_boxes_df = pd.DataFrame(
train_boxes, columns=['x1', 'y1', 'del_x', 'del_y', 'pid'])
train_boxes_df['imname'] = train_box_imnames
train_boxes_df['cls_id'] = np.ones((train_boxes.shape[0], 1),
dtype=np.int32)
train_boxes_df = train_boxes_df[ordered_columns]
test_boxes_df = pd.DataFrame(
test_boxes, columns=['x1', 'y1', 'del_x', 'del_y', 'pid'])
test_boxes_df['imname'] = test_box_imnames
test_boxes_df['cls_id'] = np.ones((test_boxes.shape[0], 1), dtype=np.int32)
test_boxes_df = test_boxes_df[ordered_columns]
train_imnames = list(set(train_box_imnames))
test_imnames = list(set(test_box_imnames))
train_imnames = pd.Series(train_imnames)
test_imnames = pd.Series(test_imnames)
train_boxes_df.to_csv(os.path.join(anno_dir, 'trainAllDF.csv'),
index=False)
test_boxes_df.to_csv(os.path.join(anno_dir, 'testAllDF.csv'), index=False)
train_imnames.to_csv(os.path.join(anno_dir, 'trainImnamesSe.csv'),
index=False)
test_imnames.to_csv(os.path.join(anno_dir, 'testImnamesSe.csv'),
index=False)
def produce_query_set(root_dir):
"""Produce query set"""
anno_dir = os.path.join(root_dir, 'annotation')
test_boxes_df = pd.read_csv(os.path.join(anno_dir, 'testAllDF.csv'))
ordered_columns = ['imname', 'x1', 'y1', 'del_x', 'del_y', 'cls_id', 'pid',
'num_g']
query_boxes_df = pd.DataFrame([['0', 0, 0, 0, 0, 0, 0, 0]],
columns=ordered_columns)
for pid in sorted(list(set(test_boxes_df['pid']))):
if pid == -1:
continue
df = test_boxes_df[test_boxes_df['pid'] == pid]
chosen = random.choice(range(df.shape[0]))
query = df.iloc[chosen].copy()
query['num_g'] = df.shape[0] - 1
query = query.to_frame().transpose()
query_boxes_df = pd.concat((query_boxes_df, query))
query_boxes_df = query_boxes_df.iloc[1:, :]
query_boxes_df.index = range(query_boxes_df.shape[0])
query_boxes_df = query_boxes_df.drop(['cls_id'], axis=1) # remove `cls_id`
query_boxes_df.to_csv(os.path.join(anno_dir, 'queryDF.csv'), index=False)
def produce_query_gallery(root_dir):
"""Produce query_to_gallery"""
anno_dir = os.path.join(root_dir, 'annotation')
test_boxes_df = pd.read_csv(os.path.join(anno_dir, 'testAllDF.csv'))
query_boxes_df = pd.read_csv(os.path.join(anno_dir, 'queryDF.csv'))
test_imnames = pd.read_csv(os.path.join(anno_dir, 'testImnamesSe.csv'),
header=None, squeeze=True)
max_num_g = max(query_boxes_df['num_g'])
chosen_sizes = [50, 100, 200, 500]
gallery_sizes = [size for size in chosen_sizes if size > max_num_g]
for size in gallery_sizes:
queries_to_galleries = [[] for _ in range(query_boxes_df.shape[0])]
for i in range(query_boxes_df.shape[0]):
q_name = query_boxes_df.iloc[i]['imname']
pid = query_boxes_df.iloc[i]['pid']
df = test_boxes_df[test_boxes_df['pid'] == pid]
gt_gallery = list(set(df['imname']))
gt_gallery.remove(q_name)
for gt_im in gt_gallery:
queries_to_galleries[i].append(gt_im)
# Add other images that don't contain the `pid` person to fill
candidates = list(set(test_imnames) - set(df['imname']))
num_to_fill = size - len(queries_to_galleries[i])
chosen_ones = random.sample(candidates, num_to_fill)
queries_to_galleries[i].extend(chosen_ones)
queries_to_galleries = pd.DataFrame(queries_to_galleries,
index=query_boxes_df['imname'])
queries_to_galleries.to_csv(os.path.join(
anno_dir, 'q_to_g{}DF.csv'.format(size)))
def main():
root_dir = '/Users/liliangqi/Desktop/myResearch/mydataset/label_result'
dest_dir = '/Users/liliangqi/Desktop/myResearch/mydataset/sjtu318'
# rename_video_dir(root_dir)
# rename_query(root_dir)
# rename_img_and_txt(root_dir)
# collect_files(root_dir, dest_dir)
# collect_queries(root_dir, dest_dir)
produce_train_and_test(dest_dir)
# produce_query_set(dest_dir)
# produce_query_gallery(dest_dir)
# test_all = pd.read_csv(osp.join(dest_dir, 'annotation', 'testAllDF.csv'))
# test_imnames = pd.read_csv(osp.join(
# dest_dir, 'annotation', 'testImnamesSe.csv'),
# header=None, squeeze=True)
# exception_test_image = []
# for im_name in test_imnames:
# im_df = test_all[test_all['imname'] == im_name]
# counter = Counter(im_df['pid'])
# for id in counter.keys():
# if id > -1 and counter[id] > 1:
# exception_test_image.append(im_name)
train_imnames = pd.read_csv(osp.join(
dest_dir, 'annotation','trainImnamesSe.csv'),
header=None, squeeze=True)
train_all = pd.read_csv(osp.join(dest_dir, 'annotation', 'trainAllDF.csv'))
exception_train_image = []
for im_name in train_imnames:
im_df = train_all[train_all['imname'] == im_name]
counter = Counter(im_df['pid'])
for id in counter.keys():
if id > -1 and counter[id] > 1:
exception_train_image.append(im_name)
print('Debug')
if __name__ == '__main__':
main()