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