-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaug_polygon_labelme.py
267 lines (216 loc) · 9.66 KB
/
aug_polygon_labelme.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
"""
对labelme的标注文件进行增强, 得到训练文件
"""
from imgaug.augmentables import base
from imgaug.augmenters.geometric import Rot90
import numpy as np
import cv2
import json
import os
import imgaug.augmenters as iaa
from imgaug.augmentables.polys import Polygon, PolygonsOnImage
import imgaug
import argparse
import os
import glob
sometimes = lambda p, aug: iaa.Sometimes(p, aug)
def get_points_from_label(content):
points = []
for elem in content['shapes']:
points.append(elem["points"])
return points
def vis_polygon(image, points):
# points (n, m, 2)
points = np.array(points)
points = points[:, :, np.newaxis, :].astype(np.int)
# cv2.polylines(image, points, True, (128, 255, 0), thickness=2)
cv2.polylines(image, points, True, (0, 0, 255), thickness=2)
return image
def vis_polygon_from_json(image_path, json_path):
image = cv2.imread(image_path)
if image is None:
return None
fr = open(json_path, 'r')
content = json.load(fr)
fr.close()
points = get_points_from_label(content)
image = vis_polygon(image, points)
return image
def vis_polygon_batch(label_list, image_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(label_list, 'r') as fr:
for i, label_path in enumerate(fr):
label_path = label_path.strip()
name = os.path.split(label_path)[-1]
shortname = os.path.splitext(name)[0]
image_path = os.path.join(image_dir, shortname + ".jpg")
image = vis_polygon_from_json(image_path, label_path)
save_image_path = os.path.join(output_dir, shortname+".jpg")
cv2.imwrite(save_image_path, image)
if i % 10 == 0:
print("{} has done".format(i))
def convert_to_segment(mask, points, scalar):
"""
@param image
@param points [[x1, y1], [x2, y2], [x3, y3], [x1, y1]]
"""
points = np.array(points)
# cv2.fillConvexPoly(mask, points, scalar)
cv2.fillPoly(mask, [points], scalar)
return mask
def resize_by_scale(image, max_len):
height, width = image.shape[0:2]
scale = min(max_len/height, max_len/width)
if height < width:
new_width = max_len
new_height = int(round(height * scale))
elif height > width:
new_width = int(round(width * scale))
new_height = max_len
else:
new_width = max_len
new_height = max_len
# image = cv2.imresize(image, (new_width, new_height))
# return image, scale
return new_width, new_height
def get_segments(label_list, image_dir, output_dir, max_len, pixel_constant=1):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
base_seq = iaa.Sequential(
[
sometimes(0.3, iaa.AdditiveGaussianNoise(scale=(0.05, 0.4))),
sometimes(0.3, iaa.GammaContrast(gamma=(0.8, 1.2))),
sometimes(0.2, iaa.AddToSaturation(value=(-10, 10))),
sometimes(0.2, iaa.GaussianBlur(sigma=(0.05, 0.3))),
],
random_order=True
)
seq_origin = iaa.Sequential([
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_lr = iaa.Sequential([
iaa.Fliplr(),
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_ud = iaa.Sequential([
iaa.Flipud(),
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_rot90 = iaa.Sequential([
# iaa.Rotate(rotate=90),
iaa.Rot90(k=1, keep_size=False),
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_rot180 = iaa.Sequential([
# iaa.Rotate(rotate=180),
iaa.Rot90(k=2, keep_size=False),
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_rot270 = iaa.Sequential([
iaa.Rot90(k=3, keep_size=False),
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom")
])
seq_normal = iaa.Sequential([
base_seq,
iaa.PadToFixedSize(width=max_len, height=max_len, position="normal")
])
seq_resize = iaa.Sequential([
base_seq,
iaa.Resize((max_len, max_len))
# iaa.PadToFixedSize(width=max_len, height=max_len, position="normal")
])
with open(label_list, 'r') as fr:
for i, label_path in enumerate(fr):
label_path = label_path.strip()
name = os.path.split(label_path)[-1]
shortname = os.path.splitext(name)[0]
# get points
content = json.load(open(label_path, 'r'))
points = get_points_from_label(content)
psoi = Polygon(points[0])
# get image
image_path = glob.glob(os.path.join(image_dir, shortname + ".*"))[0]
image = cv2.imread(image_path)
# resize
new_width, new_height = resize_by_scale(image, max_len)
seq = iaa.Sequential([
iaa.Resize({"height":new_height, "width":new_width}),
])
image, psoi = seq(image=image, polygons=psoi)
#
image_aug, psoi_aug = seq_origin(image=image, polygons=psoi)
mask = np.zeros((max_len, max_len), dtype=np.uint8)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_origion.jpg")
save_label_path = os.path.join(output_dir, shortname+"_origion.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_lr(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_horizontal.jpg")
save_label_path = os.path.join(output_dir, shortname+"_horizontal.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_ud(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_vertical.jpg")
save_label_path = os.path.join(output_dir, shortname+"_vertical.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_rot90(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_rot90.jpg")
save_label_path = os.path.join(output_dir, shortname+"_rot90.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_rot180(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_rot180.jpg")
save_label_path = os.path.join(output_dir, shortname+"_rot180.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_rot270(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_rot270.jpg")
save_label_path = os.path.join(output_dir, shortname+"_rot270.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_normal(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_normal.jpg")
save_label_path = os.path.join(output_dir, shortname+"_normal.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
#
image_aug, psoi_aug = seq_resize(image=image, polygons=psoi)
mask_new = convert_to_segment(mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant)
save_image_path = os.path.join(output_dir, shortname+"_resize.jpg")
save_label_path = os.path.join(output_dir, shortname+"_resize.png")
cv2.imwrite(save_image_path, image_aug)
cv2.imwrite(save_label_path, mask_new)
# image_polys_aug = psoi_aug.draw_on_image(image_aug)
# cv2.imwrite("output.jpg", image_polys_aug)
# exit()
if i % 10 == 0:
print("{} has done".format(i))
if __name__ == "__main__":
parser = argparse.ArgumentParser("ONNX Location Demo")
parser.add_argument('-l', '--label_list', type=str, default=None, help='label list')
parser.add_argument('-d', '--dst_dir', type=str, default=None, help='output dir')
parser.add_argument('-s', '--src_dir', type=str, default=None, help='image dir')
parser.add_argument('--max_len', type=int, default=320, help='max len')
args = parser.parse_args()
get_segments(args.label_list, args.src_dir, args.dst_dir, args.max_len, pixel_constant=1)