forked from koyeongmin/PINet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
340 lines (284 loc) · 10.7 KB
/
util.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
import torch.nn as nn
import cv2
import torch
from copy import deepcopy
import numpy as np
from torch.autograd import Variable
from torch.autograd import Function as F
from parameters import Parameters
import math
p = Parameters()
###############################################################
##
## visualize
##
###############################################################
def visualize_points(image, x, y):
image = image
image = np.rollaxis(image, axis=2, start=0)
image = np.rollaxis(image, axis=2, start=0)*255.0
image = image.astype(np.uint8).copy()
for k in range(len(y)):
for i, j in zip(x[k], y[k]):
if i > 0:
image = cv2.circle(image, (int(i), int(j)), 5, p.color[1], -1)
cv2.imshow("test2", image)
cv2.waitKey(0)
def visualize_points_origin_size(x, y, test_image, ratio_w, ratio_h):
color = 0
image = deepcopy(test_image)
image = np.rollaxis(image, axis=2, start=0)
image = np.rollaxis(image, axis=2, start=0)*255.0
image = image.astype(np.uint8).copy()
image = cv2.resize(image, (int(p.x_size/ratio_w), int(p.y_size/ratio_h)))
for i, j in zip(x, y):
color += 1
for index in range(len(i)):
cv2.circle(image, (int(i[index]), int(j[index])), 10, p.color[color], -1)
cv2.imshow("test2", image)
cv2.waitKey(0)
return test_image
def visualize_gt(self, gt_point, gt_instance, ground_angle, image):
image = np.rollaxis(image, axis=2, start=0)
image = np.rollaxis(image, axis=2, start=0)*255.0
image = image.astype(np.uint8).copy()
for y in range(self.p.grid_y):
for x in range(self.p.grid_x):
if gt_point[0][y][x] > 0:
xx = int(gt_point[1][y][x]*self.p.resize_ratio+self.p.resize_ratio*x)
yy = int(gt_point[2][y][x]*self.p.resize_ratio+self.p.resize_ratio*y)
image = cv2.circle(image, (xx, yy), 10, self.p.color[1], -1)
cv2.imshow("image", image)
cv2.waitKey(0)
def visualize_regression(image, gt):
image = np.rollaxis(image, axis=2, start=0)
image = np.rollaxis(image, axis=2, start=0)*255.0
image = image.astype(np.uint8).copy()
for i in gt:
for j in range(p.regression_size):#gt
y_value = p.y_size - (p.regression_size-j)*(220/p.regression_size)
if i[j] >0:
x_value = int(i[j]*p.x_size)
image = cv2.circle(image, (x_value, y_value), 5, p.color[1], -1)
cv2.imshow("image", image)
cv2.waitKey(0)
def draw_points(x, y, image):
color_index = 0
for i, j in zip(x, y):
color_index += 1
if color_index > 12:
color_index = 12
for index in range(len(i)):
image = cv2.circle(image, (int(i[index]), int(j[index])), 5, p.color[color_index], -1)
return image
###############################################################
##
## calculate
##
###############################################################
def convert_to_original_size(x, y, ratio_w, ratio_h):
# convert results to original size
out_x = []
out_y = []
for i, j in zip(x,y):
out_x.append((np.array(i)/ratio_w).tolist())
out_y.append((np.array(j)/ratio_h).tolist())
return out_x, out_y
def get_closest_point_along_angle(x, y, point, angle):
index = 0
for i, j in zip(x, y):
a = get_angle_two_points(point, (i,j))
if abs(a-angle) < 0.1:
return (i, j), index
index += 1
return (-1, -1), -1
def get_num_along_point(x, y, point1, point2, image=None): # point1 : source
x = np.array(x)
y = np.array(y)
x = x[y<point1[1]]
y = y[y<point1[1]]
dis = np.sqrt( (x - point1[0])**2 + (y - point1[1])**2 )
count = 0
shortest = 1000
target_angle = get_angle_two_points(point1, point2)
for i in range(len(dis)):
angle = get_angle_two_points(point1, (x[i], y[i]))
diff_angle = abs(angle-target_angle)
distance = dis[i] * math.sin( diff_angle*math.pi*2 )
if distance <= 12:
count += 1
if distance < shortest:
shortest = distance
return count, shortest
def get_closest_upper_point(x, y, point, n):
x = np.array(x)
y = np.array(y)
x = x[y<point[1]]
y = y[y<point[1]]
dis = (x - point[0])**2 + (y - point[1])**2
ind = np.argsort(dis, axis=0)
x = np.take_along_axis(x, ind, axis=0).tolist()
y = np.take_along_axis(y, ind, axis=0).tolist()
points = []
for i, j in zip(x[:n], y[:n]):
points.append((i,j))
return points
def get_angle_two_points(p1, p2):
del_x = p2[0] - p1[0]
del_y = p2[1] - p1[1] + 0.000001
if p2[0] >= p1[0] and p2[1] > p1[1]:
theta = math.atan(float(del_x/del_y))*180/math.pi
theta /= 360.0
elif p2[0] > p1[0] and p2[1] <= p1[1]:
theta = math.atan(float(del_x/del_y))*180/math.pi
theta += 180
theta /= 360.0
elif p2[0] <= p1[0] and p2[1] < p1[1]:
theta = math.atan(float(del_x/del_y))*180/math.pi
theta += 180
theta /= 360.0
elif p2[0] < p1[0] and p2[1] >= p1[1]:
theta = math.atan(float(del_x/del_y))*180/math.pi
theta += 360
theta /= 360.0
return theta
def get_angle_on_lane(x, y):
sorted_x = None
sorted_y = None
angle = []
# sort
ind = np.argsort(y, axis=0)
sorted_x = np.take_along_axis(x, ind[::-1], axis=0)
sorted_y = np.take_along_axis(y, ind[::-1], axis=0)
# calculate angle
length = len(x)
theta = -2
for i in range(length-1):
if sorted_x[i] < 0 :
angle.append(-2)
else:
p1 = (sorted_x[i], sorted_y[i])
for index, j in enumerate(sorted_x[i+1:]):
if j > 0:
p2 = (sorted_x[i+1+index], sorted_y[i+1+index])
break
else:
p2 = (-2, -2)
if p2[0] < 0:
angle.append(theta)
continue
theta = get_angle_two_points(p1, p2)
angle.append(theta)
angle.append(theta)
return angle
def sort_along_y(x, y):
out_x = []
out_y = []
for i, j in zip(x, y):
i = np.array(i)
j = np.array(j)
ind = np.argsort(j, axis=0)
out_x.append(np.take_along_axis(i, ind[::-1], axis=0).tolist())
out_y.append(np.take_along_axis(j, ind[::-1], axis=0).tolist())
return out_x, out_y
def sort_along_x(x, y):
out_x = []
out_y = []
for i, j in zip(x, y):
i = np.array(i)
j = np.array(j)
ind = np.argsort(i, axis=0)
out_x.append(np.take_along_axis(i, ind[::-1], axis=0).tolist())
out_y.append(np.take_along_axis(j, ind[::-1], axis=0).tolist())
return out_x, out_y
def sort_batch_along_y(target_lanes, target_h):
out_x = []
out_y = []
for x_batch, y_batch in zip(target_lanes, target_h):
temp_x = []
temp_y = []
for x, y, in zip(x_batch, y_batch):
ind = np.argsort(y, axis=0)
sorted_x = np.take_along_axis(x, ind[::-1], axis=0)
sorted_y = np.take_along_axis(y, ind[::-1], axis=0)
temp_x.append(sorted_x)
temp_y.append(sorted_y)
out_x.append(temp_x)
out_y.append(temp_y)
return out_x, out_y
'''
def find_x_value(x, y, y_value):
min_y = min(y[x>0])
max_y = max(y[x>0])
target_h = y[x>0]
target_lanes = x[x>0]
if y_value < min_y:
if target_lanes[-2] < target_lanes[-1]:
l = target_lanes[-2] + abs(-target_h[-2] + y_value)*abs(target_lanes[-2]-target_lanes[-1])/abs(target_h[-2]+0.0001 - target_h[-1])
else:
l = target_lanes[-2] - abs(-target_h[-2] + y_value)*abs(target_lanes[-2]-target_lanes[-1])/abs(target_h[-2]+0.0001 - target_h[-1])
if l > p.x_size or l < 0 :
return -2
else:
return l
elif min_y <= y_value and y_value <= max_y:
for k in range(len(target_h)-1):
if target_h[k] >= y_value and y_value >= target_h[k+1]:
#linear regression
if target_lanes[k] < target_lanes[k+1]:
return target_lanes[k+1] - float(abs(target_h[k+1] - y_value))*abs(target_lanes[k+1]-target_lanes[k])/abs(target_h[k+1]+0.0001 - target_h[k])
else:
return target_lanes[k+1] + float(abs(target_h[k+1] - y_value))*abs(target_lanes[k+1]-target_lanes[k])/abs(target_h[k+1]+0.0001 - target_h[k])
break
else:
if target_lanes[1] < target_lanes[0]:
l = target_lanes[1] + abs(-target_h[1] + y_value)*abs(target_lanes[1]-target_lanes[0])/abs(target_h[1]+0.0001 - target_h[0])
else:
l = target_lanes[1] - abs(-target_h[1] + y_value)*abs(target_lanes[1]-target_lanes[0])/abs(target_h[1]+0.0001 - target_h[0])
if l > p.x_size or l < 0 :
return -2
else:
return l
def find_x_value_target(target_lanes, target_h, y_value):
min_y = min(target_h[target_lanes>0])
max_y = max(target_h[target_lanes>0])
if y_value < min_y:
return -2
elif min_y <= y_value and y_value <= max_y:
for k in range(len(target_h)-1):
if target_h[k] >= y_value and y_value >= target_h[k+1]:
#linear regression
if target_lanes[k] < target_lanes[k+1]:
return target_lanes[k+1] - float(abs(target_h[k+1] - y_value))*abs(target_lanes[k+1]-target_lanes[k])/abs(target_h[k+1]+0.0001 - target_h[k])
else:
return target_lanes[k+1] + float(abs(target_h[k+1] - y_value))*abs(target_lanes[k+1]-target_lanes[k])/abs(target_h[k+1]+0.0001 - target_h[k])
break
else:
if target_lanes[0] < target_lanes[1]:
l = target_lanes[1] - abs(-target_h[1] + y_value)*abs(target_lanes[1]-target_lanes[0])/abs(target_h[1]+0.0001 - target_h[0])
if l > p.x_size or l < 0 :
return -2
else:
return l
else:
l = target_lanes[1] + abs(-target_h[1] + y_value)*abs(target_lanes[1]-target_lanes[0])/abs(target_h[1]+0.0001 - target_h[0])
if l > p.x_size or l < 0 :
return -2
else:
return l
def find_gt(x, y, target_lanes, target_h):
ind = np.argsort(y, axis=0)
min_x = np.take_along_axis(x, ind[::-1], axis=0).tolist()[0]
min_y = np.take_along_axis(y, ind[::-1], axis=0).tolist()[0]
index = 0
dis = 10000
closest_index = 0
for i, j in zip(target_lanes, target_h):
if i[0] > 0:
temp = (i[0]-min_x)**2 + (j[0]-min_y)**2
if temp < dis:
dis = temp
closest_index = index
index += 1
return closest_index
'''