-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_folder_tree.py
70 lines (56 loc) · 2.41 KB
/
build_folder_tree.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
from argparse import ArgumentParser
import os
from shutil import copyfile
def main(split_path):
# load up the relevant files
images_path = os.path.join(split_path, 'images')
annotations_file = 'annotations.txt'
annotations_file_path = os.path.join(split_path, annotations_file)
with open(file=annotations_file_path, mode='r') as file:
for i, line in enumerate(file):
print(f'{i+1}')
img_name = 'ILSVRC2012_val_{}.JPEG'.format(str(i+1).zfill(8))
class_id = line.split('\t')
class_id = class_id.pop().split('\n')[0]
# make class id folder
class_path = os.path.join(split_path, 'validation', class_id)
if not os.path.exists(class_path):
os.makedirs(class_path)
# move image file
# try:
from_path = os.path.join(images_path, img_name)
to_path = os.path.join(class_path, img_name)
print(from_path, to_path)
copyfile(from_path, to_path)
# except Exception as e:
# print(f'skipping... {from_path}')
def main2lol(split_path):
# load up the relevant files
images_path = os.path.join(split_path, 'images')
annotations_file = 'val.txt'
annotations_file_path = os.path.join(split_path, annotations_file)
with open(file=annotations_file_path, mode='r') as file:
for i, lines in enumerate(file):
print(f'{i+1}')
# img_name = 'ILSVRC2012_val_{}.JPEG'.format(str(i+1).zfill(8))
line = lines.split()
image_name = line[0]
label = line[1]
# make class id folder
class_path = os.path.join(split_path, 'caffeValidation', label)
if not os.path.exists(class_path):
os.makedirs(class_path)
# move image file
# try:
from_path = os.path.join(images_path, image_name)
to_path = os.path.join(class_path, image_name)
print(from_path, to_path)
copyfile(from_path, to_path)
# except Exception as e:
# print(f'skipping... {from_path}')
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('-split_path', help='/path/to/split (test, train, val). Folder must have an images folder and annotations.txt')
args = parser.parse_args()
# main(args.split_path)
main2lol(args.split_path)