-
Notifications
You must be signed in to change notification settings - Fork 0
/
augmentation.py
187 lines (163 loc) · 8.22 KB
/
augmentation.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
import albumentations as A
import utils
import os
def transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename):
bboxes_local = []
for bbox in bboxes:
faugBbox = list(map(lambda fcoord: float(f'{float(fcoord):.6f}'), bbox[:-1]))
faugBbox = [*faugBbox, bbox[-1]]
bboxes_local.append(faugBbox)
try:
transformResult = transform(image=img, bboxes=bboxes_local)
augImg = transformResult["image"]
augBboxes = transformResult["bboxes"]
faugBboxes = []
for bbox in augBboxes:
faugBbox = list(map(lambda fcoord: float(f'{float(fcoord):.6f}'), bbox[:-1]))
faugBbox = [*faugBbox, bbox[-1]]
faugBboxes.append(faugBbox)
unformattedAugBboxes = utils.unformatBBOXes(faugBboxes)
unformattedAugBboxes = list(map(lambda uB: list(map(lambda b: str(b), uB)), unformattedAugBboxes))
utils.saveBboxes(bboxesFilename, unformattedAugBboxes)
utils.saveImage(imgFilename, augImg)
except: pass
def augRot0(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
bboxes = utils.readBBOXes(labelsPath)
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_0.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_0.txt")
utils.saveBboxes(bboxesFilename, bboxes)
utils.saveImage(imgFilename, img)
def augRot0Flip(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.HorizontalFlip(
always_apply=True,
p = 1
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_0_flip.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_0_flip.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot90(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=width, width=height, always_apply=True),
A.SafeRotate(
[-90, -90],
p = 1,
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_90.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_90.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot90Flip(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=width, width=height, always_apply=True),
A.SafeRotate(
[-90, -90],
p = 1,
),
A.HorizontalFlip(
always_apply=True,
p = 1
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_90_flip.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_90_flip.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot180(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=height, width=width, always_apply=True),
A.SafeRotate(
[-180, -180],
p = 1,
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_180.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_180.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot180Flip(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=height, width=width, always_apply=True),
A.SafeRotate(
[-180, -180],
p = 1,
),
A.HorizontalFlip(
always_apply=True,
p = 1
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_180_flip.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_180_flip.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot270(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=width, width=height, always_apply=True),
A.SafeRotate(
[-270, -270],
p = 1,
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_270.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_270.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augRot270Flip(imgPath : str, labelsPath : str, resultFolderPath : str):
img = utils.readImage(imgPath)
height, width, _ = img.shape
bboxes = utils.formatBBOXes(utils.readBBOXes(labelsPath))
transform = A.Compose([
A.Resize(height=width, width=height, always_apply=True),
A.SafeRotate(
[-270, -270],
p = 1,
),
A.HorizontalFlip(
always_apply=True,
p = 1
),
A.RandomBrightnessContrast(brightness_limit=(-0.3, 0.3), contrast_limit=0, p=0.7)
], bbox_params=A.BboxParams(format="yolo", min_visibility=0.6))
imgFilename = os.path.join(resultFolderPath, imgPath.split(os.path.sep)[-1].split(".")[0] + "_270_flip.jpg")
bboxesFilename = os.path.join(resultFolderPath, labelsPath.split(os.path.sep)[-1].split(".")[0] + "_270_flip.txt")
transformAndSave(transform, img, bboxes, imgFilename, bboxesFilename)
def augmentDataset(datasetFolder, resultFolder):
print("Augmentation started!")
try: os.mkdir(resultFolder)
except: pass
imgs = list(filter(lambda it: it.endswith(".jpg"), os.listdir(datasetFolder)))
labels = list(filter(lambda it: it.endswith(".txt"), os.listdir(datasetFolder)))
imgs_paths = list(map(lambda arg: os.path.join(datasetFolder, arg), imgs))
labels_paths = list(map(lambda arg: os.path.join(datasetFolder, arg), labels))
for img, label in zip(imgs_paths, labels_paths):
print(f"augmenting: {img} and {label}")
augRot0(img, label, resultFolder)
augRot0Flip(img, label, resultFolder)
augRot90(img, label, resultFolder)
augRot90Flip(img, label, resultFolder)
augRot180(img, label, resultFolder)
augRot180Flip(img, label, resultFolder)
augRot270(img, label, resultFolder)
augRot270Flip(img, label, resultFolder)