-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageaugmentation.py
More file actions
78 lines (73 loc) · 2.81 KB
/
imageaugmentation.py
File metadata and controls
78 lines (73 loc) · 2.81 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
import os, random
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
import librys
def generateImages(augmentation, value, cval=0.0):
train_dir = librys.pathDir().loadDir()[0]
# idx_label = np.random.randint(low=0, high=len(os.listdir(path=train_dir)))
idx_label = 0
idx_img = np.random.randint(low=0, high=len(os.listdir(path=train_dir+os.listdir(path=train_dir)[idx_label])))
img_path = train_dir+os.listdir(path=train_dir)[idx_label]+'/'+os.listdir(path=train_dir+os.listdir(path=train_dir)[idx_label])[idx_img]
img = imread(img_path)
fig, ax = plt.subplots(nrows=1, ncols=5, figsize=(25,5))
if augmentation.lower() == "rotation_range":
try:
data_gen = ImageDataGenerator(rotation_range=value)
except:
return ValueError
elif augmentation.lower() == "width_shift_range":
try:
data_gen = ImageDataGenerator(width_shift_range=value)
except:
return ValueError
elif augmentation.lower() == "height_shift_range":
try:
data_gen = ImageDataGenerator(height_shift_range=value)
except:
return ValueError
elif augmentation.lower() == "shear_range":
try:
data_gen = ImageDataGenerator(shear_range=value)
except:
return ValueError
elif augmentation.lower() == "brightness_range":
try:
data_gen = ImageDataGenerator(brightness_range=value)
except:
return ValueError
elif augmentation.lower() == "zoom_range":
try:
data_gen = ImageDataGenerator(zoom_range=value)
except:
return ValueError
elif augmentation.lower() == "channel_shift_range":
try:
data_gen = ImageDataGenerator(channel_shift_range=value)
except:
return ValueError
elif augmentation.lower() == "horizontal_flip":
try:
data_gen = ImageDataGenerator(horizontal_flip=value)
except:
return ValueError
elif augmentation.lower() == "vertical_flip":
try:
data_gen = ImageDataGenerator(vertical_flip=value)
except:
return ValueError
elif augmentation.lower() == "fill_mode":
try:
data_gen = ImageDataGenerator(width_shift_range=0.4, fill_mode=value, cval=cval)
except:
return ValueError
else:
return ValueError
images_iter = data_gen.flow(np.expand_dims(image.img_to_array(img), axis=0))
for col in range(5):
ax[col].imshow(images_iter.next()[0].astype('int'))
ax[col].axis('off')
plt.suptitle(augmentation+" = "+str(value), fontsize=26, fontweight='bold')
plt.show()