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 ,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' )
38
43
39
44
seq = iaa .Sequential (prmt )
40
45
@@ -44,8 +49,8 @@ def aug_process(j,prmt,i,prjName,images,masks,counter,permutations):
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 } _ { 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 )
49
54
50
55
with counter .get_lock (): counter .value += 1
51
56
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):
69
74
70
75
while True :
71
76
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 ]]
73
78
74
- for i ,e in enumerate (chose_augmenters ):
79
+ for i ,e in enumerate (chosen_augmenters ):
75
80
#if i<1: continue
76
- p = list (itertools .permutations (chose_augmenters ,i + 1 ))
81
+ p = list (itertools .permutations (chosen_augmenters ,i + 1 ))
77
82
all_permutations .append (p )
78
83
79
84
permutations = 0
@@ -96,43 +101,48 @@ def augmentateData(prj):
96
101
{2} 3 : Gaussian Blur
97
102
{3} 4 : Average Pooling
98
103
{4} 5 : Perspective ransform
99
- {5} 6 : Piecewise Affine
104
+ {5} 6 : Piecewise Affine (Slow)
100
105
{6} 7 : Enhance Sharpness
101
106
{7} 8 : Gamma Contrast
102
107
103
108
9 : Augmentate
104
109
0 : Exit""" .format (* ('' .join (tick [opt ]) for opt in parameters )))
105
110
106
111
try :
107
- choice = input ("\n Enter your choice : " )
112
+ choice = int ( input ("\n Enter your choice : " ) )
108
113
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 } ' )
110
118
111
- # Load project
112
- if choice == '0' :
119
+ elif choice == 0 :
113
120
return
114
121
115
122
elif 1 <= int (choice ) <= 8 :
116
123
parameters [int (choice )- 1 ] = not parameters [int (choice )- 1 ]
117
124
118
- elif choice == '9' :
125
+ elif choice == 9 :
119
126
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 ' )
122
136
123
137
# Create a global variable.
124
138
counter = multiprocessing .Value ("i" , 0 , lock = True )
125
139
126
140
printProgressBar (0 , permutations * len (images ), prefix = 'Augmenting:' , suffix = f'({ counter .value } /{ permutations * len (images )} ) Complete' , length = 50 )
127
141
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 ):
133
143
processes = []
134
144
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 ))
136
146
processes .append (p )
137
147
p .start ()
138
148
0 commit comments