-
Notifications
You must be signed in to change notification settings - Fork 28
/
odir_data_augmentation_strategies.py
140 lines (116 loc) · 5.73 KB
/
odir_data_augmentation_strategies.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
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
# Copyright 2019-2020 Jordi Corbilla. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import csv
import os
import cv2
from odir_image_treatment import ImageTreatment
class DataAugmentationStrategy:
def __init__(self, image_size, file_name):
self.base_image = file_name
self.treatment = ImageTreatment(image_size)
self.file_path = r'C:\temp\ODIR-5K_Training_Dataset_treated_' + str(image_size)
self.saving_path = r'C:\temp\ODIR-5K_Training_Dataset_augmented_' + str(image_size)
self.file_id = file_name.replace('.jpg', '')
def save_image(self, original_vector, image, sample):
central = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
file = self.file_id + '_'+str(sample)+'.jpg'
file_name = os.path.join(self.saving_path, file)
exists = os.path.isfile(file_name)
if exists:
print("duplicate file found: " + file_name)
status = cv2.imwrite(file_name, central)
with open(r'ground_truth\odir_augmented.csv', 'a', newline='') as csv_file:
file_writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
file_writer.writerow([file, original_vector[1], original_vector[2], original_vector[3], original_vector[4],
original_vector[5], original_vector[6], original_vector[7], original_vector[8]])
#print(file_name + " written to file-system : ", status)
def generate_images(self, number_samples, original_vector, weights):
eye_image = os.path.join(self.file_path, self.base_image)
image = cv2.imread(eye_image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
original_image = image
saved = 0
# For any repeating elements, just give the other output
# We are only expecting up to 3 repetitions
if weights == 20:
original_image = self.treatment.rot90(original_image, 2)
if weights == 400:
original_image = self.treatment.rot90(original_image, 3)
if weights > 401:
print(str(self.file_id) + ' samples:' + str(number_samples))
raise ValueError('this cannot happen')
# for the sample type 14, just generate 1 image and leave the method
if number_samples == 14:
central = self.treatment.rot90(original_image, 1)
self.save_image(original_vector, central, weights+14)
saved = saved +1
return saved
if number_samples > 0:
central = self.treatment.crop_to_bounding_box(original_image, 0, 0, 112, 112)
self.save_image(original_vector, central, weights+0)
saved = saved + 1
if number_samples > 1:
central = self.treatment.crop_to_bounding_box(original_image, 112, 0, 112, 112)
self.save_image(original_vector, central, weights+1)
saved = saved + 1
if number_samples > 2:
central = self.treatment.crop_to_bounding_box(original_image, 0, 112, 112, 112)
self.save_image(original_vector, central, weights+2)
saved = saved + 1
if number_samples > 3:
central = self.treatment.crop_to_bounding_box(original_image, 112, 112, 112, 112)
self.save_image(original_vector, central, weights+3)
saved = saved + 1
if number_samples > 4:
vector = [0.50]
central = self.treatment.scaling(original_image, vector)
self.save_image(original_vector, central[0], weights+4)
saved = saved + 1
if number_samples > 5:
vector = [0.70]
central = self.treatment.scaling(original_image, vector)
self.save_image(original_vector, central[0], weights+5)
saved = saved + 1
if number_samples > 6:
vector = [0.80]
central = self.treatment.scaling(original_image, vector)
self.save_image(original_vector, central[0], weights+6)
saved = saved + 1
if number_samples > 7:
vector = [0.90]
central = self.treatment.scaling(original_image, vector)
self.save_image(original_vector, central[0], weights+7)
saved = saved + 1
if number_samples > 8:
central = self.treatment.rescale_intensity(original_image)
self.save_image(original_vector, central, weights+8)
saved = saved + 1
if number_samples > 9:
central = self.treatment.contrast(original_image, 2)
self.save_image(original_vector, central, weights+9)
saved = saved + 1
if number_samples > 10:
central = self.treatment.saturation(original_image, 0.5)
self.save_image(original_vector, central, weights+10)
saved = saved + 1
if number_samples > 11:
central = self.treatment.gamma(original_image, 0.5)
self.save_image(original_vector, central, weights+11)
saved = saved + 1
if number_samples > 12:
central = self.treatment.hue(original_image, 0.2)
self.save_image(original_vector, central, weights+12)
saved = saved + 1
return saved