-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmelNET_augData.py
More file actions
352 lines (292 loc) · 12.4 KB
/
melNET_augData.py
File metadata and controls
352 lines (292 loc) · 12.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#######################################################################################################################
import cv2
import numpy as np
import os.path
import os
from tkinter import *
import shutil
import melNET_blur_detection
# Main >>>
def main():
# Check-Box creation
root = Tk()
root.title("Data Augmentation") # Title
global pressed
pressed = False
def apply_button_pressed():
root.destroy()
global pressed
pressed = True
def quit_button_pressed():
root.destroy()
global pressed
pressed = False
# Check Box variables
_del_blur = BooleanVar()
_original = BooleanVar()
_histEqualization = BooleanVar()
_dilation = BooleanVar()
_erosion = BooleanVar()
_blur = BooleanVar()
_sharpen = BooleanVar()
_mirror = BooleanVar()
_rotate = BooleanVar()
_five_fold = BooleanVar()
_resize = BooleanVar()
_rotate_ang = IntVar(0)
_blur_thresh = IntVar(0)
# UI Creation
Checkbutton(root, text="Detect & Delete Blurry Images", variable=_del_blur).grid(row=0, sticky=W)
Checkbutton(root, text="Original", variable=_original).grid(row=1, sticky=W)
Checkbutton(root, text="Hist. Equalization", variable=_histEqualization).grid(row=2, sticky=W)
Checkbutton(root, text="Dilation", variable=_dilation).grid(row=3, sticky=W)
Checkbutton(root, text="Erosion", variable=_erosion).grid(row=4, sticky=W)
Checkbutton(root, text="Median Filter", variable=_blur).grid(row=5, sticky=W)
Checkbutton(root, text="Sharpen", variable=_sharpen).grid(row=6, sticky=W)
Checkbutton(root, text="Mirror", variable=_mirror).grid(row=7, sticky=W)
Checkbutton(root, text="Rotate All", variable=_rotate).grid(row=8, sticky=W)
Checkbutton(root, text="Resize (400 x 400)", variable=_resize).grid(row=10, sticky=W)
Label(root, text="Angle Span in Degrees: ").grid(row=8, column=2)
Label(root, text="(Default: 45)").grid(row=9, column=2)
Entry(root, textvariable=_rotate_ang).grid(row=8, column=3)
Label(root, text="Blur Threshold (Default=7):").grid(row=0, column=2)
Entry(root, textvariable=_blur_thresh).grid(row=0, column=3)
Checkbutton(root, text="Five-Fold (Default: Single-Fold)", variable=_five_fold).grid(row=11, sticky=W)
Button(root, text="Quit", command=quit_button_pressed, width=15).grid(row=12, column=2, sticky=W)
Button(root, text="Apply", command=apply_button_pressed, width=15).grid(row=12, column=3, sticky=W)
root.mainloop()
# Checking [Start] status
if pressed:
del_blur = _del_blur.get()
original = _original.get()
histEqualization = _histEqualization.get()
dilation = _dilation.get()
erosion = _erosion.get()
blur = _blur.get()
sharpen = _sharpen.get()
mirror = _mirror.get()
rotate = _rotate.get()
resize = _resize.get()
global five_fold
five_fold = _five_fold.get()
rot_ang = _rotate_ang.get()
blur_thresh = _blur_thresh.get()
if (original is False and histEqualization is False and dilation is False and
erosion is False and blur is False and sharpen is False and mirror is False):
print("Nothing is Selected!")
exit()
else:
print("Augmentation Process has not been Started!")
exit()
# Checking for a valid rotation angle
global rotationAngle
if rotate:
if (rot_ang > 0) & (rot_ang < 360):
rotationAngle = rot_ang
else:
rotationAngle = 45 # Default: 45
else:
rotationAngle = None
# Detect and Delete blurry images
if del_blur:
if blur_thresh is 0:
blur_thresh = 7
melNET_blur_detection.main(blur_thresh)
# Number of folds
global fold_num
fold_num = 5 # For 5Folds
# Getting Sub-Folder names in [Data] folder
global folder_list
folder_list = []
if len(os.listdir("Data")) == 0:
print("[Data] folder is empty!\n")
exit()
else:
for entry_name in os.listdir("Data"):
if entry_name.find("aug") < 0: # Skipping old folders with Augmented data
entry_path = os.path.join("Data", entry_name)
if os.path.isdir(entry_path):
folder_list.append(entry_name)
else:
continue
if len(folder_list) == 0: # All sub-folders contain augmented data
exit()
# Generate folders for augmented images
generate_aug_folders(folder_list)
# Read, Process and Distribute images from Sub-Folders
global name_counter
name_counter = 0
for folder in folder_list:
folderPath = "Data/" + folder
image_list = load_images_from_folder(folderPath)
set_counter = 0
for img in image_list:
set_counter += 1
# Resizing
if resize:
width = 400
height = 400
img = cv2.resize(img, (width, height))
# Original Saving Code
if original:
file_naming(img, "O", folder, set_counter)
# Histogram Equalization Code
if histEqualization:
ycbImage = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y_channel, Cr, Cb = cv2.split(ycbImage)
Y_channel = cv2.equalizeHist(Y_channel)
ycbImage = cv2.merge([Y_channel, Cr, Cb])
imgEqualized = cv2.cvtColor(ycbImage, cv2.COLOR_YCrCb2BGR)
file_naming(imgEqualized, "HE", folder, set_counter)
# Dilation Code
if dilation:
dilationSize = 1
dilationElement = cv2.getStructuringElement(cv2.MORPH_CROSS, (2 * dilationSize + 1, 2 * dilationSize + 1),
(dilationSize, dilationSize))
imgDilated = cv2.dilate(img, dilationElement)
file_naming(imgDilated, "D", folder, set_counter)
# Erosion Code
if erosion:
erosionSize = 1
erosionElement = cv2.getStructuringElement(cv2.MORPH_CROSS, (2 * erosionSize + 1, 2 * erosionSize + 1),
(erosionSize, erosionSize))
imgEroded = cv2.erode(img, erosionElement)
file_naming(imgEroded, "E", folder, set_counter)
# Blur Code
if blur:
kernelSize = 3
imgBlur = cv2.medianBlur(img, kernelSize)
file_naming(imgBlur, "B", folder, set_counter)
# Sharpen Code
if sharpen:
sharpenElement = np.array((
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]), dtype="int")
imgSharp = cv2.filter2D(img, -1, sharpenElement)
file_naming(imgSharp, "S", folder, set_counter)
# Mirror Code
if mirror:
imgMirror = cv2.flip(img, 1)
file_naming(imgMirror, "M", folder, set_counter)
if set_counter is fold_num:
set_counter = 0
# Make five-fold ready
if five_fold:
get_fold_ready()
#######################################################################################################################
# Functions >>>
def show_image(_window_name, _image):
cv2.namedWindow(_window_name, cv2.WINDOW_AUTOSIZE)
cv2.imshow(_window_name, _image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def load_images_from_folder(_folder_name):
_image_list = []
for _file_name in os.listdir(_folder_name):
_img = cv2.imread(os.path.join(_folder_name, _file_name), cv2.IMREAD_COLOR)
if _img is not None:
_image_list.append(_img)
else:
print("Could not open or find any image in [" + _folder_name + "] folder!\n")
return _image_list
def generate_aug_folders(_folder_list):
if five_fold:
path_root = "aug_Data/Five_Fold_(Aug)"
if os.path.exists(path_root):
shutil.rmtree(path_root) # Removing old dir.
os.makedirs(path_root)
else:
os.makedirs(path_root)
for x in range(fold_num):
fold_path = path_root + "/Fold_" + str(x+1)
os.makedirs(fold_path)
set_path = path_root + "/Temp/Set_" + str(x+1)
os.makedirs(set_path)
# Set Folder
for _folder in _folder_list:
path_temp = set_path + "/" + _folder
os.makedirs(path_temp)
# Train Folder
for _folder in _folder_list:
path_temp = fold_path + "/Train/" + _folder
os.makedirs(path_temp)
# Test Folder
for _folder in _folder_list:
path_temp = fold_path + "/Test/" + _folder
os.makedirs(path_temp)
else:
path = "aug_Data/Single_Fold_(Aug)/Train"
if os.path.exists(path):
shutil.rmtree(path) # Removing old dir.
os.makedirs(path)
for _folder in _folder_list:
path_temp = path + "/" + _folder
os.makedirs(path_temp)
def copy_all(_src, _dst):
src_files = os.listdir(_src)
for file_name in src_files:
full_file_name = os.path.join(_src, file_name)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, _dst)
def file_naming(_img, process_initial, _folder, _set_counter):
if five_fold:
dst_root = "aug_Data/Five_Fold_(Aug)/Temp/Set_" + str(_set_counter) + "/"
else:
dst_root = "aug_Data/Single_Fold_(Aug)/Train/"
global name_counter
name_counter += 1
if rotationAngle is not None:
# Rotation Code
for angle in range(0, 360, rotationAngle):
# Correct-Rotation Code
_scaleFactor = 1
(h, w) = _img.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), angle, _scaleFactor)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
_imgRotated = cv2.warpAffine(_img, M, (nW, nH), flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_REFLECT_101)
"""
# Non-Correct Rotation Code:
dim = _img.shape
_scaleFactor = 1
rotationMatrix = cv2.getRotationMatrix2D((dim[1] / 2, dim[0] / 2), angle, _scaleFactor)
_imgRotated = cv2.warpAffine(_img, rotationMatrix, (dim[1], dim[0]), flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_REFLECT_101) """
_savingName = dst_root + _folder + "/" + _folder + "_" + str(name_counter) + "_" \
+ process_initial + "_Rot_" + str(angle) + ".jpg"
cv2.imwrite(_savingName, _imgRotated)
else:
_savingName = dst_root + _folder + "/" + _folder + "_" + str(name_counter) + "_" + process_initial + ".jpg"
cv2.imwrite(_savingName, _img)
def get_fold_ready():
for fold_scroll in range(fold_num):
fold_root = "aug_Data/Five_Fold_(Aug)/Fold_" + str(fold_scroll+1)
for set_scroll in range(fold_num):
set_root = "aug_Data/Five_Fold_(Aug)/Temp/Set_" + str(set_scroll+1)
if set_scroll is fold_scroll:
for _folder in folder_list:
_src = set_root + "/" + _folder
_dst = fold_root + "/Test/" + _folder
copy_all(_src, _dst)
else:
for _folder in folder_list:
_src = set_root + "/" + _folder
_dst = fold_root + "/Train/" + _folder
copy_all(_src, _dst)
shutil.rmtree("aug_Data/Five_Fold_(Aug)/Temp") # Removing [Temp] Folder
#######################################################################################################################
# Main Call Func. >>>
if __name__ == "__main__":
main()