forked from holger-jodel/sc2_predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-files-for-learning.py
86 lines (65 loc) · 2.36 KB
/
prepare-files-for-learning.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
# copy files from data/img/
# according to csv file
# to data/train/<class> and data/test/<class>
import sys
import os
import numpy as np
import pandas as pd
import shutil
import random
from VideoHelper.HelperFunctions import get_files_in_dir, path_is_dir
def setup_folders(class_names, path='data/', noTestnoValid=False):
train_path = path + 'train/'
test_path = path + 'test/'
if noTestnoValid:
train_path, test_path = path, path
for tmp_path in [train_path, test_path]:
for class_name in class_names:
if not os.path.exists(tmp_path + class_name):
os.makedirs(tmp_path + class_name)
def copy_img(source, class_name, dest_path='data/', test_ratio=0.7, noTestnoValid=False, dry=False, verbose=False):
dest = ""
rand = random.random()
if noTestnoValid:
dest = dest_path + class_name
else:
if rand > test_ratio:
dest = dest_path + 'test/' + class_name
else:
dest = dest_path + 'train/' + class_name
if verbose:
print("copy from {} to {}".format(source, dest))
if not(dry):
shutil.copy(source, dest)
def main(args):
if(len(args) < 4):
print(
'define paths to image input folder, destination folder and .csv file \n\
and 0|1 if no test/valid folders should be created [default=0]')
return -1
img_path = args[1]
dest_path = args[2]
filename = args[3]
noTestnoValid = 0
if(len(args) == 5):
noTestnoValid = bool(args[4])
if (not(path_is_dir(img_path)) or not(path_is_dir(dest_path))):
print('given folders not available to load or save data')
return -1
class_names = ['ingame', 'misc']
print('filename {}\nfolder {}'.format(filename, img_path))
is_file = os.path.isfile(filename)
if(not is_file):
print('given csv file not available')
return -1
setup_folders(class_names=class_names, noTestnoValid=noTestnoValid)
df = pd.read_csv(filename, names=['filename', 'class_name'], dtype={
'class_name': np.int8}, header=1)
print('start copying {} files'.format(df.shape[0]))
for t in df.itertuples():
copy_img(img_path + t.filename + '.png',
class_names[t.class_name], dest_path,
noTestnoValid=noTestnoValid)
print('done')
if __name__ == "__main__":
main(sys.argv)