forked from boostcampaitech5/level2_cv_datacentric-cv-03
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
498 lines (419 loc) · 16.1 KB
/
dataset.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import os.path as osp
import math
import json
from tqdm import tqdm
import pickle
import numpy as np
import cv2
import albumentations as A
from torch.utils.data import Dataset
from shapely.geometry import Polygon
def cal_distance(x1, y1, x2, y2):
"""calculate the Euclidean distance"""
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def move_points(vertices, index1, index2, r, coef):
"""move the two points to shrink edge
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
index1 : offset of point1
index2 : offset of point2
r : [r1, r2, r3, r4] in paper
coef : shrink ratio in paper
Output:
vertices: vertices where one edge has been shinked
"""
index1 = index1 % 4
index2 = index2 % 4
x1_index = index1 * 2 + 0
y1_index = index1 * 2 + 1
x2_index = index2 * 2 + 0
y2_index = index2 * 2 + 1
r1 = r[index1]
r2 = r[index2]
length_x = vertices[x1_index] - vertices[x2_index]
length_y = vertices[y1_index] - vertices[y2_index]
length = cal_distance(
vertices[x1_index], vertices[y1_index], vertices[x2_index], vertices[y2_index]
)
if length > 1:
ratio = (r1 * coef) / length
vertices[x1_index] += ratio * (-length_x)
vertices[y1_index] += ratio * (-length_y)
ratio = (r2 * coef) / length
vertices[x2_index] += ratio * length_x
vertices[y2_index] += ratio * length_y
return vertices
def shrink_poly(vertices, coef=0.3):
"""shrink the text region
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
coef : shrink ratio in paper
Output:
v : vertices of shrinked text region <numpy.ndarray, (8,)>
"""
x1, y1, x2, y2, x3, y3, x4, y4 = vertices
r1 = min(cal_distance(x1, y1, x2, y2), cal_distance(x1, y1, x4, y4))
r2 = min(cal_distance(x2, y2, x1, y1), cal_distance(x2, y2, x3, y3))
r3 = min(cal_distance(x3, y3, x2, y2), cal_distance(x3, y3, x4, y4))
r4 = min(cal_distance(x4, y4, x1, y1), cal_distance(x4, y4, x3, y3))
r = [r1, r2, r3, r4]
# obtain offset to perform move_points() automatically
if cal_distance(x1, y1, x2, y2) + cal_distance(x3, y3, x4, y4) > cal_distance(
x2, y2, x3, y3
) + cal_distance(x1, y1, x4, y4):
offset = 0 # two longer edges are (x1y1-x2y2) & (x3y3-x4y4)
else:
offset = 1 # two longer edges are (x2y2-x3y3) & (x4y4-x1y1)
v = vertices.copy()
v = move_points(v, 0 + offset, 1 + offset, r, coef)
v = move_points(v, 2 + offset, 3 + offset, r, coef)
v = move_points(v, 1 + offset, 2 + offset, r, coef)
v = move_points(v, 3 + offset, 4 + offset, r, coef)
return v
def get_rotate_mat(theta):
"""positive theta value means rotate clockwise"""
return np.array(
[[math.cos(theta), -math.sin(theta)], [math.sin(theta), math.cos(theta)]]
)
def rotate_vertices(vertices, theta, anchor=None):
"""rotate vertices around anchor
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
theta : angle in radian measure
anchor : fixed position during rotation
Output:
rotated vertices <numpy.ndarray, (8,)>
"""
v = vertices.reshape((4, 2)).T
if anchor is None:
anchor = v[:, :1]
rotate_mat = get_rotate_mat(theta)
res = np.dot(rotate_mat, v - anchor)
return (res + anchor).T.reshape(-1)
def get_boundary(vertices):
"""get the tight boundary around given vertices
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
Output:
the boundary
"""
x1, y1, x2, y2, x3, y3, x4, y4 = vertices
x_min = min(x1, x2, x3, x4)
x_max = max(x1, x2, x3, x4)
y_min = min(y1, y2, y3, y4)
y_max = max(y1, y2, y3, y4)
return x_min, x_max, y_min, y_max
def cal_error(vertices):
"""default orientation is x1y1 : left-top, x2y2 : right-top, x3y3 : right-bot, x4y4 : left-bot
calculate the difference between the vertices orientation and default orientation
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
Output:
err : difference measure
"""
x_min, x_max, y_min, y_max = get_boundary(vertices)
x1, y1, x2, y2, x3, y3, x4, y4 = vertices
err = (
cal_distance(x1, y1, x_min, y_min)
+ cal_distance(x2, y2, x_max, y_min)
+ cal_distance(x3, y3, x_max, y_max)
+ cal_distance(x4, y4, x_min, y_max)
)
return err
def find_min_rect_angle(vertices):
"""find the best angle to rotate poly and obtain min rectangle
Input:
vertices: vertices of text region <numpy.ndarray, (8,)>
Output:
the best angle <radian measure>
"""
angle_interval = 1
angle_list = list(range(-90, 90, angle_interval))
area_list = []
for theta in angle_list:
rotated = rotate_vertices(vertices, theta / 180 * math.pi)
x1, y1, x2, y2, x3, y3, x4, y4 = rotated
temp_area = (max(x1, x2, x3, x4) - min(x1, x2, x3, x4)) * (
max(y1, y2, y3, y4) - min(y1, y2, y3, y4)
)
area_list.append(temp_area)
sorted_area_index = sorted(list(range(len(area_list))), key=lambda k: area_list[k])
min_error = float("inf")
best_index = -1
rank_num = 10
# find the best angle with correct orientation
for index in sorted_area_index[:rank_num]:
rotated = rotate_vertices(vertices, angle_list[index] / 180 * math.pi)
temp_error = cal_error(rotated)
if temp_error < min_error:
min_error = temp_error
best_index = index
return angle_list[best_index] / 180 * math.pi
def is_cross_text(start_loc, length, vertices):
"""check if the crop image crosses text regions
Input:
start_loc: left-top position
length : length of crop image
vertices : vertices of text regions <numpy.ndarray, (n,8)>
Output:
True if crop image crosses text region
"""
if vertices.size == 0:
return False
start_w, start_h = start_loc
a = np.array(
[
start_w,
start_h,
start_w + length,
start_h,
start_w + length,
start_h + length,
start_w,
start_h + length,
]
).reshape((4, 2))
p1 = Polygon(a).convex_hull
for vertice in vertices:
p2 = Polygon(vertice.reshape((4, 2))).convex_hull
inter = p1.intersection(p2).area
if 0.01 <= inter / p2.area <= 0.99:
return True
return False
def crop_img(img, vertices, labels, length):
"""crop img patches to obtain batch and augment
Input:
img : ndarray
vertices : vertices of text regions <numpy.ndarray, (n,8)>
labels : 1->valid, 0->ignore, <numpy.ndarray, (n,)>
length : length of cropped image region
Output:
region : cropped image region
new_vertices: new vertices in cropped region
"""
h, w, _ = img.shape
# confirm the shortest side of image >= length
if h >= w and w < length:
img = cv2.resize(
img, (length, int(h * length / w)), interpolation=cv2.INTER_LINEAR
)
elif h < w and h < length:
img = cv2.resize(
img, (int(w * length / h), length), interpolation=cv2.INTER_LINEAR
)
ratio_w = img.shape[1] / w
ratio_h = img.shape[0] / h
assert ratio_w >= 1 and ratio_h >= 1
new_vertices = np.zeros(vertices.shape)
if vertices.size > 0:
new_vertices[:, [0, 2, 4, 6]] = vertices[:, [0, 2, 4, 6]] * ratio_w
new_vertices[:, [1, 3, 5, 7]] = vertices[:, [1, 3, 5, 7]] * ratio_h
# find random position
remain_h = img.shape[0] - length
remain_w = img.shape[1] - length
flag = True
cnt = 0
while flag and cnt < 1000:
cnt += 1
start_w = int(np.random.rand() * remain_w)
start_h = int(np.random.rand() * remain_h)
flag = is_cross_text([start_w, start_h], length, new_vertices[labels == 1, :])
region = img[start_h : start_h + length, start_w : start_w + length]
if new_vertices.size == 0:
return region, new_vertices
new_vertices[:, [0, 2, 4, 6]] -= start_w
new_vertices[:, [1, 3, 5, 7]] -= start_h
return region, new_vertices
def rotate_all_pixels(rotate_mat, anchor_x, anchor_y, length):
"""get rotated locations of all pixels for next stages
Input:
rotate_mat: rotatation matrix
anchor_x : fixed x position
anchor_y : fixed y position
length : length of image
Output:
rotated_x : rotated x positions <numpy.ndarray, (length,length)>
rotated_y : rotated y positions <numpy.ndarray, (length,length)>
"""
x = np.arange(length)
y = np.arange(length)
x, y = np.meshgrid(x, y)
x_lin = x.reshape((1, x.size))
y_lin = y.reshape((1, x.size))
coord_mat = np.concatenate((x_lin, y_lin), 0)
rotated_coord = np.dot(
rotate_mat, coord_mat - np.array([[anchor_x], [anchor_y]])
) + np.array([[anchor_x], [anchor_y]])
rotated_x = rotated_coord[0, :].reshape(x.shape)
rotated_y = rotated_coord[1, :].reshape(y.shape)
return rotated_x, rotated_y
def resize_img(img, vertices, size):
h, w, _ = img.shape
ratio = size / max(h, w)
if w > h:
img = cv2.resize(img, (size, int(h * ratio)), interpolation=cv2.INTER_LINEAR)
else:
img = cv2.resize(img, (int(w * ratio), size), interpolation=cv2.INTER_LINEAR)
new_vertices = vertices * ratio
return img, new_vertices
def adjust_height(img, vertices, ratio=0.2):
"""adjust height of image to aug data
Input:
img : ndarray
vertices : vertices of text regions <numpy.ndarray, (n,8)>
ratio : height changes in [0.8, 1.2]
Output:
img : adjusted ndarray
new_vertices: adjusted vertices
"""
ratio_h = 1 + ratio * (np.random.rand() * 2 - 1)
old_h, width, _ = img.shape
new_h = int(np.around(old_h * ratio_h))
img = cv2.resize(img, (width, new_h), interpolation=cv2.INTER_LINEAR)
new_vertices = vertices.copy()
if vertices.size > 0:
new_vertices[:, [1, 3, 5, 7]] = vertices[:, [1, 3, 5, 7]] * (new_h / old_h)
return img, new_vertices
def rotate_img(img, vertices, angle_range=10):
"""rotate image [-10, 10] degree to aug data
Input:
img : ndarray
vertices : vertices of text regions <numpy.ndarray, (n,8)>
angle_range : rotate range
Output:
img : rotated ndarray
new_vertices: rotated vertices
"""
height, width, _ = img.shape
center_x = (width - 1) / 2
center_y = (height - 1) / 2
angle = angle_range * (np.random.rand() * 2 - 1)
M = cv2.getRotationMatrix2D((center_x, center_y), angle, 1.0)
img = cv2.warpAffine(img, M, (width, height), flags=cv2.INTER_LINEAR)
new_vertices = np.zeros(vertices.shape)
for i, vertice in enumerate(vertices):
new_vertices[i, :] = rotate_vertices(
vertice, -angle / 180 * math.pi, np.array([[center_x], [center_y]])
)
return img, new_vertices
def generate_roi_mask(image, vertices, labels):
mask = np.ones(image.shape[:2], dtype=np.float32)
ignored_polys = []
for vertice, label in zip(vertices, labels):
if label == 0:
ignored_polys.append(np.around(vertice.reshape((4, 2))).astype(np.int32))
cv2.fillPoly(mask, ignored_polys, 0)
return mask
def filter_vertices(vertices, labels, ignore_under=0, drop_under=0):
if drop_under == 0 and ignore_under == 0:
return vertices, labels
new_vertices, new_labels = vertices.copy(), labels.copy()
areas = np.array([Polygon(v.reshape((4, 2))).convex_hull.area for v in vertices])
labels[areas < ignore_under] = 0
if drop_under > 0:
passed = areas >= drop_under
new_vertices, new_labels = new_vertices[passed], new_labels[passed]
return new_vertices, new_labels
class SceneTextDataset(Dataset):
def __init__(
self,
root_dir,
split="train",
num=0,
color_jitter=True,
normalize=True,
):
if num == 0:
pkl_dir = osp.join(root_dir, "ufo/{}.pickle".format(split))
else:
pkl_dir = osp.join(root_dir, "ufo/{}.pickle".format(split + str(num)))
with open(pkl_dir, "rb") as fr:
total = pickle.load(fr)
self.images = total["images"]
self.vertices = total["vertices"]
self.labels = total["labels"]
self.color_jitter, self.normalize = color_jitter, normalize
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = self.images[idx]
vertices = self.vertices[idx]
labels = self.labels[idx]
funcs = []
if self.color_jitter:
funcs.append(A.ColorJitter(0.5, 0.5, 0.5, 0.25))
if self.normalize:
funcs.append(A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)))
transform = A.Compose(funcs)
image = transform(image=image)["image"]
word_bboxes = np.reshape(vertices, (-1, 4, 2))
roi_mask = generate_roi_mask(image, vertices, labels)
return image, word_bboxes, roi_mask
class SceneTextDataset_VAL(Dataset):
def __init__(self, root_dir,
image_fnames,
split='train',
image_size=2048,
crop_size=1024,
ignore_tags=[],
ignore_under_threshold=10,
drop_under_threshold=1,
color_jitter=True,
normalize=True,
transform=True,
train=True):
with open(osp.join(root_dir, 'ufo/{}.json'.format(split)), 'r') as f:
anno = json.load(f)
self.anno = anno
self.image_fnames = image_fnames
self.image_dir = osp.join(root_dir, 'img', split)
self.image_size, self.crop_size = image_size, crop_size
self.color_jitter, self.normalize = color_jitter, normalize
self.transform = transform
self.train = train
self.ignore_tags = ignore_tags
self.drop_under_threshold = drop_under_threshold
self.ignore_under_threshold = ignore_under_threshold
def __len__(self):
return len(self.image_fnames)
def __getitem__(self, idx):
image_fname = self.image_fnames[idx]
image_fpath = osp.join(self.image_dir, image_fname)
vertices, labels = [], []
for word_info in self.anno['images'][image_fname]['words'].values():
word_tags = word_info['tags']
ignore_sample = any(elem for elem in word_tags if elem in self.ignore_tags)
num_pts = np.array(word_info['points']).shape[0]
# skip samples with ignore tag and
# samples with number of points greater than 4
if ignore_sample or num_pts > 4:
continue
vertices.append(np.array(word_info['points']).flatten())
labels.append(int(not word_info['illegibility']))
vertices, labels = np.array(vertices, dtype=np.float32), np.array(labels, dtype=np.int64)
vertices, labels = filter_vertices(
vertices,
labels,
ignore_under=self.ignore_under_threshold,
drop_under=self.drop_under_threshold
)
image = Image.open(image_fpath)
if self.transform:
image, vertices = resize_img(image, vertices, self.image_size)
image, vertices = adjust_height(image, vertices)
image, vertices = rotate_img(image, vertices)
image, vertices = crop_img(image, vertices, labels, self.crop_size)
if image.mode != 'RGB':
image = image.convert('RGB')
image = np.array(image)
funcs = []
if self.color_jitter:
funcs.append(A.ColorJitter(0.5, 0.5, 0.5, 0.25))
if self.normalize:
funcs.append(A.Normalize())
transform = A.Compose(funcs)
word_bboxes = np.reshape(vertices, (-1, 4, 2))
roi_mask = generate_roi_mask(image, vertices, labels)
if self.train:
image = transform(image=image)['image']
return image, word_bboxes, roi_mask