forked from opconty/pixellink_keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixellink_utils.py
236 lines (181 loc) · 6.25 KB
/
pixellink_utils.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
#-*- coding:utf-8 -*-
#'''
# Created on 18-10-15
#
# @Author: Greg Gao(laygin)
#'''
import cv2
import numpy as np
def resize_image(im, max_side_len=2400):
'''
resize image to a size multiple of 32 which is required by the network
:param im: the resized image
:param max_side_len: limit of max image size to avoid out of memory in gpu
:return: the resized image and the resize ratio
'''
h, w, _ = im.shape
resize_w = w
resize_h = h
# limit the max side
if max(resize_h, resize_w) > max_side_len:
ratio = float(max_side_len) / resize_h if resize_h > resize_w else float(max_side_len) / resize_w
else:
ratio = 1.
resize_h = int(resize_h * ratio)
resize_w = int(resize_w * ratio)
resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 - 1) * 32
resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 - 1) * 32
im = cv2.resize(im, (int(resize_w), int(resize_h)))
ratio_h = resize_h / float(h)
ratio_w = resize_w / float(w)
return im, ratio_h, ratio_w
def softmax(X, theta=1.0, axis=None):
# make X at least 2d
y = np.atleast_2d(X)
# find axis
if axis is None:
axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)
# multiply y against the theta parameter,
y = y * float(theta)
# subtract the max for numerical stability
y = y - np.expand_dims(np.max(y, axis=axis), axis)
# exponentiate y
y = np.exp(y)
# take the sum along the specified axis
ax_sum = np.expand_dims(np.sum(y, axis=axis), axis)
# finally: divide elementwise
p = y / ax_sum
# flatten if X was 1D
if len(X.shape) == 1: p = p.flatten()
return p
def get_neighbours_8(x, y):
return [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1),
(x - 1, y), (x + 1, y),
(x - 1, y + 1), (x, y + 1), (x + 1, y + 1)]
def is_valid_cord(x, y, w, h):
return x >= 0 and x < w and y >= 0 and y < h
def decode_image_by_join(pixel_scores, link_scores, pixel_conf_threshold, link_conf_threshold):
pixel_mask = pixel_scores >= pixel_conf_threshold
link_mask = link_scores >= link_conf_threshold
points = zip(*np.where(pixel_mask))
h, w = np.shape(pixel_mask)
group_mask = dict.fromkeys(points, -1)
def find_parent(point):
return group_mask[point]
def set_parent(point, parent):
group_mask[point] = parent
def is_root(point):
return find_parent(point) == -1
def find_root(point):
root = point
update_parent = False
while not is_root(root):
root = find_parent(root)
update_parent = True
# for acceleration of find_root
if update_parent:
set_parent(point, root)
return root
def join(p1, p2):
root1 = find_root(p1)
root2 = find_root(p2)
if root1 != root2:
set_parent(root1, root2)
def get_all():
root_map = {}
def get_index(root):
if root not in root_map:
root_map[root] = len(root_map) + 1
return root_map[root]
mask = np.zeros_like(pixel_mask, dtype=np.int32)
points = zip(*np.where(pixel_mask))
for point in points:
point_root = find_root(point)
bbox_idx = get_index(point_root)
mask[point] = bbox_idx
return mask
points = zip(*np.where(pixel_mask))
for point in points:
y, x = point
neighbours = get_neighbours_8(x, y)
for n_idx, (nx, ny) in enumerate(neighbours):
if is_valid_cord(nx, ny, w, h):
# print(nx, ny, y, x, n_idx)
link_value = link_mask[y, x, n_idx]
pixel_cls = pixel_mask[ny, nx]
if link_value and pixel_cls:
join(point, (ny, nx))
mask = get_all()
return mask
def decode_batch(pixel_cls_scores, pixel_link_scores,
pixel_conf_threshold=None, link_conf_threshold=None):
if pixel_conf_threshold is None:
pixel_conf_threshold = 0.6
if link_conf_threshold is None:
link_conf_threshold = 0.9
batch_size = pixel_cls_scores.shape[0]
batch_mask = []
for image_idx in range(batch_size):
image_pos_pixel_scores = pixel_cls_scores[image_idx, :, :, 1]
image_pos_link_scores = pixel_link_scores[image_idx, :, :, :, 1]
mask = decode_image_by_join(
image_pos_pixel_scores, image_pos_link_scores,
pixel_conf_threshold, link_conf_threshold
)
batch_mask.append(mask)
return np.asarray(batch_mask, np.int32)
def rect_to_xys(rect, image_shape):
h, w = image_shape[0:2]
def get_valid_x(x):
if x < 0:
return 0
if x >= w:
return w - 1
return x
def get_valid_y(y):
if y < 0:
return 0
if y >= h:
return h - 1
return y
rect = ((rect[0], rect[1]), (rect[2], rect[3]), rect[4])
points = cv2.boxPoints(rect)
points = np.int0(points)
for i_xy, (x, y) in enumerate(points):
x = get_valid_x(x)
y = get_valid_y(y)
points[i_xy, :] = [x, y]
points = np.reshape(points, -1)
return points
def min_area_rect(cnt):
rect = cv2.minAreaRect(cnt)
cx, cy = rect[0]
w, h = rect[1]
theta = rect[2]
box = [cx, cy, w, h, theta]
return box, w * h
def mask_to_bboxes(mask, image_shape=None, min_area=None,
min_height=None, min_aspect_ratio=None):
image_h, image_w = image_shape[0:2]
if min_area is None:
min_area = 300
if min_height is None:
min_height = 10
bboxes = []
max_bbox_idx = mask.max()
mask = cv2.resize(mask, (image_w, image_h), interpolation=cv2.INTER_NEAREST)
for bbox_idx in range(1, max_bbox_idx + 1):
bbox_mask = mask == bbox_idx
_, cnts, _ = cv2.findContours(bbox_mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(cnts) == 0:
continue
cnt = cnts[0]
rect, rect_area = min_area_rect(cnt)
w, h = rect[2:-1]
if min(w, h) < min_height:
continue
if rect_area < min_area:
continue
xys = rect_to_xys(rect, image_shape)
bboxes.append(xys)
return bboxes