-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
108 lines (93 loc) · 5.64 KB
/
preprocessing.py
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
import os
import cv2
import imgaug as ia
from imgaug import augmenters as iaa
def preprocess_images(input_folder, output_folder, augmentation):
augmenter = augmentation
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.lower().endswith((".jpg", ".jpeg")):
image_path = os.path.join(input_folder, filename)
augmented_image_path = os.path.join(output_folder, filename)
if not os.path.exists(augmented_image_path):
image = cv2.imread(image_path)
augmented_image = augmenter.augment_image(image)
cv2.imwrite(augmented_image_path, augmented_image)
augmentations = [
iaa.Grayscale(),
iaa.Affine(rotate=(-45, 45), mode='reflect', order=3, cval=0),
iaa.GammaContrast(gamma=(0.5, 1.5)),
iaa.Fliplr(1.0),
iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255), per_channel=True),
iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, mode='reflect', order=3, cval=0)
]
folders = [
"dataset/indoor/1",
"dataset/indoor/2",
"dataset/indoor/3",
"dataset/indoor/4",
"dataset/indoor/5",
"dataset/indoor/5+",
"dataset/indoor/6",
"dataset/outdoor/1",
"dataset/outdoor/2",
"dataset/outdoor/3",
"dataset/outdoor/4",
"dataset/outdoor/5",
"dataset/outdoor/5+",
"dataset/outdoor/6"
]
for folder in folders:
input_folder = os.path.join(folder, "original", "original_only")
grayscale_output_folder = os.path.join(folder, "grayscale", "grayscale_only")
rotation_output_folder = os.path.join(folder, "original", "rotated")
contrast_output_folder = os.path.join(folder, "original", "contrast")
flipped_output_folder = os.path.join(folder, "original", "flipped")
noisy_output_folder = os.path.join(folder, "original", "noisy")
scaled_output_folder = os.path.join(folder, "original", "scaled")
preprocess_images(input_folder, grayscale_output_folder, augmentations[0])
preprocess_images(input_folder, rotation_output_folder, augmentations[1])
preprocess_images(input_folder, contrast_output_folder, augmentations[2])
preprocess_images(input_folder, flipped_output_folder, augmentations[3])
preprocess_images(input_folder, noisy_output_folder, augmentations[4])
preprocess_images(input_folder, scaled_output_folder, augmentations[5])
contrast_flipped_output_folder = os.path.join(folder, "original", "contrast_flipped")
contrast_rotation_output_folder = os.path.join(folder, "original", "contrast_rotated")
noisy_flipped_output_folder = os.path.join(folder, "original", "noisy_flipped")
noisy_rotation_output_folder = os.path.join(folder, "original", "noisy_rotated")
rotation_flipped_output_folder = os.path.join(folder, "original", "rotated_flipped")
scaled_flipped_output_folder = os.path.join(folder, "original", "scaled_flipped")
scaled_rotation_output_folder = os.path.join(folder, "original", "scaled_rotated")
preprocess_images(contrast_output_folder, contrast_flipped_output_folder, augmentations[3])
preprocess_images(contrast_output_folder, contrast_rotation_output_folder, augmentations[1])
preprocess_images(noisy_output_folder, noisy_flipped_output_folder, augmentations[3])
preprocess_images(noisy_output_folder, noisy_rotation_output_folder, augmentations[1])
preprocess_images(rotation_output_folder, rotation_flipped_output_folder, augmentations[3])
preprocess_images(scaled_output_folder, scaled_flipped_output_folder, augmentations[3])
preprocess_images(scaled_output_folder, scaled_rotation_output_folder, augmentations[1])
input_folder = os.path.join(folder, "grayscale", "grayscale_only")
rotation_output_folder = os.path.join(folder, "grayscale", "rotated")
contrast_output_folder = os.path.join(folder, "grayscale", "contrast")
flipped_output_folder = os.path.join(folder, "grayscale", "flipped")
noisy_output_folder = os.path.join(folder, "grayscale", "noisy")
scaled_output_folder = os.path.join(folder, "grayscale", "scaled")
preprocess_images(input_folder, rotation_output_folder, augmentations[1])
preprocess_images(input_folder, contrast_output_folder, augmentations[2])
preprocess_images(input_folder, flipped_output_folder, augmentations[3])
preprocess_images(input_folder, noisy_output_folder, augmentations[4])
preprocess_images(input_folder, scaled_output_folder, augmentations[5])
contrast_flipped_output_folder = os.path.join(folder, "grayscale", "contrast_flipped")
contrast_rotation_output_folder = os.path.join(folder, "grayscale", "contrast_rotated")
noisy_flipped_output_folder = os.path.join(folder, "grayscale", "noisy_flipped")
noisy_rotation_output_folder = os.path.join(folder, "grayscale", "noisy_rotated")
rotation_flipped_output_folder = os.path.join(folder, "grayscale", "rotated_flipped")
scaled_flipped_output_folder = os.path.join(folder, "grayscale", "scaled_flipped")
scaled_rotation_output_folder = os.path.join(folder, "grayscale", "scaled_rotated")
preprocess_images(contrast_output_folder, contrast_flipped_output_folder, augmentations[3])
preprocess_images(contrast_output_folder, contrast_rotation_output_folder, augmentations[1])
preprocess_images(noisy_output_folder, noisy_flipped_output_folder, augmentations[3])
preprocess_images(noisy_output_folder, noisy_rotation_output_folder, augmentations[1])
preprocess_images(rotation_output_folder, rotation_flipped_output_folder, augmentations[3])
preprocess_images(scaled_output_folder, scaled_flipped_output_folder, augmentations[3])
preprocess_images(scaled_output_folder, scaled_rotation_output_folder, augmentations[1])