This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveData.py
More file actions
126 lines (105 loc) · 3.38 KB
/
saveData.py
File metadata and controls
126 lines (105 loc) · 3.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import shutil
import csv
import numpy as np
import pandas as pd
from tqdm import tqdm
import cv2
def initializeData(DataStoreName):
tmp = os.getcwd()
if DataStoreName in os.listdir():
shutil.rmtree(DataStoreName)
os.mkdir(DataStoreName)
os.chdir(DataStoreName)
os.mkdir('images')
os.chdir('images')
os.mkdir('train')
os.mkdir('val')
os.mkdir('test')
os.chdir('..')
os.mkdir('labels')
os.chdir('labels')
os.mkdir('train')
os.mkdir('val')
os.mkdir('test')
os.chdir(tmp)
def makePoly(relMask):
tmp = cv2.imread(relMask)
mh, mw, _ = tmp.shape
tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
contours, _ = cv2.findContours(tmp, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
tmp = np.array(contours[0]/[mw, mh])
poly = tmp.reshape(len(contours[0])*2)
return poly
def saveData(target, relImg):
targetDirImg = DataStoreDir + '/images/' + target
targetDirLab = DataStoreDir + '/labels/' + target
imgName = relImg[-13:]
# Make Target Data: IMG
shutil.copy(relImg, targetDirImg + '/' + imgName)
# Make Target Data: Poly (GT)
gt = relImg.replace('.png', '.gt_data.txt')
gt = pd.read_csv(gt, sep=' ', header=None)
with open(targetDirLab + '/' + imgName.replace('.png', '.txt'), 'a', encoding='utf-8') as f:
wr = csv.writer(f, delimiter=' ')
for i in range(len(gt)):
mask = gt.iloc[i, 6]
relMask = relImg.replace('.png', '.' + mask + '.png')
poly = makePoly(relMask)
wr.writerow([0, *poly])
if __name__ == "__main__":
DataStoreName = "LogoRec"
os.chdir('..')
initializeData(DataStoreName)
datasets = os.getcwd()
DataStoreDir = datasets + '/' + DataStoreName
candidate = []
tmp = "train/filelist-logosonly"
file = open("FlickrLogos_47/" + tmp + ".txt", "r")
while True:
line = file.readline()
if not line:
break
candidate.append('./FlickrLogos_47/train' + line.strip()[1:])
tmp = "test/filelist"
file = open("FlickrLogos_47/" + tmp + ".txt", "r")
while True:
line = file.readline()
if not line:
break
candidate.append('./FlickrLogos_47/test' + line.strip()[1:])
cnt = 0
for c in tqdm(candidate):
if cnt % 12 == 0:
saveData('val', c)
elif cnt % 12 == 1:
saveData('test', c)
else:
saveData('train', c)
cnt += 1
print('=' * 30)
print('No. Total Data: ', cnt)
print('=' * 30)
os.chdir(DataStoreDir + '/images/train')
tin = len(os.listdir())
print('Training Data: No. Images', tin)
os.chdir(DataStoreDir + '/labels/train')
ttn = len(os.listdir())
print('Training Data: No. GT', ttn)
os.chdir(DataStoreDir + '/images/val')
vin = len(os.listdir())
print('Validation Data: No. Images', vin)
os.chdir(DataStoreDir + '/labels/val')
vtn = len(os.listdir())
print('Validation Data: No. GT', vtn)
os.chdir(DataStoreDir + '/images/test')
ttin = len(os.listdir())
print('Test Data: No. Images', ttin)
os.chdir(DataStoreDir + '/labels/test')
tttn = len(os.listdir())
print('Test Data: No. GT', tttn)
print('=' * 30)
print('No. Total Image Data: ', tin + vin + ttin)
print('No. Total GT Data: ', ttn + vtn + tttn)
print('=' * 30)
os.chdir(datasets + '/MakeData')