forked from EuphoriaYan/DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
283 lines (250 loc) · 9.86 KB
/
utils.py
File metadata and controls
283 lines (250 loc) · 9.86 KB
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
# -*- coding: utf-8 -*-
import itertools
from functools import reduce
import numpy as np
import cv2
import math
import collections
from PIL import Image
from typing import List
from matplotlib import pyplot as plt
from sklearn.cluster import DBSCAN, KMeans, MeanShift, OPTICS, Birch, SpectralClustering, AgglomerativeClustering
# (n, 2) poly -> box_dict
def trans_poly_to_rec(idx, poly):
# poly = np.array(poly).reshape(-1, 2)
poly = poly.tolist()
l = min([i[0] for i in poly])
r = max([i[0] for i in poly])
u = min([i[1] for i in poly])
d = max([i[1] for i in poly])
Rec = collections.namedtuple('Rec', 'l r u d idx')
rec = Rec(l, r, u, d, idx)
return rec
def cluster_recs_with_lr(recs, type='DBSCAN'):
switch = {
'DBSCAN': DBSCAN(min_samples=1, eps=0.012),
'MeanShift': MeanShift(bandwidth=0.3),
'OPTICS': OPTICS(min_samples=1, eps=20),
'Birch': Birch(n_clusters=None)
}
try:
cluster = switch[type]
except ValueError as e:
raise ValueError('type should be DBSCAN, MeanShift, OPTICS or Birch')
recs_data = [[rec.l, rec.r] for rec in recs]
recs_data = np.array(recs_data)
recs_min, recs_max = recs_data.min(), recs_data.max()
recs_data = (recs_data - recs_min) / (recs_max - recs_min)
labels = cluster.fit_predict(recs_data)
# plt.scatter(recs_data[:, 0], recs_data[:, 1], s=1, c=labels)
# plt.show()
classified_box_ids = collections.defaultdict(list)
for idx, label in enumerate(labels):
classified_box_ids[label].append(idx)
return classified_box_ids
def cal_dis(x1, y1, x2, y2):
return np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def cluster_recs_with_width(recs, boxes, type='Birch', n_clusters=None):
switch = {
'KMeans': KMeans(n_clusters=n_clusters),
'Birch': Birch(n_clusters=n_clusters),
'SpectralClustering': SpectralClustering(n_clusters=n_clusters),
'AgglomerativeClustering_ward': AgglomerativeClustering(n_clusters=n_clusters, linkage='ward'),
'AgglomerativeClustering_complete': AgglomerativeClustering(n_clusters=n_clusters, linkage='complete'),
}
try:
cluster = switch[type]
except ValueError as e:
raise ValueError('type should be KMeans, Birch, SpectralClustering, AgglomerativeClustering')
recs_data = [cal_dis(box[0][0], box[0][1], box[1][0], box[1][1]) for box in boxes]
recs_data = np.array(recs_data).reshape(-1, 1)
# recs_max = np.max(recs_data)
# recs_data = recs_data / recs_max * 5
labels = cluster.fit_predict(recs_data)
# plt.scatter(recs_data[:], recs_data[:], s=1, c=labels)
# plt.show()
classified_box_ids = collections.defaultdict(list)
for idx, label in enumerate(labels):
classified_box_ids[label].append(idx)
return classified_box_ids
def check_one_over_two(cur, nxt, recs, cover_threshold=0.3):
cur_l = np.mean([i.l for i in cur])
cur_r = np.mean([i.r for i in cur])
nxt_l = np.mean([i.l for i in nxt])
nxt_r = np.mean([i.r for i in nxt])
cur_len = cur_r - cur_l
nxt_len = nxt_r - nxt_l
cover = min(cur_r, nxt_r) - max(cur_l, nxt_l)
if nxt_len * 1.4 <= cur_len and cover > cover_threshold * nxt_len:
return True
else:
return False
def read_out(classified_recs, recs, cover_threshold, bigger_idx=None):
output_idx = []
total_clusters = len(classified_recs)
for i in range(total_clusters):
# i - 1 can't be one-column.
if classified_recs[i]:
# check if cur cluster is one-column and any of next clusters is two-column
cur = classified_recs[i]
# weight = np.mean([0.5 * (i.r - i.l) + i.r for i in cur])
flag = False
for rec in cur:
if rec.idx in bigger_idx:
flag = True
break
if not flag:
while classified_recs[i]:
output_idx.append(classified_recs[i].pop(0))
continue
nxt_list = []
for j in range(1, 5):
if i + j > total_clusters - 1:
break
nxt = classified_recs[i + j]
if nxt and check_one_over_two(cur, nxt, recs, cover_threshold):
nxt_list.append(nxt)
if not nxt_list:
while classified_recs[i]:
output_idx.append(classified_recs[i].pop(0))
continue
while cur or reduce(lambda x, y: x or y, nxt_list, False):
if not cur:
for nxt in nxt_list:
while nxt:
output_idx.append(nxt.pop(0))
break
cur_u = cur[0].u
for nxt in nxt_list:
while nxt and nxt[0].u < cur_u:
output_idx.append(nxt.pop(0))
output_idx.append(cur.pop(0))
else:
continue
return output_idx
def check_cover(cur, nxt, cover_threshold=0.3):
cur_len = cur.r - cur.l
nxt_len = nxt.r - nxt.l
cover = min(cur.r, nxt.r) - max(cur.l, nxt.l)
if cover > cover_threshold * nxt_len and cover > cover_threshold * cur_len:
return True
else:
return False
def check_two_column(cur, nxt, cover_threshold=0.3):
cur_len = cur.r - cur.l
nxt_len = nxt.r - nxt.l
cover = min(cur.r, nxt.r) - max(cur.l, nxt.l)
if cover > cover_threshold * nxt_len and cur_len > nxt_len:
return True
else:
return False
def read_out_2(recs, bigger_idx=None, sort_hp=0.02):
output_idx = []
recs = sorted(recs, key=lambda x: x.r - sort_hp * x.u, reverse=True)
rec_cnt = len(recs)
vis = [False for _ in range(rec_cnt)]
while len(output_idx) < rec_cnt:
# cur = 0
for i in range(rec_cnt):
if vis[i]:
continue
cur = recs[i]
vis[i] = True
break
# one-column state, find the same position one-column, and find two-column between them.
if cur.idx in bigger_idx:
one_column_list = [cur]
for i in range(rec_cnt):
if vis[i]:
continue
nxt = recs[i]
if nxt.idx not in bigger_idx:
continue
if check_cover(cur, nxt, cover_threshold=0.7):
one_column_list.append(nxt)
vis[i] = True
two_column_list = []
for i in range(rec_cnt):
if vis[i]:
continue
nxt = recs[i]
if nxt.idx in bigger_idx:
continue
if check_one_over_two(one_column_list, [nxt], recs):
two_column_list.append(nxt)
vis[i] = True
while one_column_list or two_column_list:
cur_nxt = None
if one_column_list:
output_idx.append(one_column_list.pop(0))
if one_column_list:
cur_nxt = one_column_list[0]
else:
cur_nxt = None
if two_column_list:
if cur_nxt is not None:
new_two_column_list = []
for rec in two_column_list:
if rec.u < cur_nxt.u:
output_idx.append(rec)
else:
new_two_column_list.append(rec)
two_column_list = new_two_column_list
else:
output_idx.extend(two_column_list)
two_column_list = []
# two-column state, find the cover one-column, and find two-column been covered.
else:
one_column_list = []
two_column_list = [cur]
for i in range(rec_cnt):
if vis[i]:
continue
nxt = recs[i]
if nxt.idx not in bigger_idx:
continue
if check_two_column(nxt, cur):
one_column_list.append(nxt)
vis[i] = True
for i in range(rec_cnt):
if vis[i]:
continue
nxt = recs[i]
if nxt.idx in bigger_idx:
continue
if one_column_list and check_one_over_two(one_column_list, [nxt], recs):
two_column_list.append(nxt)
vis[i] = True
while one_column_list or two_column_list:
if one_column_list:
cur_nxt = one_column_list[0]
else:
cur_nxt = None
if two_column_list:
if cur_nxt is not None:
new_two_column_list = []
for rec in two_column_list:
if rec.u < cur_nxt.u:
output_idx.append(rec)
else:
new_two_column_list.append(rec)
two_column_list = new_two_column_list
else:
output_idx.extend(two_column_list)
two_column_list = []
if one_column_list:
output_idx.append(one_column_list.pop(0))
return output_idx
def list_sort(box_list, cover_threshold=0.6):
r = np.mean([b.r for b in box_list])
length = np.mean([b.r - b.l for b in box_list])
return r + length * cover_threshold
def width_sort(box_list):
width = np.mean([b.r - b.l for b in box_list])
return width
def box_sort(box):
return box.u - box.r
def cv2read(path, mode=cv2.IMREAD_COLOR):
return cv2.imdecode(np.fromfile(path, dtype=np.uint8), mode)
def cv2save(img, path):
cv2.imencode(".jpg", img)[1].tofile(path)