-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
212 lines (166 loc) · 6.03 KB
/
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
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()
def cross_entropy2d(inputs, target, weight=None, size_average=True):
loss = torch.nn.CrossEntropyLoss()
n, c, h, w = inputs.size()
prediction = inputs.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
gt =target.transpose(1, 2).transpose(2, 3).contiguous().view(-1)
return loss(prediction, gt)
###############################################################
##
## 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)), 2, 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 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