-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile_manager.py
More file actions
86 lines (63 loc) · 2.35 KB
/
file_manager.py
File metadata and controls
86 lines (63 loc) · 2.35 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
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
#!/usr/bin/python
import os, random, shutil, glob
from box import Box
class FileManager:
def __init__(self, data_dir="./images/", output_dir="./output/"):
self.data_dir = data_dir
self.train_dir = self.data_dir + "train/"
self.test_dir = self.data_dir + "test/"
self.output_dir = output_dir
self.pickle_file = self.output_dir + "trained.pkl"
self.confusion_matrix_file = self.output_dir + "conf.png"
self.class_labels = Box((0,0)).getBoxLabels()
def setup(self):
if (self.__checkDir()):
print "\nWARNING: Directory named:\t" + self.data_dir + "\talready exists!"
self.__askAlternativeDir()
if (self.data_dir == 'n'):
print "Program terminating due to not providing alternative data directory\n"
exit()
else:
print "Directory named:\t"+ self.data_dir +"\tcreated\n"
os.makedirs(self.data_dir)
def getDir(self):
return self.data_dir
def getFileName(self, sample_count, frame_number, class_label):
s = str(sample_count) + "_" + str(frame_number) + "_" + class_label + ".png"
return s
def getFilePath(self, sample_count, frame_number, class_label):
fname = self.getFileName(sample_count, frame_number, class_label)
return self.data_dir + fname
def getPickleFile(self):
return self.pickle_file
def getConfusionMatrixFile(self):
return self.confusion_matrix_file
def getClassLabels(self):
return self.class_labels
def __checkDir(self):
already_exists = False
if (os.path.exists(self.data_dir)):
already_exists = True
return already_exists
def __askAlternativeDir(self):
prompt = "Please provide an alternative directory name to save data files.\n" + \
"Or enter 'n' to quit the program: "
self.data_dir = raw_input(prompt)
def splitTrainingAndTest(self, ratio=0.8):
for l in self.class_labels:
files = glob.glob(self.data_dir+"*_"+l+".png")
random.shuffle(files)
train_sz = round(len(files)*0.8)
train_dir = self.train_dir + l
test_dir = self.test_dir + l
os.makedirs(train_dir)
os.makedirs(test_dir)
count = 0
for f in files:
dirname = train_dir if (count < train_sz) else test_dir
print "count: " + str(count) + "\t" + f + "\t=>\t" + dirname
shutil.move(f, dirname)
count += 1
print "For class: " + l + "\tmoved " + str(train_sz) + " files into " + \
self.train_dir + "\nand " + str(len(files)-train_sz) + \
" files into " + self.test_dir