/
augment_data.py
281 lines (237 loc) · 10.7 KB
/
augment_data.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
import os
import re
import math
import cv2
import yaml
import argparse
import numpy as np
import pandas as pd
import imageio
import imgaug as ia
from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage
from imgaug import augmenters as iaa
from PIL import Image
from PIL import ImageDraw
IMG_PREFIX = "aug1_"
IMG_WIDTH = 600
IMG_HEIGHT = 400
draw_bboxes = False
class DataAugmenter:
"""
Class for aplying data augmentations to images and labels
Methods
-------
setup()
Performs initial setup.
get_class_folders()
Obtains list of classes from folder names in training data
"""
def __init__(self, base_dir):
self.classes = None
self.bb_list = []
self.bb_list_df = None
self.labels_dir = "%s/labels" % base_dir
self.images_dir = "%s/images" % base_dir
self.aug_images_dir = "%s/aug_images" % base_dir
self.aug_images_bboxes_dir = "%s/aug_images_bboxes" % base_dir
self.aug_labels_dir = "%s/aug_labels" % base_dir
print('labels_dir:', self.labels_dir)
print('images_dir:', self.images_dir)
print('aug_images_dir:', self.aug_images_dir)
print('aug_images_bboxes_dir:', self.aug_images_bboxes_dir)
print('aug_labels_dir:', self.aug_labels_dir)
self.aug = iaa.SomeOf(2, [
iaa.Affine(scale=(0.5, 1.5)),
iaa.Affine(rotate=(-60, 60)),
iaa.Affine(translate_percent={"x":(-0.3, 0.3),"y":(-0.3, 0.3)}),
iaa.Fliplr(1),
iaa.Flipud(1)
])
def setup(self):
self.classes = list(self.get_class_folders(self.labels_dir))
# print(self.classes)
self.classes.remove('normal')
self.classes = sorted(self.classes, key=str.lower)
# print(self.classes)
def get_class_folders(self, path):
for folder in os.listdir(path):
if os.path.isdir(os.path.join(path, folder)):
yield folder
def bbs_obj_to_df(self, bbs_object):
"""
Converts bounding box object to DataFrame
Parameters
----------
bbs_object: imgaug.augmentables.bbs.BoundingBoxesOnImage
bounding box object to convert
Returns
-------
df_bbs: pd.DataFrame
the conversion result
"""
# print('bbs_obj_to_df')
# convert BoundingBoxesOnImage object into array
bbs_array = bbs_object.to_xyxy_array()
# convert to a DataFrame
df_bbs = pd.DataFrame(bbs_array, columns=['xmin', 'ymin', 'xmax', 'ymax'])
return df_bbs
def convert(self, size, box):
"""
Converts bounding box to YOLO format
Parameters
----------
size: tuple(int, int)
image size (width, height)
box: tuple(int, int, int, int)
bounding box to convert
Returns
-------
result: tuple(int, int, int, int)
the conversion result (x_center, y_center, width, height)
"""
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def draw_bbox(self, image, class_name, filename, bbs_df):
"""
Draw bounding box from DataFrame
"""
PIL_image = Image.fromarray(image.astype('uint8'), 'RGB')
draw = ImageDraw.Draw(PIL_image)
draw.rectangle([(bbs_df['xmin'].iloc[0], bbs_df['ymin'].iloc[0]), (bbs_df['xmax'].iloc[0], bbs_df['ymax'].iloc[0])], outline ='red')
# PIL_image.show()
PIL_image.save('%s/%s/%s%s' % (self.aug_images_bboxes_dir, class_name, IMG_PREFIX, filename))
def save_labels(self, label_path, image_aug, class_name, bbs_df):
"""
Save augmented bounding box labels in YOLO format
Parameters
----------
label_path: str
path to label file
image_aug: np.array
augmented image
class_name: str
class name of augmented image
bbs_df: pd.DataFrame
bounding boxes
Returns
-------
"""
with open(label_path, 'w') as f:
for index, row in bbs_df.iterrows():
xmin, ymin, xmax, ymax = row['xmin'], row['ymin'], row['xmax'], row['ymax']
w, h = image_aug.shape[1], image_aug.shape[0]
b = (xmin, xmax, ymin, ymax)
bb = self.convert((w, h), b)
class_ind = self.classes.index(class_name)
label_line = '{0} {1:.2f} {2:.2f} {3:.2f} {4:.2f}\n'.format(class_ind, *bb)
f.write(label_line)
def convert_to_df(self):
"""
Converts bounding boxes for training images in YOLO format to DataFrame
Parameters
----------
"""
for class_folder in self.get_class_folders(self.labels_dir):
print('class: ', class_folder)
label_files = [f for f in os.listdir(os.path.join(self.labels_dir, class_folder))
if f.endswith('txt')
and os.path.getsize(os.path.join(self.labels_dir, class_folder, f)) > 0]
for label_file in label_files:
img_name = label_file.split('.')[0] + '.jpeg'
with open(os.path.join(self.labels_dir, class_folder, label_file)) as f:
for line in f:
# print(line.split(' '))
class_ind = int(line.split(' ')[0])
# print('class_ind:', class_ind)
box = [float(x) for x in line.split(' ')[1:]] * np.array([IMG_WIDTH, IMG_HEIGHT, IMG_WIDTH, IMG_HEIGHT])
(x_center, y_center, width, height) = box.astype("int")
x_min = int(x_center - (width / 2))
y_min = int(y_center - (height / 2))
x_max = x_min + int(width)
y_max = y_min + int(height)
class_name = self.classes[class_ind]
value = (img_name, IMG_WIDTH, IMG_HEIGHT, class_name, x_min, y_min, x_max, y_max)
self.bb_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
self.bb_list_df = pd.DataFrame(self.bb_list, columns=column_name)
# save to csv
self.bb_list_df.to_csv(('labels.csv'), index=None)
def augment(self):
print('Augmenting ...')
aug_bbs_xy = pd.DataFrame(columns=
['filename','width','height','class', 'xmin', 'ymin', 'xmax', 'ymax']
)
class_grouped = self.bb_list_df.groupby('class')
for class_name in self.bb_list_df['class'].unique():
class_group_df = class_grouped.get_group(class_name)
filename_grouped = class_group_df.groupby('filename')
for filename in class_group_df['filename'].unique():
name = filename.split('.')[0]
# get separate data frame grouped by file name
filename_group_df = filename_grouped.get_group(filename)
filename_group_df = filename_group_df.reset_index()
filename_group_df = filename_group_df.drop(['index'], axis=1)
# read image and bbox labels
image_path = '%s/%s/%s' % (self.images_dir, class_name, filename)
image = imageio.imread(image_path)
bb_array = filename_group_df.drop(['filename', 'width', 'height', 'class'], axis=1).values
bbs = BoundingBoxesOnImage.from_xyxy_array(bb_array, shape=image.shape)
# apply augmentation on image and on the bounding boxes
image_aug, bbs_aug = self.aug(image=image, bounding_boxes=bbs)
# disregard bounding boxes which have fallen out of image pane
bbs_aug = bbs_aug.remove_out_of_image()
# clip bounding boxes which are partially outside of image pane
bbs_aug = bbs_aug.clip_out_of_image()
# don't perform any actions with the image without bounding boxes and for class storage
if re.findall('Image...', str(bbs_aug)) == ['Image([]'] or class_name == 'storage':
pass
else:
# create a data frame with augmented values of image width and height
info_df = filename_group_df.drop(['xmin', 'ymin', 'xmax', 'ymax'], axis=1)
for index, _ in info_df.iterrows():
info_df.at[index, 'width'] = image_aug.shape[1]
info_df.at[index, 'height'] = image_aug.shape[0]
# rename filenames by adding the predifined prefix
info_df['filename'] = info_df['filename'].apply(lambda x: IMG_PREFIX + x)
# create a data frame with augmented bounding boxes coordinates
bbs_df = self.bbs_obj_to_df(bbs_aug)
bbs_df[['xmin', 'ymin', 'xmax', 'ymax']] = bbs_df[['xmin', 'ymin', 'xmax', 'ymax']].astype(int)
# concat all new augmented info into new data frame
aug_df = pd.concat([info_df, bbs_df], axis=1)
if aug_df.isnull().sum().sum() == 0:
# draw bbox
if draw_bboxes:
self.draw_bbox(image_aug, class_name, filename, bbs_df)
# save augmented image
cv2.imwrite('%s/%s/%s%s' % (self.aug_images_dir, class_name, IMG_PREFIX, filename), image_aug)
# append rows to aug_bbs_xy data frame
aug_bbs_xy = pd.concat([aug_bbs_xy, aug_df])
# write bboxes to label file
label_path = os.path.join(self.aug_images_dir, class_name, name + '.txt')
self.save_labels(label_path, image_aug, class_name, bbs_df)
# construct dataframe with updated images and bounding boxes annotations
aug_bbs_xy = aug_bbs_xy.reset_index()
aug_bbs_xy = aug_bbs_xy.drop(['index'], axis=1)
def process(self):
self.setup()
self.convert_to_df()
self.augment()
def augment_data():
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required=True,
help="Path to data")
args = vars(ap.parse_args())
base_dir = args['path']
augmenter = DataAugmenter(base_dir)
augmenter.process()
if __name__ == '__main__':
augment_data()