Skip to content

Commit 4a4b190

Browse files
Merge branch 'Development' into main
2 parents a36c062 + d69c4db commit 4a4b190

File tree

1 file changed

+72
-40
lines changed

1 file changed

+72
-40
lines changed

lib/augmentateData.py

+72-40
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,sum):
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,73 +49,100 @@ def aug_process(j,prmt,i,prjName,images,masks,counter,sum):
4449
cv2.imread(f'projects/{prjName}/masks/{masks[k]}'),
4550
seq
4651
)
47-
cv2.imwrite(f'projects/{prjName}/augmented/{i}/{j}/img/{img}', image_aug)
48-
cv2.imwrite(f'projects/{prjName}/augmented/{i}/{j}/masks/{masks[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
51-
printProgressBar(counter.value, sum*len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{sum*len(images)}) Complete', length = 50)
56+
printProgressBar(counter.value, permutations*len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{permutations*len(images)}) Complete', length = 50)
5257

5358

5459
def augmentateData(prj):
60+
61+
# Stores tick and cross symbols for the menu
62+
tick = dict([(True, f'{bcolors.OKGREEN}{bcolors.BOLD}{bcolors.ENDC}'), (False, f'{bcolors.FAIL}{bcolors.BOLD}{bcolors.ENDC}')])
63+
64+
#Global vars
5565
prjName = prj.name
5666
images = sorted(os.listdir(f'projects/{prjName}/img'))
5767
masks = sorted(os.listdir(f'projects/{prjName}/masks'))
68+
69+
#Stores the augmenters
70+
augmenters = [aug1,aug2,aug3,aug4,aug5,aug6,aug7,aug8]
71+
72+
#Stores which augmenters will be used
73+
parameters = [True for aug in augmenters]
5874

5975
while True:
76+
all_permutations = []
77+
chosen_augmenters = [aug for i,aug in enumerate(augmenters) if parameters[i]]
78+
79+
for i,e in enumerate(chosen_augmenters):
80+
#if i<1: continue
81+
p = list(itertools.permutations(chosen_augmenters,i+1))
82+
all_permutations.append(p)
83+
84+
permutations = 0
85+
for e in all_permutations:
86+
permutations += len(list(e))
87+
6088
os.system("clear")
6189
printHeader()
6290
printLoadedProject(prj)
6391

6492
print(f'{bcolors.BOLD}Nº of images:{bcolors.ENDC} {len(prj.images)}')
93+
print(f'{bcolors.BOLD}Permutations:{bcolors.ENDC} {permutations}')
6594

6695
print("""
67-
\nChoose an option:
68-
69-
┏━━━━━━━━━━━━━━━━━━━━━━━┓
70-
┃ 1 : Augmentate ┃
71-
┗━━━━━━━━━━━━━━━━━━━━━━━┛
72-
73-
0 : Exit""")
96+
\n
97+
Toggle options
98+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
99+
{0} 1 : Elastic Transformation
100+
{1} 2 : Flip vertical axis
101+
{2} 3 : Gaussian Blur
102+
{3} 4 : Average Pooling
103+
{4} 5 : Perspective ransform
104+
{5} 6 : Piecewise Affine (Slow)
105+
{6} 7 : Enhance Sharpness
106+
{7} 8 : Gamma Contrast
107+
108+
9 : Augmentate
109+
0 : Exit""".format(*(''.join(tick[opt]) for opt in parameters)))
74110

75111
try:
76-
choice = input("\nEnter your choice : ")
112+
choice = int(input("\nEnter your choice : "))
77113
except:
78-
choice = ''
114+
choice = -1
79115

80-
# Load project
81-
if choice == '0':
116+
if choice == -1:
117+
input(f'\n{bcolors.FAIL}Unexpected option, press a key to continue...{bcolors.ENDC}')
118+
119+
elif choice == 0:
82120
return
83121

84-
elif choice == '1':
122+
elif 1 <= int(choice) <= 8:
123+
parameters[int(choice)-1] = not parameters[int(choice)-1]
124+
125+
elif choice == 9:
85126

86-
if not os.path.exists(f'projects/{prjName}/augmented'):
87-
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')
88130

89-
elements = [aug1,aug2,aug4,aug5,aug6,aug7,aug8]
90-
all_permutations = []
91-
for i,e in enumerate(elements):
92-
if i<1: continue
93-
p = list(itertools.permutations(elements,i+1))
94-
all_permutations.append(p)
131+
aug_key = {k.__class__.__name__: v for v, k in enumerate(chosen_augmenters)}
95132

96-
sum = 0
97-
for e in all_permutations:
98-
sum += len(list(e))
99-
print(sum)
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')
100136

101137
# Create a global variable.
102138
counter = multiprocessing.Value("i", 0, lock=True)
103139

104-
printProgressBar(0, sum * len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{sum*len(images)}) Complete', length = 50)
105-
106-
for i,prmt_list in enumerate(all_permutations):
107-
if not os.path.exists(f'projects/{prjName}/augmented/{i}'):
108-
os.makedirs(f'projects/{prjName}/augmented/{i}')
140+
printProgressBar(0, permutations * len(images), prefix = 'Augmenting:', suffix = f'({counter.value}/{permutations*len(images)}) Complete', length = 50)
109141

110-
142+
for i,prmt_list in enumerate(all_permutations):
111143
processes = []
112144
for j,prmt in enumerate(prmt_list):
113-
p = multiprocessing.Process(target=aug_process, args=(j,prmt,i,prjName,images,masks,counter,sum))
145+
p = multiprocessing.Process(target=aug_process, args=(j,prmt,i,prjName,images,masks,counter,permutations,aug_key))
114146
processes.append(p)
115147
p.start()
116148

0 commit comments

Comments
 (0)