Skip to content

Commit d69c4db

Browse files
New augmented file name convention
1 parent e85293e commit d69c4db

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

lib/augmentateData.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, cv2, itertools, multiprocessing, random
1+
import os, cv2, itertools, multiprocessing, random, shutil
22
import imgaug as ia
33
import imgaug.augmenters as iaa
44
from functools import partial
@@ -30,11 +30,16 @@ def augment_seg(img,seg,seq):
3030

3131
return image_aug , segmap_aug
3232

33-
def aug_process(j,prmt,i,prjName,images,masks,counter,permutations):
34-
if not os.path.exists(f'projects/{prjName}/augmented/{i}/{j}/img'):
35-
os.makedirs(f'projects/{prjName}/augmented/{i}/{j}/img')
36-
if not os.path.exists(f'projects/{prjName}/augmented/{i}/{j}/masks'):
37-
os.makedirs(f'projects/{prjName}/augmented/{i}/{j}/masks')
33+
def aug_process(j,prmt,i,prjName,images,masks,counter,permutations,aug_key):
34+
name = ''
35+
for aug in prmt:
36+
name += str(aug_key[aug.__class__.__name__])
37+
38+
if not os.path.exists(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/img'):
39+
os.makedirs(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/img')
40+
41+
if not os.path.exists(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/masks'):
42+
os.makedirs(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/masks')
3843

3944
seq = iaa.Sequential(prmt)
4045

@@ -44,8 +49,8 @@ def aug_process(j,prmt,i,prjName,images,masks,counter,permutations):
4449
cv2.imread(f'projects/{prjName}/masks/{masks[k]}'),
4550
seq
4651
)
47-
cv2.imwrite(f'projects/{prjName}/augmented/{i}/{j}/img/{img}_{i}-{j}-{k}', image_aug)
48-
cv2.imwrite(f'projects/{prjName}/augmented/{i}/{j}/masks/{masks[k]}_{i}-{j}-{k}', segmap_aug)
52+
cv2.imwrite(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/img/{img.strip(".jpg")}-{i+1}_prmt-{name}-{k}.bmp', image_aug)
53+
cv2.imwrite(f'projects/{prjName}/augmented/{i+1}_prmt/{name}/masks/{masks[k].strip(".jpg")}-{i+1}_prmt-{name}-{k}.bmp', segmap_aug)
4954

5055
with counter.get_lock(): counter.value += 1
5156
printProgressBar(counter.value, permutations*len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{permutations*len(images)}) Complete', length = 50)
@@ -69,11 +74,11 @@ def augmentateData(prj):
6974

7075
while True:
7176
all_permutations = []
72-
chose_augmenters = [aug for i,aug in enumerate(augmenters) if parameters[i]]
77+
chosen_augmenters = [aug for i,aug in enumerate(augmenters) if parameters[i]]
7378

74-
for i,e in enumerate(chose_augmenters):
79+
for i,e in enumerate(chosen_augmenters):
7580
#if i<1: continue
76-
p = list(itertools.permutations(chose_augmenters,i+1))
81+
p = list(itertools.permutations(chosen_augmenters,i+1))
7782
all_permutations.append(p)
7883

7984
permutations = 0
@@ -96,43 +101,48 @@ def augmentateData(prj):
96101
{2} 3 : Gaussian Blur
97102
{3} 4 : Average Pooling
98103
{4} 5 : Perspective ransform
99-
{5} 6 : Piecewise Affine
104+
{5} 6 : Piecewise Affine (Slow)
100105
{6} 7 : Enhance Sharpness
101106
{7} 8 : Gamma Contrast
102107
103108
9 : Augmentate
104109
0 : Exit""".format(*(''.join(tick[opt]) for opt in parameters)))
105110

106111
try:
107-
choice = input("\nEnter your choice : ")
112+
choice = int(input("\nEnter your choice : "))
108113
except:
109-
choice = ''
114+
choice = -1
115+
116+
if choice == -1:
117+
input(f'\n{bcolors.FAIL}Unexpected option, press a key to continue...{bcolors.ENDC}')
110118

111-
# Load project
112-
if choice == '0':
119+
elif choice == 0:
113120
return
114121

115122
elif 1 <= int(choice) <= 8:
116123
parameters[int(choice)-1] = not parameters[int(choice)-1]
117124

118-
elif choice == '9':
125+
elif choice == 9:
119126

120-
if not os.path.exists(f'projects/{prjName}/augmented'):
121-
os.makedirs(f'projects/{prjName}/augmented')
127+
if os.path.exists(f'projects/{prjName}/augmented'):
128+
shutil.rmtree(f'projects/{prjName}/augmented')
129+
os.makedirs(f'projects/{prjName}/augmented')
130+
131+
aug_key = {k.__class__.__name__: v for v, k in enumerate(chosen_augmenters)}
132+
133+
with open(f'projects/{prjName}/augmented/augmenters.txt', 'w') as aug_file:
134+
for aug, i in aug_key.items():
135+
aug_file.write(f'({i}) : {aug}\n')
122136

123137
# Create a global variable.
124138
counter = multiprocessing.Value("i", 0, lock=True)
125139

126140
printProgressBar(0, permutations * len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{permutations*len(images)}) Complete', length = 50)
127141

128-
for i,prmt_list in enumerate(all_permutations):
129-
if not os.path.exists(f'projects/{prjName}/augmented/{i}'):
130-
os.makedirs(f'projects/{prjName}/augmented/{i}')
131-
132-
142+
for i,prmt_list in enumerate(all_permutations):
133143
processes = []
134144
for j,prmt in enumerate(prmt_list):
135-
p = multiprocessing.Process(target=aug_process, args=(j,prmt,i,prjName,images,masks,counter,permutations))
145+
p = multiprocessing.Process(target=aug_process, args=(j,prmt,i,prjName,images,masks,counter,permutations,aug_key))
136146
processes.append(p)
137147
p.start()
138148

0 commit comments

Comments
 (0)