-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_split.py
64 lines (49 loc) · 2.13 KB
/
random_split.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
# import os, random
# import shutil
# m = 13779
# n = 4133
# x = 9647
# src_dir = "D:/Kuliah/Semester 7/Skripsi/RegionContrast/SaliencyRC-master/cell_images/Parasitized/"
# dst_dir_test = "D:/Kuliah/Semester 7/Skripsi/RegionContrast/SaliencyRC-master/image_test/PArasitized/"
# dst_dir_train = "D:/Kuliah/Semester 7/Skripsi/RegionContrast/SaliencyRC-master/image_train/Parasitized/"
# file_list = os.listdir(src_dir)
# for f in range(n):
# a = random.choice(file_list)
# file_list.remove(a)
# shutil.copy(src_dir + a, dst_dir_test + a)
# for g in range(x):
# a = random.choice(file_list)
# file_list.remove(a)
# shutil.copy(src_dir + a, dst_dir_train + a)
import os, random
import shutil
import math
source_directory = 'F:\\SKRIPSI 2019-2020\\6. Giovanni Tjahyamulia - 1620250081\\image_resized'
test_directory = 'F:\\SKRIPSI 2019-2020\\6. Giovanni Tjahyamulia - 1620250081\\image_test'
train_directory = 'F:\\SKRIPSI 2019-2020\\6. Giovanni Tjahyamulia - 1620250081\\image_train'
for folder in os.listdir(source_directory):
current_path = "".join((source_directory, "\\", folder))
file_list = os.listdir(current_path)
path_test = "".join((test_directory, "\\", folder))
path_train = "".join((train_directory, "\\", folder))
try:
os.makedirs(path_test)
print("Directory", folder, "Test Created ")
except FileExistsError:
print("Directory", folder, "Test already exists")
try:
os.makedirs(path_train)
print("Directory", folder, "Train Created ")
except FileExistsError:
print("Directory", folder, "Train already exists")
images = len(file_list)
train_images = math.floor(images * 70 / 100)
test_images = math.ceil(images * 30 / 100)
for i in range(train_images):
a = random.choice(file_list)
file_list.remove(a)
shutil.copy(current_path + "\\" + a, path_train + "\\" + a)
for i in range(test_images):
a = random.choice(file_list)
file_list.remove(a)
shutil.copy(current_path + "\\" + a, path_test + "\\" + a)