-
Notifications
You must be signed in to change notification settings - Fork 667
/
generate_data_for_SVM.py
64 lines (45 loc) · 1.48 KB
/
generate_data_for_SVM.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
#!/usr/bin/env python
import glob
import random
import uuid
import shutil
import os
target_dist = "color-80-20"
def processLine(_line):
return (_line.split("\t")[0], _line.split("\t")[-1].strip())
_train = open("lmdb/"+target_dist+"/train.txt", "r")
TRAIN = []
for _line in _train.readlines():
TRAIN.append( processLine(_line))
_test = open("lmdb/"+target_dist+"/test.txt", "r")
TEST = []
for _line in _test.readlines():
TEST.append( processLine(_line))
random.shuffle(TRAIN)
random.shuffle(TEST)
TRAIN_MAPPINGS = open("SVM/train_mapping.txt", "w")
percent_of_train = 0.2
for _entry in TRAIN[:int(percent_of_train*len(TRAIN))]:
try:
os.mkdir("SVM/train/"+_entry[-1]) #Try to create the label directory
except:
pass
print "TRAIN :: Copying....", _entry
oldName = _entry[0].replace("/home/mohanty/data/final_dataset/", "")
newName = "SVM/train/"+_entry[-1]+"/"+str(uuid.uuid4()) + ".JPG"
shutil.copy(oldName, newName)
TRAIN_MAPPINGS.write(oldName+"\t"+newName + "\n")
TRAIN_MAPPINGS.close()
TEST_MAPPINGS = open("SVM/test_mapping.txt", "w")
percent_of_test = 1
for _entry in TEST[:int(percent_of_test*len(TEST))]:
try:
os.mkdir("SVM/test/"+_entry[-1]) #Try to create the label directory
except:
pass
print "TEST :: Copying....", _entry
oldName = _entry[0].replace("/home/mohanty/data/final_dataset/", "")
newName = "SVM/test/"+_entry[-1]+"/"+str(uuid.uuid4()) + ".JPG"
shutil.copy(oldName, newName)
TEST_MAPPINGS.write(oldName+"\t"+newName + "\n")
TEST_MAPPINGS.close()