-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracking_head_angle
268 lines (226 loc) · 10.1 KB
/
Tracking_head_angle
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
########################### TrackingModule ###########################
import numpy as np
from numpy.linalg import inv, cholesky
import math as mt
from matplotlib import pyplot as plt
class track:
# Initialization
def __init__(self, centroid, box, frame_num, cluster_id):
self.state = centroid
self.state = np.insert(self.state,2,0.5)
self.state = np.insert(self.state,3,0.5)
self.state = np.insert(self.state,5,0.1)
self.box = box
self.P = np.array([[0.3, 0, 0, 0, 0, 0],
[0, 0.3, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0.3, 0],
[0, 0, 0, 0, 0, 1]])
self.Q = np.array([[0.2, 0, 0, 0, 0, 0],
[0, 0.2, 0, 0, 0, 0],
[0, 0, 0.1, 0, 0, 0],
[0, 0, 0, 0.1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]])
self.R = np.array([[0.01, 0, 0, 0, 0],
[0, 0.01, 0, 0, 0],
[0, 0, 0.5, 0, 0],
[0, 0, 0, 0.5, 0],
[0, 0, 0, 0, 0.2]])
self.width_max = 2
self.length_max = 4
self.processed = 0
self.kappa = 0
self.alpha = 0.3
self.Age = 1
self.ClusterID = cluster_id
self.Start = frame_num
self.Activated = 0
self.DelCnt = 0
self.history_state = np.empty([0,6])
self.history_box = np.empty([0,3])
self.dead_flag = 0
def sigma_points(self,P):
# Should We take kappa len - 3 because of wid, len, hei term ??
# wid, len, hei term -> not in the kalman filter but in the LPF ??
n = len(self.state)
Xi = np.zeros((n, 2*n+1))
W = np.zeros(2*n+1)
self.kappa=20
Xi[:, 0] = self.state
W[0] = self.kappa / (n + self.kappa)
U = cholesky((n + self.kappa)*P)
for i in range(n):
Xi[:, i+1] = self.state + U[:, i]
Xi[:, n+i+1] = self.state - U[:, i]
W[i+1] = 1 / (2*(n+self.kappa))
W[n+i+1] = W[i+1]
return Xi, W
self.Start = frame_num
self.Activated = 0
self.DelCnt = 0
self.history_state = np.empty([0,6])
self.history_box = np.empty([0,3])
self.dead_flag = 0
def sigma_points(self,P):
# Should We take kappa len - 3 because of wid, len, hei term ??
# wid, len, hei term -> not in the kalman filter but in the LPF ??
n = len(self.state)
Xi = np.zeros((n, 2*n+1))
W = np.zeros(2*n+1)
def UT(self,Xi, W, noiseCov):
mean = np.sum(W * Xi, axis=1)
cov = W * (Xi - mean.reshape(-1, 1)) @ (Xi - mean.reshape(-1, 1)).T
return mean, cov + noiseCov
def fx(self,Xi, dt):
''' cosy=mt.cos(self.state[4])
siny=mt.sin(self.state[4])
A=np.array([[1,0,dt,0,0,0],
[0,1,0,dt,0,0],
[0,0,1,0,0,0],
[0,0,0,1,0,0],
[0,0,0,0,1,dt],
[0,0,0,0,0,1]])'''
# Revised CTRV Model
##################################
# Use state of sigma points
##################################
Xi_pred = np.zeros([6,13])
for i in range(0,len(Xi.T)):
coeff = (Xi[2][i]/abs(Xi[2][i]))*((Xi[2][i]**2+Xi[3][i]**2)**(1/2))/Xi[5][i]
add_part = np.array([coeff * (mt.sin(Xi[4][i] + Xi[5][i]*dt) - mt.sin(Xi[4][i])),
coeff * (-mt.cos(Xi[4][i] + Xi[5][i]*dt) + mt.cos(Xi[4][i])),
0,
0,
Xi[5][i] * dt,
0])
Xi_pred[:,i] = Xi[:,i] + add_part
'''coeff = self.state[2]/self.state[4]
add_part = np.array([coeff * (mt.sin(self.state[3] + self.state[4]*dt) - mt.sin(self.state[3])),
coeff * (-mt.cos(self.state[3] + self.state[4]*dt) + mt.cos(self.state[3])),
0,
self.state[4] * dt,
0,
0,
0,
0])'''
#return Xi + add_part.reshape(-1,1)
return Xi_pred
def hx(self,Xi):
# B = np.array([[1,0,0,0,0],
# [0,1,0,0,0],
# [0,0,1,0,0],
# [0,0,0,1,0],
# [0,0,0,0,1]])
B = np.array([[1,0,0,0,0,0],
[0,1,0,0,0,0],
[0,0,1,0,0,0],
[0,0,0,1,0,0],
[0,0,0,0,1,0]])
return B @ Xi
#def unscented_kalman_filter(self, z_meas, box_meas, car_list, z_processed, dt):
def unscented_kalman_filter(self, centroid, box, cluster_id, processed, dt):
temp = -1
"""Unscented Ksalman Filter Algorithm."""
# (1) Sample Sigma Points and Weights.
Xi, W = self.sigma_points(self.P)
# (2) Predict Mean and Error Covariance of States.
fXi = self.fx(Xi, dt)
x_pred, P_x = self.UT(fXi, W, self.Q)
print(x_pred)
x_pred_plot = (np.array([ [0,-1], [1,0]]) @ x_pred[:2].T).T
#plt.plot(x_pred_plot[0], x_pred_plot[1], 'mo')
# (3) Data Association
# First Gate
for i in range(0, len(centroid)):
if processed[i] == 1:
continue
z_meas_trans = np.array([0,0])
z_meas_trans[0] = centroid[i,0] - x_pred[0]
z_meas_trans[1] = centroid[i,1] - x_pred[1]
Rot_inverse = np.array([[mt.cos(self.state[4]), mt.sin(self.state[4])],
[-mt.sin(self.state[4]), mt.cos(self.state[4])]])
z_meas_rot = Rot_inverse @ z_meas_trans
if -self.width_max * 0.3 <= z_meas_rot[0] <= self.width_max * 0.3 and -self.length_max * 0.3 <= z_meas_rot[1] <= self.length_max * 0.3:
self.processed = 1
processed[i] = 1
temp = i
break
# Second Gate (When Self Track is Not updated by the First gate)
if self.processed == 0:
for i in range(0, len(centroid)):
if processed[i] == 1:
continue
z_meas_trans = np.array([0,0])
z_meas_trans[0] = centroid[i,0] - x_pred[0]
z_meas_trans[1] = centroid[i,1] - x_pred[1]
# z_meas_trans[0] = clusters[i].res[0] - self.state[0]
# z_meas_trans[1] = clusters[i].res[1] - self.state[1]
Rot_inverse = np.array([[mt.cos(self.state[4]), mt.sin(self.state[4])],
[-mt.sin(self.state[4]), mt.cos(self.state[4])]])
z_meas_rot = Rot_inverse @ z_meas_trans
if -self.width_max * 0.3 <= z_meas_rot[0] <= self.width_max * 0.3 and -self.length_max * 0.5 <= z_meas_rot[1] <= self.length_max * 0.5:
self.processed = 1
processed[i] = 1
temp = i
break
# (4) Measurement Update
if temp == -1:
self.state = x_pred
self.P = P_x
self.DelCnt += 1
else:
hXi = self.hx(fXi)
z_pred, P_z = self.UT(hXi, W, self.R)
# Calculate Off Diagonal Elements of Error Covariance and Kalman Gain.
Pxz = W * (fXi - x_pred.reshape(-1, 1)) @ (hXi - z_pred.reshape(-1, 1)).T
K = Pxz @ inv(P_z)
# Validation Check : Yaw angle
#heading_angle =
measured_state = centroid[temp]
if (not measured_state[0] or (measured_state[0]-self.state[0])==0) and (not measured_state[1] or ((measured_state[1]-self.state[1])==0)):
v_x=0
v_y=0
hang=0
else:
v_x=(measured_state[0]-self.state[0])/dt
v_y=(measured_state[1]-self.state[1])/dt
hang=mt.atan2(v_y,v_x)
measured_state = np.insert(measured_state,2,v_x)
measured_state = np.insert(measured_state,3,v_y)
measured_state[4] = hang
#print("measured angle:", measured_state[2])
# if 80 * mt.pi/180 <= mt.fabs(measured_state[4] - self.state[4]) < 100*mt.pi/180:
# if self.state[4] >= 0:
# measured_state[4] += mt.pi/2
# elif self.state[4] < 0:
# measured_state[4] -= mt.pi/2
# if mt.fabs(measured_state[4] - self.state[4]) >= 160*mt.pi/180:
# if self.state[4] >= 0:
# measured_state[4] -= mt.pi
# elif self.state[4] < 0:
# measured_state[4] += mt.pi
'''if measured_state[2] > mt.pi/2:
measured_state[2] -= mt.pi
elif measured_state[2] < -mt.pi/2:
measured_state[2] += mt.pi'''
# if measured_state[4] > mt.pi/2 or measured_state[4] < -mt.pi/2:
# #print("check")
# measured_state[4] = self.state[4]
self.state = x_pred + K @ (measured_state - z_pred)
self.P = P_x - K @ P_z @ K.T
self.update_box(box[temp])
self.Age += 1
self.DelCnt = 0
self.ClusterID = cluster_id[temp]
# (5) Get max width and length box
if self.width_max < self.box[0]:
self.width_max = self.box[0]
if self.length_max < self.box[1]:
self.length_max = self.box[1]
# (6) Store History
self.history_state = np.append(self.history_state, [self.state], axis = 0)
self.history_box = np.append(self.history_box, [self.box], axis = 0)
def update_box(self, box_meas):
self.box = (1 - self.alpha) * self.box + self.alpha * box_meas