1
- import os , cv2 , itertools , multiprocessing , random
1
+ import os , cv2 , itertools , multiprocessing , random , shutil
2
2
import imgaug as ia
3
3
import imgaug .augmenters as iaa
4
4
from functools import partial
@@ -30,11 +30,16 @@ def augment_seg(img,seg,seq):
30
30
31
31
return image_aug , segmap_aug
32
32
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' )
38
43
39
44
seq = iaa .Sequential (prmt )
40
45
@@ -44,73 +49,100 @@ def aug_process(j,prmt,i,prjName,images,masks,counter,sum):
44
49
cv2 .imread (f'projects/{ prjName } /masks/{ masks [k ]} ' ),
45
50
seq
46
51
)
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 )
49
54
50
55
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 )
52
57
53
58
54
59
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
55
65
prjName = prj .name
56
66
images = sorted (os .listdir (f'projects/{ prjName } /img' ))
57
67
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 ]
58
74
59
75
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
+
60
88
os .system ("clear" )
61
89
printHeader ()
62
90
printLoadedProject (prj )
63
91
64
92
print (f'{ bcolors .BOLD } Nº of images:{ bcolors .ENDC } { len (prj .images )} ' )
93
+ print (f'{ bcolors .BOLD } Permutations:{ bcolors .ENDC } { permutations } ' )
65
94
66
95
print ("""
67
- \n Choose 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 )))
74
110
75
111
try :
76
- choice = input ("\n Enter your choice : " )
112
+ choice = int ( input ("\n Enter your choice : " ) )
77
113
except :
78
- choice = ''
114
+ choice = - 1
79
115
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 :
82
120
return
83
121
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 :
85
126
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' )
88
130
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 )}
95
132
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 ' )
100
136
101
137
# Create a global variable.
102
138
counter = multiprocessing .Value ("i" , 0 , lock = True )
103
139
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 )
109
141
110
-
142
+ for i , prmt_list in enumerate ( all_permutations ):
111
143
processes = []
112
144
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 ))
114
146
processes .append (p )
115
147
p .start ()
116
148
0 commit comments