forked from Sense-X/X-Temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_label_kinetics.py
executable file
·86 lines (80 loc) · 2.97 KB
/
gen_label_kinetics.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
import os
dataset_path = 'kinetics_600/train_frames'
label_path = ''
if __name__ == '__main__':
with open('kinetics_label_map.txt') as f:
categories = f.readlines()
categories = [
c.strip().replace(
' ',
'_').replace(
'"',
'').replace(
'(',
'').replace(
')',
'').replace(
"'",
'') for c in categories]
assert len(set(categories)) == 600
dict_categories = {}
for i, category in enumerate(categories):
dict_categories[category] = i
print(dict_categories)
files_input = ['kinetics-600_val.csv', 'kinetics-600_train.csv']
files_output = ['val_videofolder.txt', 'train_videofolder.txt']
for (filename_input, filename_output) in zip(files_input, files_output):
count_cat = {k: 0 for k in dict_categories.keys()}
with open(os.path.join(label_path, filename_input)) as f:
lines = f.readlines()[1:]
folders = []
idx_categories = []
categories_list = []
for line in lines:
line = line.rstrip()
items = line.split(',')
st = int(items[2])
et = int(items[3])
folders.append(items[1] + '_' + "%06d" % st + '_' + "%06d" % et)
this_catergory = items[0].replace(
' ',
'_').replace(
'"',
'').replace(
'(',
'').replace(
')',
'').replace(
"'",
'')
categories_list.append(this_catergory)
idx_categories.append(dict_categories[this_catergory])
count_cat[this_catergory] += 1
print(max(count_cat.values()))
assert len(idx_categories) == len(folders)
missing_folders = []
output = []
for i in range(len(folders)):
curFolder = folders[i]
curIDX = idx_categories[i]
# counting the number of frames in each video folders
img_dir = os.path.join(dataset_path, categories_list[i], curFolder)
if not os.path.exists(img_dir):
missing_folders.append(img_dir)
# print(missing_folders)
else:
dir_files = os.listdir(img_dir)
output.append(
'%s %d %d' %
(os.path.join(
categories_list[i],
curFolder),
len(dir_files),
curIDX))
print(
'%d/%d, missing %d' %
(i, len(folders), len(missing_folders)))
with open(os.path.join(label_path, filename_output), 'w') as f:
f.write('\n'.join(output))
with open(os.path.join(label_path, 'missing_' + filename_output), 'w') as f:
f.write('\n'.join(missing_folders))