-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_data.py
69 lines (50 loc) · 1.86 KB
/
split_data.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
import os, shutil, random
# preparing the folder structure
full_data_path = 'data/dl_target_dataset/'
extension_allowed = '.png'
split_percentage = 90
images_path = 'data/images/'
if os.path.exists(images_path):
shutil.rmtree(images_path)
os.mkdir(images_path)
labels_path = 'data/labels/'
if os.path.exists(labels_path):
shutil.rmtree(labels_path)
os.mkdir(labels_path)
training_images_path = images_path + 'training/'
validation_images_path = images_path + 'validation/'
training_labels_path = labels_path + 'training/'
validation_labels_path = labels_path +'validation/'
os.mkdir(training_images_path)
os.mkdir(validation_images_path)
os.mkdir(training_labels_path)
os.mkdir(validation_labels_path)
files = []
ext_len = len(extension_allowed)
for r, d, f in os.walk(full_data_path):
for file in f:
if file.endswith(extension_allowed):
strip = file[0:len(file) - ext_len]
files.append(strip)
random.shuffle(files)
size = len(files)
split = int(split_percentage * size / 100)
print("copying training data")
for i in range(split):
strip = files[i]
image_file = strip + extension_allowed
src_image = full_data_path + image_file
shutil.copy(src_image, training_images_path)
annotation_file = strip + '.txt'
src_label = full_data_path + annotation_file
shutil.copy(src_label, training_labels_path)
print("copying validation data")
for i in range(split, size):
strip = files[i]
image_file = strip + extension_allowed
src_image = full_data_path + image_file
shutil.copy(src_image, validation_images_path)
annotation_file = strip + '.txt'
src_label = full_data_path + annotation_file
shutil.copy(src_label, validation_labels_path)
print("finished")