forked from MTammvee/openpilot-supercombo-model
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenpilot_onnx.py
executable file
·233 lines (173 loc) · 7 KB
/
openpilot_onnx.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
import cv2
import json
import numpy as np
import onnxruntime
import pandas as pd
from matplotlib import pyplot as plt
X_IDXS = np.array([ 0. , 0.1875, 0.75 , 1.6875, 3. , 4.6875,
6.75 , 9.1875, 12. , 15.1875, 18.75 , 22.6875,
27. , 31.6875, 36.75 , 42.1875, 48. , 54.1875,
60.75 , 67.6875, 75. , 82.6875, 90.75 , 99.1875,
108. , 117.1875, 126.75 , 136.6875, 147. , 157.6875,
168.75 , 180.1875, 192.])
def parse_image(frame):
H = (frame.shape[0]*2)//3
W = frame.shape[1]
print(H,W)
parsed = np.zeros((6, H//2, W//2), dtype=np.float32)
parsed[0] = frame[0:H:2, 0::2]
parsed[1] = frame[1:H:2, 0::2]
parsed[2] = frame[0:H:2, 1::2]
parsed[3] = frame[1:H:2, 1::2]
parsed[4] = frame[H:H+H//4].reshape((-1, H//2,W//2))
parsed[5] = frame[H+H//4:H+H//2].reshape((-1, H//2,W//2))
return parsed
def seperate_points_and_std_values(df):
points = df.iloc[lambda x: x.index % 2 == 0]
std = df.iloc[lambda x: x.index % 2 != 0]
points = pd.concat([points], ignore_index = True)
std = pd.concat([std], ignore_index = True)
return points, std
def main():
model = "supercombo.onnx"
cap = cv2.VideoCapture('data/cropped_mini.mp4')
parsed_images = []
width = 512
height = 256
dim = (width, height)
#Model Output IDs
plan_start_idx = 0
plan_end_idx = 4955
lanes_start_idx = plan_end_idx
lanes_end_idx = lanes_start_idx + 528
lane_lines_prob_start_idx = lanes_end_idx
lane_lines_prob_end_idx = lane_lines_prob_start_idx + 8
road_start_idx = lane_lines_prob_end_idx
road_end_idx = road_start_idx + 264
# lead_start_idx = road_end_idx
# lead_end_idx = lead_start_idx + 55
#
# lead_prob_start_idx = lead_end_idx
# lead_prob_end_idx = lead_prob_start_idx + 3
#
# desire_start_idx = lead_prob_end_idx
# desire_end_idx = desire_start_idx + 72
#
# meta_start_idx = desire_end_idx
# meta_end_idx = meta_start_idx + 32
#
# desire_pred_start_idx = meta_end_idx
# desire_pred_end_idx = desire_pred_start_idx + 32
#
# pose_start_idx = desire_pred_end_idx
# pose_end_idx = pose_start_idx + 12
#
# rnn_start_idx = pose_end_idx
# rnn_end_idx = rnn_start_idx + 908
session = onnxruntime.InferenceSession(model, None)
while(cap.isOpened()):
ret, frame = cap.read()
if (ret == False):
print('Return False')
break
if frame is not None:
img = cv2.resize(frame, dim)
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV_I420)
parsed = parse_image(img_yuv)
if (len(parsed_images) >= 2):
del parsed_images[0]
parsed_images.append(parsed)
if (len(parsed_images) >= 2):
print("Parsed Images: ", len(parsed_images))
parsed_arr = np.array(parsed_images)
parsed_arr.resize((1,12,128,256))
input_imgs = session.get_inputs()[0].name
big_input_imgs = session.get_inputs()[1].name
desire = session.get_inputs()[2].name
traffic_convention = session.get_inputs()[3].name
nav_features = session.get_inputs()[4].name
nav_instructions = session.get_inputs()[5].name
features_buffer = session.get_inputs()[6].name
output_name = session.get_outputs()[0].name
data = json.dumps({'data': parsed_arr.tolist()})
data = np.array(json.loads(data)['data']).astype('float16')
big_input_imgs_data = np.zeros((1,12,128,256),dtype=np.float16)
desire_data = np.array([0]).astype('float16')
desire_data.resize((1,100,8))
traffic_convention_data = np.array([0]).astype('float16')
traffic_convention_data.resize((1,2))
initial_state_data = np.array([0]).astype('float16')
initial_state_data.resize((1,2))
nav_features_data = np.zeros((1,256),dtype=np.float16)
nav_instructions_data = np.zeros((1,150),dtype=np.float16)
features_buffer_data = np.zeros((1,99,512),dtype=np.float16)
result = session.run([output_name], {input_imgs: data,
big_input_imgs :big_input_imgs_data,
desire: desire_data,
traffic_convention: traffic_convention_data,
nav_features: nav_features_data,
nav_instructions : nav_instructions_data,
features_buffer : features_buffer_data
})
res = np.array(result)
# plan = res[:,:,plan_start_idx:plan_end_idx]
lanes = res[:,:,lanes_start_idx:lanes_end_idx]
# lane_lines_prob = res[:,:,lane_lines_prob_start_idx:lane_lines_prob_end_idx]
lane_road = res[:,:,road_start_idx:road_end_idx]
# lead = res[:,:,lead_start_idx:lead_end_idx]
# lead_prob = res[:,:,lead_prob_start_idx:lead_prob_end_idx]
# desire_state = res[:,:,desire_start_idx:desire_end_idx]
# meta = res[:,:,meta_start_idx:meta_end_idx]
# desire_pred = res[:,:,desire_pred_start_idx:desire_pred_end_idx]
# pose = res[:,:,pose_start_idx:pose_end_idx]
# recurrent_layer = res[:,:,rnn_start_idx:rnn_end_idx]
lanes_flat = lanes.flatten()
df_lanes = pd.DataFrame(lanes_flat)
ll_t = df_lanes[0:66]
ll_t2 = df_lanes[66:132]
points_ll_t, std_ll_t = seperate_points_and_std_values(ll_t)
points_ll_t2, std_ll_t2 = seperate_points_and_std_values(ll_t2)
l_t = df_lanes[132:198]
l_t2 = df_lanes[198:264]
points_l_t, std_l_t = seperate_points_and_std_values(l_t)
points_l_t2, std_l_t2 = seperate_points_and_std_values(l_t2)
r_t = df_lanes[264:330]
r_t2 = df_lanes[330:396]
points_r_t, std_r_t = seperate_points_and_std_values(r_t)
points_r_t2, std_r_t2 = seperate_points_and_std_values(r_t2)
rr_t = df_lanes[396:462]
rr_t2 = df_lanes[462:528]
points_rr_t, std_rr_t = seperate_points_and_std_values(rr_t)
points_rr_t2, std_rr_t2 = seperate_points_and_std_values(rr_t2)
road_flat = lane_road.flatten()
df_road = pd.DataFrame(road_flat)
roadr_t = df_road[0:66]
roadr_t2 = df_road[66:132]
points_road_t, std_ll_t = seperate_points_and_std_values(roadr_t)
points_road_t2, std_ll_t2 = seperate_points_and_std_values(roadr_t2)
roadl_t = df_road[132:198]
roadl_t2 = df_road[198:264]
points_roadl_t, std_rl_t = seperate_points_and_std_values(roadl_t)
points_roadl_t2, std_rl_t2 = seperate_points_and_std_values(roadl_t2)
middle = points_ll_t2.add(points_l_t, fill_value=0) / 2
plt.scatter(middle, X_IDXS, color = "g")
# plt.scatter(points_ll_t, X_IDXS, color = "b", marker = "*")
plt.scatter(points_ll_t2, X_IDXS, color = "y")
plt.scatter(points_l_t, X_IDXS, color = "y")
# plt.scatter(points_l_t2, X_IDXS, color = "y", marker = "*")
plt.scatter(points_road_t, X_IDXS, color = "r")
plt.scatter(points_road_t2, X_IDXS, color = "r")
plt.title("Raod lines")
plt.xlabel("red - road lines | green - predicted path | yellow - lane lines")
plt.ylabel("Range")
plt.show()
plt.pause(0.1)
plt.clf()
frame = cv2.resize(frame, (900, 500))
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()