-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataAugmentationScript.py
More file actions
175 lines (157 loc) · 8.27 KB
/
Copy pathdataAugmentationScript.py
File metadata and controls
175 lines (157 loc) · 8.27 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
#library imports
from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
import cv2
import os
import constants
import numpy as np
#set directory
os.chdir(constants.OUTPUT_DIRECTORY)
#creates NUM_WIDTH_SHIFTS amount of pictures that are edited versions of the given image; applies the width shift edit
#returns a string with the name of the first generated image
def widthShift(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'WidthShift'
for i in range(constants.NUM_WIDTH_SHIFTS):
datagen = ImageDataGenerator(width_shift_range = constants.WIDTH_SHIFT_RANGE)
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_HEIGHT_SHIFTS amount of pictures that are edited versions of the given image; applies the height shift edit
#returns a string with the name of the first generated image
def heightShift(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'HeightShift'
for i in range(constants.NUM_HEIGHT_SHIFTS):
datagen = ImageDataGenerator(height_shift_range = constants.HEIGHT_SHIFT_RANGE)
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_ROTATIONS amount of pictures that are edited versions of the given image; applies the rotation edit
#returns a string with the name of the first generated image
def rotation(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'Rotation'
for i in range(constants.NUM_ROTATIONS):
datagen = ImageDataGenerator(rotation_range = constants.ROTATION_RANGE)
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_BRIGHTNESS amount of pictures that are edited versions of the given image; applies the brightness edit
#returns a string with the name of the first generated image
def brightness(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'Brightness'
for i in range(constants.NUM_BRIGHTNESS):
datagen = ImageDataGenerator(brightness_range=[constants.BRIGHTNESS_MIN,constants.BRIGHTNESS_MAX])
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_ZOOMS amount of pictures that are edited versions of the given image; applies the zoom edit
#returns a string with the name of the first generated image
def zoom(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'Zoom'
for i in range(constants.NUM_ZOOMS):
datagen = ImageDataGenerator(zoom_range=[constants.ZOOM_MIN,constants.ZOOM_MAX])
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_V_FLIPS amount of pictures that are edited versions of the given image; applies the vertical flip edit
#returns a string with the name of the first generated image
def vFlip(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'VFlip'
for i in range(constants.NUM_V_FLIPS):
datagen = ImageDataGenerator(vertical_flip=True)
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#creates NUM_H_FLIPS amount of pictures that are edited versions of the given image; applies the horizontal flip edit
#returns a string with the name of the first generated image
def hFlip(imageLocation, imageName):
img = load_img(imageLocation)
data = img_to_array(img)
samples = expand_dims(data, 0)
baseName = imageName[:len(imageName)-4] + 'HFlip'
for i in range(constants.NUM_H_FLIPS):
datagen = ImageDataGenerator(horizontal_flip=True)
it = datagen.flow(samples, batch_size=1)
batch = it.next()
image = batch[0].astype('uint8')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
uniqueFileName = baseName + str(i) + constants.RESULT_IMAGE_TYPE
cv2.imwrite(uniqueFileName, image_rgb)
return baseName + str(0) + constants.RESULT_IMAGE_TYPE
#receives user input regarding which augmentations need to be done and in which order/combination they need to be applied
userInput = input('Please enter the order of augmentations. \nOptions include: \nW for width \nH for height \nR for rotation \nB for brightness \nZ for zoom.\n'
'V for vertical flips\nF for horizontal flips\n')
#for loop gets names of all files in the folder INPUT_DIRECTORY
for imageName in os.listdir(constants.INPUT_DIRECTORY):
#loads image from the image's location and file name
imageLocation = constants.INPUT_DIRECTORY + '/' + imageName
img = load_img(imageLocation)
#parses through userInput, applies edits, and changes the imageName and imageLocation variables as needed
for currentLetter in userInput:
if currentLetter == 'w' or currentLetter == 'W':
imageName = widthShift(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'h' or currentLetter == 'H':
imageName = heightShift(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'r' or currentLetter == 'R':
imageName = rotation(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'b' or currentLetter == 'B':
imageName = brightness(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'z' or currentLetter == 'Z':
imageName = zoom(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'f' or currentLetter == 'F':
imageName = hFlip(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
elif currentLetter == 'v' or currentLetter == 'V':
imageName = vFlip(imageLocation,imageName)
imageLocation = constants.OUTPUT_DIRECTORY + '/' + imageName
else:
print('get gud')