-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJPDA_Tracker.py
303 lines (259 loc) · 11 KB
/
JPDA_Tracker.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
# This is the main function which takes the detections, model and
# parameters and generates trajectories.
# ----------------------------------------------------------------------------------------------
# Output:
# Xe: Estimated mean of targets' states representing the trajectories
# - position and velocity at each frame (x_t Vx_t y_t Vy_t)
# Pe: Estimated covariance of the targets' states
# Ff: Frame numbers "t" for which each target's state is estimated.
# Term_Con: Number of consecutive frames which a target is miss-detected before its termination
# M: used to generate video clip
# ----------------------------------------------------------------------------------------------
# Input:
# Detection_address: The path to load the detections
# param: The method's parameters
# model: Tracking models
# Image_address: The path to load the image frames for visualization
# ----------------------------------------------------------------------------------------------
import numpy as np
import time
import scipy.sparse.csgraph
import random
from JPDA_Probabilty_Calculator import JPDA_Probabilty_Calculator
from Tree_Constructor import Tree_Constructor
def ApproxMultiscanJPDAProbabilities(M, Obj_info, mbest):
U = len(Obj_info)
Final_probabilty = []
for i in range(19):
Final_probabilty.append([])
n_components, labels = scipy.sparse.csgraph.connected_components(M, False, 'strong')
C2 = labels[:U]
for i in np.unique(C2):
ix = [item for item in range(len(C2)) if C2[item] == i]
tempInput = [obj_info[item] for item in ix]
temp = JPDA_Probabilty_Calculator(tempInput)
for j in ix:
Final_probabilty[j] = temp[ix.index(j)]
def ismember(a, b):
if a in b:
return False
else:
return True
def cellfundevidedsum(b, stepNo):
if b == []:
return b
else:
if type(b) == float:
c = np.array(1).reshape(-1, 1)
# return c
else:
c = b / float(sum(b))
# c = np.array([item / sum(b) for item in b]).reshape(-1)
return c
def JPDA(path, model, param, X0, P0, Term_Con, stepNo, Terminated_objects_index, Image_address=None):
# if stepNo%50==0:
print('stepNo now is:', stepNo)
# check the number of input arguments e.g .3 <= n_input <= 4
detection_new = path
Frame = 2 # len(detections[2]) Total Number of frames
# Detection parameter
Prun_Thre = param['Prun_Thre'] # Parameter for pruning detections with the confidence score less than this value
# Track termination parameter
Term_Frame = param['Term_Frame'] # The parameter for termination condition
# Kalman Models
F = model['F'] # The transition matrix for the dynamic model
Q = model['Q'] # The process covariance matrix for the dynamic model
H = model['H'] # Measurement matrix
R = model['R'] # Measurement covariance matrix
P1 = model['P0']
# JPDA Parameters
PD = param['PD'] # Detection Probability
Beta = param['Beta'] # False detection (clutter) likelihood
Gate = param['Gate'] # Gate size for gating
S_limit = param['S_limit'] # parameter to stop the gate size growing too much
# mbest = param['N_H'] # Threshold for considering m-best number of Hypotheses for JPDA
DMV = len(H)
DSV = len(H[0])
if not(len(X0)):
N_Target = 0
else:
N_Target = len(np.array(X0)[0])
# print(N_Target)
# ---------------------------------- Parameters from Last Frame -------------------------------------
# Initial State Vector
Xe = []
# Pe = []
BxT = []
Ff = []
ij = 0
for index in list(range(N_Target)):
Ff.append([1, 1])
ij += 1
Pe = P0
Xe = X0
# print(np.array(X0).shape,'\n')
Xe = list(np.transpose(Xe))
# ---------------- loading Image and detections for frame number > 1 -------------------------
f = 1 # previous frame
if f >= 1: # previous frame
ordinaryFrame = []
if not(len(detection_new)):
ordinaryFrame = []
else:
aaa = [detection_new[1][item] for item in range(len(detection_new[5])) if
detection_new[5][item] > Prun_Thre]
bbb = [detection_new[2][item] for item in range(len(detection_new[5])) if
detection_new[5][item] > Prun_Thre]
ordinaryFrame.append(aaa)
ordinaryFrame.append(bbb)
ordinaryFrame = np.transpose(ordinaryFrame)
# N_Target = len(Xe[1])
MXe = np.zeros((DSV, N_Target))
PXe = np.zeros((DSV, DSV, N_Target))
S = np.zeros((DMV, DMV, N_Target))
K = np.zeros((DSV, DMV, N_Target))
Target_Obs_indx = [[] for item in range(N_Target)]
Target_probabilty = [[] for item in range(N_Target)]
Curr_Obs = []
Xe = np.array(Xe)
Pe = np.array(Pe)
# print(Pe, '\n')
# if stepNo >= 88:
# print('1234567890:',N_Target)
# if stepNo == 99:
# print('1111:', Pe)
for no in list(range(N_Target)):
# if stepNo == 99:
# print(N_Target, Pe[no])
if not (no in Terminated_objects_index):
# Kalman Preditction & Hypothesis Construction
Target_Obs_indx[no], Target_probabilty[no], MXe[:, no], PXe[:, :, no], S[:, :, no], K[:, :, no] = \
Tree_Constructor(Xe[no].reshape(4), Pe[no], F, Q, H, R, ordinaryFrame, S_limit, Gate, PD, Beta, DMV,
stepNo, no)
# Mes_Tar[Target_Obs_indx[no], no] = true
# print(N_Target)
# print(Target_Obs_indx[no])
# if stepNo == 157:
# print(no, Target_probabilty[no])
# if stepNo == 157:
# print(Target_Obs_indx[58])
Temp_probability = [[] for count in range(N_Target)]
exist_ind = [ismember(item_temp, Terminated_objects_index) for item_temp in range(N_Target)]
# Temp_probability = [item_temptemp for item_temptemp, item_temptemp_index in zip(Target_probabilty, exist_ind) if
# item_temptemp_index]
for item_temp in range(N_Target):
if exist_ind[item_temp]:
Temp_probability[item_temp] = Target_probabilty[item_temp]
# Temp_probability = [Target_probabilty[exist_ind.index()] for item_temptemp_index in exist_ind if item_temptemp_index]
##############################################
# if stepNo == 157:
# print(Temp_probability[57],)
################################################
Final_probabilty = np.array([cellfundevidedsum(item_prob, stepNo) for item_prob in Temp_probability])
# **************************** Update step *****************************
for no in range(N_Target):
if not (no in Terminated_objects_index):
# k = f - Ff[no][0, 0] + 1
NN = len(Target_Obs_indx[no])
# if stepNo == 157:
# print(no,Final_probabilty[no])
P_temp = np.transpose(Final_probabilty[no])
xsum = np.zeros((4, 1))
if not Target_Obs_indx[no]:
# if len(P_temp[0]) == 1:
Xe[no] = MXe[:, no]
dP = 0
Pe[no] = PXe[:, :, no]
else:
Yij = ordinaryFrame[Target_Obs_indx[no]] - np.tile(np.transpose(np.dot(H, MXe[:, no])),
[np.size(Target_Obs_indx[no]), 1])
# if stepNo == 157:
# print(Target_Obs_indx[58])
# Ye = P_temp[0][NN] * Yij
# if len(P_temp[0]) == 1:
# Ye = Yij
# # Ye = Ye.reshape((1, -1))
#
# if stepNo == 157:
# print(Ye)
#
# Xe[no] = (MXe[:, no]).T + (np.dot(K[:, :, no], Ye.T)).T
# dP = np.dot(
# np.dot(K[:, :, no], (np.dot(np.tile(P_temp[0], [DMV, 1]) * Yij.T, Yij) - np.dot(Ye.T, Ye))),
# K[:, :, no].T)
# # print('215:', Ye, '\n')
# if stepNo == 157:
# print(no,P_temp[0],Yij)
# else:
Ye = np.dot(P_temp[0][1:], Yij)
Ye = Ye.reshape((1, -1))
# if stepNo == 157:
# print(Ye)
# print('213:', Ye, '\n')
# print(Xe.shape)
Xe[no] = (MXe[:, no]).T + (np.dot(K[:, :, no], Ye.T)).T
dP = np.dot(
np.dot(K[:, :, no], (np.dot(np.tile(P_temp[0][1:], [DMV, 1]) * Yij.T, Yij) - np.dot(Ye.T, Ye))),
K[:, :, no].T)
Pst = PXe[:, :, no] - np.dot(np.dot(K[:, :, no], S[:, :, no]), K[:, :, no].T)
# print(PXe[:, :, no],P_temp,no,'\n')
if len(P_temp.reshape(-1, 1)) != 1:
# if stepNo == 157:
# print(P_temp)
Po = P_temp[0][0] * PXe[:, :, no] + (1 - P_temp[0][0]) * Pst
else:
Po = P_temp * PXe[:, :, no] + (1 - P_temp) * Pst
Pe[no] = Po + dP
# if stepNo == 98:
# print(Pe[no])
# print(Pe, '\n')
# indM = np.argmax(P_temp)
if np.argmax(P_temp.reshape(-1, 1)) == 0:
Term_Con[no] += 1
else:
Term_Con[no] = 0
# print()
# print(np.array(Curr_Obs).shape, np.array(Target_Obs_indx[no]).shape,'\n')
if Target_Obs_indx[no]:
if Curr_Obs == []:
Curr_Obs = Target_Obs_indx[no][0]
else:
Curr_Obs = np.vstack(
(np.array(Curr_Obs).reshape(-1, 1), np.array(Target_Obs_indx[no]).reshape(-1, 1)))
if (Term_Con[no] <= Term_Frame) or (Term_Con[no] == 0):
pass
# Ff[no][1, 2] = f
else:
if Terminated_objects_index:
Terminated_objects_index.append(no)
else:
Terminated_objects_index = [no]
# print(Terminated_objects_index, Curr_Obs)
# if stepNo == 155:
# print(Terminated_objects_index)
# if stepNo == 156:
# print(Terminated_objects_index)
# # print(len(Term_Con))
# if stepNo >= 156:
# print(Terminated_objects_index, '1234:', stepNo)
All_Obs = len(ordinaryFrame)
if type(Curr_Obs) != np.ndarray:
Curr_Obs_1 = [Curr_Obs]
else:
Curr_Obs_1 = Curr_Obs
New_Targets = [item for item in range(All_Obs) if not (item in Curr_Obs_1)]
# if stepNo == 87:
# print(Curr_Obs)
# print(Xe)
if New_Targets:
for ij in range(len(New_Targets)):
Xe = np.vstack((Xe, np.dot(np.array(H).T, np.array(ordinaryFrame[New_Targets[ij], :]).T)))
# print(np.array(P0).shape)
Pe = np.vstack((Pe, np.array([P1])))
# Ff[N_Target + ij -1] = [f, f]
Term_Con.append(0)
# print(np.array(Xe).shape,'\n')
# print(Xe, '\n')
# if stepNo == 87:
# print('12:', Xe.shape)
return Xe.T, Pe, Term_Con, Terminated_objects_index