-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_data.py
More file actions
59 lines (43 loc) · 1.67 KB
/
Copy pathprocess_data.py
File metadata and controls
59 lines (43 loc) · 1.67 KB
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
import argparse
import csv
import os
import shutil
# Check and return image label
def get_label(filepath):
label = filepath.split('/')[-1]
return label
# Move files to indicated test/train/data file
def write_files(rootpath, writepath, label, files):
csv_path = os.path.join(writepath, 'data_labels.csv')
with open(csv_path, 'a') as csvfile:
csv_writer = csv.DictWriter(csvfile, fieldnames = ['image_id', 'label'])
for image in files:
csv_entry = {'image_id': image, 'label': label}
csv_writer.writerow(csv_entry)
cur_dir = os.path.join(rootpath, image)
new_dir = os.path.join(writepath, 'images', image)
shutil.move(cur_dir, new_dir)
# Identify image classes and create label file
def prep_data(data_path, write_path, type_data):
for root, _, files in os.walk(data_path):
label = get_label(root)
write_files(root, write_path, label, files)
print("Finished copying files.")
# Create train, test folders
def create_folder(path):
if not os.path.isdir(path):
os.makedirs(path)
os.makedirs(path + '/images')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Argument inputs
parser.add_argument('--write', default = './data', help = 'Processed data write destination. PATH')
parser.add_argument('--unroll', required = True, help = 'Unroll data into labels and images file. PATH')
# Validate and assign argument inputs
args = parser.parse_args()
write_path = args.write
unroll_path = args.unroll
create_folder(write_path)
# Unroll data
if unroll_path:
prep_data(unroll_path, write_path, 'data')