-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinet_utils.py
169 lines (137 loc) · 5.4 KB
/
pinet_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
from typing import List
import cv2
from copy import deepcopy
import numpy as np
from configs.PINet.network import NetworkParameters
p = NetworkParameters()
grid_location = np.zeros((p.grid_y, p.grid_x, 2)) # anchor template
for y in range(p.grid_y):
for x in range(p.grid_x):
grid_location[y][x][0] = x
grid_location[y][x][1] = y
###############################################################
##
## 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(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(p.grid_y):
for x in range(p.grid_x):
if gt_point[0][y][x] > 0:
xx = int(gt_point[1][y][x] * p.resize_ratio + p.resize_ratio * x)
yy = int(gt_point[2][y][x] * p.resize_ratio + p.resize_ratio * y)
image = cv2.circle(image, (xx, yy), 10, 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 write_result_json(result_data, x, y, testset_index):
for i in x:
result_data[testset_index]['lanes'].append(i)
result_data[testset_index]['run_time'] = 1
return result_data
############################################################################
## linear interpolation for fixed y value on the test dataset
############################################################################
def find_target(x, y, target_h, ratio_w, ratio_h):
# find exact points on target_h
out_x = []
out_y = []
x_size = p.x_size / ratio_w
y_size = p.y_size / ratio_h
for i, j in zip(x, y):
min_y = min(j)
max_y = max(j)
temp_x = []
temp_y = []
for h in target_h:
temp_y.append(h)
if h < min_y:
temp_x.append(-2)
elif min_y <= h and h <= max_y:
for k in range(len(j) - 1):
if j[k] >= h and h >= j[k + 1]:
# linear regression
if i[k] < i[k + 1]:
temp_x.append(int(i[k + 1] - float(abs(j[k + 1] - h)) * abs(i[k + 1] - i[k]) / abs(
j[k + 1] + 0.0001 - j[k])))
else:
temp_x.append(int(i[k + 1] + float(abs(j[k + 1] - h)) * abs(i[k + 1] - i[k]) / abs(
j[k + 1] + 0.0001 - j[k])))
break
else:
if i[0] < i[1]:
l = int(i[1] - float(-j[1] + h) * abs(i[1] - i[0]) / abs(j[1] + 0.0001 - j[0]))
if l > x_size or l < 0:
temp_x.append(-2)
else:
temp_x.append(l)
else:
l = int(i[1] + float(-j[1] + h) * abs(i[1] - i[0]) / abs(j[1] + 0.0001 - j[0]))
if l > x_size or l < 0:
temp_x.append(-2)
else:
temp_x.append(l)
out_x.append(temp_x)
out_y.append(temp_y)
return out_x, out_y