forked from ZhuJD-China/ImgAug_yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AUG.py
327 lines (279 loc) · 13.4 KB
/
AUG.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from pathlib import Path
from tqdm import tqdm
import numpy as np
import cv2
from sklearn.model_selection import train_test_split
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
import os
import shutil
from skimage.util import random_noise
np.random.seed(42)
#import tensorflow_hub as hub
#import tensorflow as tf
#define here the location of the images and the annotation. in this version they might be in the same directory
####################################################
image_directory = 'test_aug/images/train'
labels_directory = 'test_aug/labels/train'
####################################################
def read_files(img_dir, lbl_dir):
lbls_dataset = []
#annotations and images might be in the same directory
files = os.listdir(img_dir)
images = list(filter(lambda x: ('.jpg' in x) or ('.png' in x) , files)) #modify if other image formats are present
img_names = []
for i, image_name in enumerate(tqdm(images)): # Load images and labels from source
img_names.append(image_name)
with open(lbl_dir + '/' + image_name.split('.')[0] + '.txt') as f:
lbls_dataset.append(f.readlines())
# lbls_dataset[i][0] = lbls_dataset[i][0].replace('\n', '_').split(
# '_') # remove unwanted characters and split data
return lbls_dataset, img_names
def find_data(labels_dataset, typ):
index = []
for d in range(len(labels_dataset)):
print(labels_dataset[d][0])
print(type(labels_dataset[d][0]))
if int(labels_dataset[d][0][0]) == typ:
index.append(d)
if len(index) == 0:
print('no labels')
return index
def xml2dim(labels_dataset):
lbl = labels_dataset[0] # category
a = float(labels_dataset[1]) # box center X
b = float(labels_dataset[2]) # box center Y
bbox_width = float(labels_dataset[3]) # box width
bbox_height = float(labels_dataset[4]) # box height
return lbl, a, b, bbox_width, bbox_height
def create_imgnlbl(labels_len, name_l, img):
for label_index in range(labels_len):
new_labels_ = labels_dataset[pic_index][label_index].split(" ")
lbl, a, b, bbox_width, bbox_height = xml2dim(new_labels_) # lbl, a, b, bbox_width, bbox_height
w = int(img.shape[1])
h = int(img.shape[0])
x1, y1 = np.round_((a - bbox_width / 2) * w, 0), np.round_((b - bbox_height / 2) * h, 0)
x2, y2 = np.round_((a + bbox_width / 2) * w, 0), np.round_((b + bbox_height / 2) * h, 0)
labels_path = Path(f"{labels_directory}") # labels path
h, w, _ = img.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
with (labels_path / name_l).open(mode="a") as label_file:
label_file.write(
f"{lbl} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
)
return
def create_lblbox(img, lbl, x1, x2, y1, y2):
h, w, _ = img.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
return [
[float(lbl), float(x1 + bbox_width / 2), float(y1 + bbox_height / 2), float(bbox_width), float(bbox_height)]]
def shear(labels_len, new_name, img, shx, shy):
M = np.float32([[1, shx, 0], [shy, 1, 0], [0, 0, 1]])
sheared_img = cv2.warpPerspective(img, M, (int(img.shape[1] * (1 + shx)), int(img.shape[0] * (1 + shy))))
for label_index in range(labels_len):
new_labels_ = labels_dataset[pic_index][label_index].split(" ")
lbl, a, b, bbox_width, bbox_height = xml2dim(new_labels_) # lbl, a, b, bbox_width, bbox_height
h, w, _ = img.shape
x1, y1 = np.round_((a - bbox_width / 2) * w, 0), np.round_((b - bbox_height / 2) * h, 0)
x2, y2 = np.round_((a + bbox_width / 2) * w, 0), np.round_((b + bbox_height / 2) * h, 0)
# Affine Transformation
u1, u2 = int(M[0][0] * x1 + M[0][1] * y1 + M[0][2]), int(M[0][0] * x2 + M[0][1] * y2 + M[0][2])
v1, v2 = int(M[1][0] * x1 + M[1][1] * y1 + M[1][2]), int(M[1][0] * x2 + M[1][1] * y2 + M[1][2])
# sheared_img, u1, u2, v1, v2
x1 = u1
x2 = u2
y1 = v1
y2 = v2
labels_path = Path(f"{labels_directory}") # labels path
h, w, _ = sheared_img.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
with (labels_path / new_name).open(mode="a") as label_file:
label_file.write(
f"{lbl} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
)
return sheared_img
def flip(labels_len, new_name, img, mode):
"""
modes:
0 = left to right
1 = up to down
"""
for label_index in range(labels_len):
new_labels_ = labels_dataset[pic_index][label_index].split(" ")
lbl, a, b, bbox_width, bbox_height = xml2dim(new_labels_) # lbl, a, b, bbox_width, bbox_height
h, w, _ = img.shape
if mode == 0:
a = 1 - a
flip_img = cv2.flip(img, 1)
elif mode == 1:
b = 1 - b
flip_img = cv2.flip(img, 0)
else:
print('mode does not exist')
return
x1, y1 = np.round_((a - bbox_width / 2) * w, 0), np.round_((b - bbox_height / 2) * h, 0)
x2, y2 = np.round_((a + bbox_width / 2) * w, 0), np.round_((b + bbox_height / 2) * h, 0)
labels_path = Path(f"{labels_directory}") # labels path
h, w, _ = flip_img.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
with (labels_path / new_name).open(mode="a") as label_file:
label_file.write(
f"{lbl} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
)
return flip_img
def rotate(labels_len, new_name, img, mode):
"""
modes:
0 = 90° counterclockwise
1 = 180°
2 = 270° counterclowise / 90° clockwise
"""
rot_img = None
for label_index in range(labels_len):
new_labels_ = labels_dataset[pic_index][label_index].split(" ")
lbl, a, b, bbox_width, bbox_height = xml2dim(new_labels_) # lbl, a, b, bbox_width, bbox_height
h, w, _ = img.shape
if mode == 0:
rot_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
x1, y1 = np.round_((b - bbox_height / 2) * h, 0), np.round_((1 - a - bbox_width / 2) * w, 0)
x2, y2 = np.round_((b + bbox_height / 2) * h, 0), np.round_((1 - a + bbox_width / 2) * w, 0)
elif mode == 1:
rot_img = cv2.rotate(img, cv2.ROTATE_180)
a = 1 - a
b = 1 - b
x1, y1 = np.round_((a - bbox_width / 2) * w, 0), np.round_((b - bbox_height / 2) * h, 0)
x2, y2 = np.round_((a + bbox_width / 2) * w, 0), np.round_((b + bbox_height / 2) * h, 0)
elif mode == 2:
rot_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
x1, y1 = np.round_((1 - b - bbox_height / 2) * h, 0), np.round_((a - bbox_width / 2) * w, 0)
x2, y2 = np.round_((1 - b + bbox_height / 2) * h, 0), np.round_((a + bbox_width / 2) * w, 0)
else:
print('mode does not exist')
labels_path = Path(f"{labels_directory}") # labels path
h, w, _ = rot_img.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
with (labels_path / new_name).open(mode="a") as label_file:
label_file.write(
f"{lbl} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
)
return rot_img
def rand_erasing(labels_len, new_name, img, mode):
"""
modes:
0 = Object-aware Random Erasing (ORE)
1 = Image-aware Random Erasing (IRE)
2 = Image and object-aware Random Erasing (I+ORE)
"""
im = img
for label_index in range(labels_len):
new_labels_ = labels_dataset[pic_index][label_index].split(" ")
lbl, a, b, bbox_width, bbox_height = xml2dim(new_labels_) # lbl, a, b, bbox_width, bbox_height
w = int(img.shape[1])
h = int(img.shape[0])
x1, y1 = np.round_((a - bbox_width / 2) * w, 0), np.round_((b - bbox_height / 2) * h, 0)
x2, y2 = np.round_((a + bbox_width / 2) * w, 0), np.round_((b + bbox_height / 2) * h, 0)
xe, ye = x2, y2
We, He = 1, 1
if (mode == 0):
while not (xe + We <= x2 and ye + He <= y2):
xe = np.random.choice(range(int(x1), int(x2)))
ye = np.random.choice(range(int(y1), int(y2)))
re = np.random.rand() * 0.7 # maximum % of total bbox area
Se = int((x2 - x1) * (y2 - y1) * np.random.rand())
He = int(np.round(np.sqrt(Se * re), 0))
We = int(np.round(np.sqrt(Se / re), 0))
# Rectangle Ie=(xe,ye,xe+We, ye+He)
for i in range(ye, ye + He):
for j in range(xe, xe + We):
val = np.random.choice(range(0, 255))
im[i][j][0] = val
im[i][j][1] = val
im[i][j][2] = val
elif (mode == 1):
h1 = int(np.round_(np.random.choice(range(0, int(h))) * 0.5, 0)) # maximum % of total image area
w1 = int(np.round_(np.random.choice(range(0, int(w))) * 0.5, 0)) # maximum % of total image area
h2 = int(np.round_(np.random.choice(range(h1, int(h))), 0))
w2 = int(np.round_(np.random.choice(range(w1, int(w))), 0))
for i in range(h1, h2):
for j in range(w1, w2):
val = np.random.choice(range(0, 255))
im[i][j][0] = val
im[i][j][1] = val
im[i][j][2] = val
elif mode == 2:
while not (xe + We <= x2 and ye + He <= y2):
xe = np.random.choice(range(int(x1), int(x2)))
ye = np.random.choice(range(int(y1), int(y2)))
re = np.random.rand() * 0.7 # maximum % of total bbox area
Se = int((x2 - x1) * (y2 - y1) * np.random.rand())
He = int(np.round(np.sqrt(Se * re), 0))
We = int(np.round(np.sqrt(Se / re), 0))
# Rectangle Ie=(xe,ye,xe+We, ye+He)
for i in range(ye, ye + He):
for j in range(xe, xe + We):
val = np.random.choice(range(0, 255))
im[i][j][0] = val
im[i][j][1] = val
im[i][j][2] = val
h1 = int(np.round_(np.random.choice(range(0, int(h))) * 0.5, 0)) # maximum % of total image area
w1 = int(np.round_(np.random.choice(range(0, int(w))) * 0.5, 0)) # maximum % of total image area
h2 = int(np.round_(np.random.choice(range(h1, int(h))), 0))
w2 = int(np.round_(np.random.choice(range(w1, int(w))), 0))
for i in range(h1, h2):
for j in range(w1, w2):
val = np.random.choice(range(0, 255))
im[i][j][0] = val
im[i][j][1] = val
im[i][j][2] = val
else:
print('ode does not exist')
labels_path = Path(f"{labels_directory}") # labels path
h, w, _ = im.shape
x1, y1 = x1 / w, y1 / h # escalate x and y (0 to 1)
x2, y2 = x2 / w, y2 / h
bbox_width = x2 - x1
bbox_height = y2 - y1
with (labels_path / new_name).open(mode="a") as label_file:
label_file.write(
f"{lbl} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
)
return im
labels_dataset, image_names = read_files(image_directory, labels_directory)
# pic_index = 0
for pic_index in range(len(image_names)):
img = np.array(cv2.imread(image_directory + '/' + image_names[pic_index]))
single_pic_name = image_names[pic_index].split('.')[0]
#enhance
img_gnoise = (255 * random_noise(img, mode='gaussian', var=0.05 ** 2)).astype(np.uint8)
cv2.imwrite(image_directory + "/" + single_pic_name + '_GN' + '.jpg', img_gnoise)
name_l = single_pic_name + '_GN' + '' + ".txt"
create_imgnlbl(len(labels_dataset[pic_index]), name_l, img_gnoise)
new_name = single_pic_name + '_sheared' + '' + ".txt"
img_shx = shear(len(labels_dataset[pic_index]), new_name, img, 0.1, 0) # sheared_img, u1, u2, v1, v2 _SX
cv2.imwrite(image_directory + "/" + single_pic_name + '_sheared' + '.jpg', img_shx)
new_name = single_pic_name + '_flip' + '' + ".txt"
img_fliplr = flip(len(labels_dataset[pic_index]), new_name, img, 0) # flip_img, x1, x2, y1, y2 _LR
cv2.imwrite(image_directory + "/" + single_pic_name + '_flip' + '.jpg', img_fliplr)
new_name = single_pic_name + '_img_r90' + '' + ".txt"
img_r90 = rotate(len(labels_dataset[pic_index]), new_name, img, 0) # rot_img, x1, x2, y1, y2 _R90
cv2.imwrite(image_directory + "/" + single_pic_name + '_img_r90' + '.jpg', img_r90)
#activate if this is desired, might be very slow
# new_name = single_pic_name + '_erasing' + '' + ".txt"
# img_re = rand_erasing(len(labels_dataset[pic_index]), new_name, img, 1) # img _RE
# cv2.imwrite(image_directory + "/" + single_pic_name + '_erasing' + '.jpg', img_re)