-
Notifications
You must be signed in to change notification settings - Fork 17
/
tracklet_utils_3d_online.py
1791 lines (1522 loc) · 92.8 KB
/
tracklet_utils_3d_online.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import argparse
import facenet
import lfw
import os
import sys
import cv2
import pickle
import time
from functools import wraps
from tensorflow.python.ops import data_flow_ops
from sklearn import metrics
from scipy.optimize import brentq
from scipy import interpolate
from scipy.interpolate import interp1d
from scipy.io import loadmat
from scipy import misc
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import shutil
import seq_nn_3d
import track_lib
global remove_set
global track_set
remove_set = []
track_set = []
def draw_result(img, save_mat, fr_id):
global track_struct
save_folder = track_struct['file_path']['tracking_img_folder']
table = track_struct['tracklet_mat']['color_table']
save_path = save_folder+'/'+track_lib.file_name(fr_id,10)+'.jpg'
# Create figure and axes
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(img)
# Create Rectangle patches
# save_mat = [fr_id, obj_id, track_id, x, y, w, h, x_3d, y_3d, w_3d, h_3d, det_score]
for k in range(len(save_mat)):
obj_id = int(save_mat[k,1])
tracklet_id = int(save_mat[k,2])
xmin = int(save_mat[k,3])
ymin = int(save_mat[k,4])
w = int(save_mat[k,5])
h = int(save_mat[k,6])
rect = patches.Rectangle((xmin,ymin),w,h,linewidth=1,edgecolor='#'+table[obj_id], facecolor='none')
img_text = plt.text(xmin,ymin,str(obj_id)+'_'+str(tracklet_id),fontsize=6,color='#'+table[obj_id])
# Add the patch to the Axes
ax.add_patch(rect)
if not os.path.isdir(save_folder):
os.makedirs(save_folder)
plt.savefig(save_path,bbox_inches='tight',dpi=400)
plt.clf()
plt.close('all')
return
def post_processing():
global track_struct
#import pdb; pdb.set_trace()
# update comb_cost
cand_track_idx = np.where(track_struct['tracklet_mat']['track_id_mat']!=-1)[0]
for n in range(len(cand_track_idx)):
track_struct['tracklet_mat']['comb_track_cost'][cand_track_idx[n],cand_track_idx] \
= track_struct['sub_tracklet_mat']['comb_track_cost'][n,:].copy()
track_struct['tracklet_mat']['comb_track_cost_mask'][cand_track_idx[n],cand_track_idx] \
= track_struct['sub_tracklet_mat']['comb_track_cost_mask'][n,:].copy()
#
tracklet_mat = track_struct['sub_tracklet_mat']
track_params = track_struct['track_params']
new_tracklet_mat = tracklet_mat.copy()
#import pdb; pdb.set_trace()
# update track cluster
N_cluster = len(tracklet_mat["track_cluster"])
new_assigned_id_mask = track_struct['tracklet_mat']['save_obj_id_mask'].copy()
avai_ids = np.where(track_struct['tracklet_mat']['assigned_obj_id_mask']==0)[0]
new_cnt = -1
for n in range(N_cluster):
if len(tracklet_mat["track_cluster"][n])==0:
continue
# check save_obj_id_mask
obj_ids = tracklet_mat['obj_id_mat'][np.array(tracklet_mat["track_cluster"][n],dtype=int)]
obj_mask = track_struct['tracklet_mat']['save_obj_id_mask'][obj_ids]
save_idx = np.where(obj_mask==1)[0]
if len(save_idx)>0:
track_struct['sub_tracklet_mat']['obj_id_mat'][np.array(tracklet_mat["track_cluster"][n],dtype=int)] = obj_ids[save_idx[0]]
continue
# check assigned_obj_id_mask
obj_mask = track_struct['tracklet_mat']['assigned_obj_id_mask'][obj_ids]
assigned_idx = np.where(obj_mask==1)[0]
if len(assigned_idx)==0:
new_cnt = new_cnt+1
track_struct['sub_tracklet_mat']['obj_id_mat'][np.array(tracklet_mat["track_cluster"][n],dtype=int)] = avai_ids[new_cnt]
else:
check_flag = 0
for k in range(len(assigned_idx)):
temp_obj_id = obj_ids[assigned_idx[k]]
if new_assigned_id_mask[temp_obj_id]==1:
continue
else:
track_struct['sub_tracklet_mat']['obj_id_mat'][np.array(tracklet_mat["track_cluster"][n],dtype=int)] \
= temp_obj_id
check_flag = 1
new_assigned_id_mask[temp_obj_id] = 1
break
if check_flag==0:
new_cnt = new_cnt+1
track_struct['sub_tracklet_mat']['obj_id_mat'][np.array(tracklet_mat["track_cluster"][n],dtype=int)] = avai_ids[new_cnt]
# copy to tracklet_mat
#import pdb; pdb.set_trace()
cand_track_idx = np.where(track_struct['tracklet_mat']['track_id_mat']!=-1)[0]
track_struct['tracklet_mat']['obj_id_mat'][cand_track_idx] = track_struct['sub_tracklet_mat']['obj_id_mat'].copy()
return
def comb_cost(tracklet_set, sess):
global track_struct
#global all_fea_mat
#global all_fea_label
img_size = track_struct['track_params']['img_size']
feature_size = track_struct['track_params']['feature_size']
max_length = track_struct['track_params']['max_length']
tracklet_mat = track_struct['sub_tracklet_mat']
loc_scales = track_struct['track_params']['loc_scales']
'''
temp_sum = np.sum(all_fea_mat[:,4,:,1], axis=1)
if len(np.where(temp_sum!=0)[0])==0:
fea_id = 0
else:
fea_id = int(np.max(np.where(temp_sum!=0)[0]))+1
'''
# cnn classifier
N_tracklet = len(tracklet_set)
track_interval = tracklet_mat['track_interval']
sort_idx = np.argsort(track_interval[np.array(tracklet_set),1])
cost = 0
if len(sort_idx)<=1:
return cost
remove_ids = []
comb_fea_mat = np.zeros((int(len(sort_idx)*(len(sort_idx)-1)/2),feature_size,max_length,3))
comb_fea_label = np.zeros((int(len(sort_idx)*(len(sort_idx)-1)/2),4))
temp_cost_list = []
#print(len(comb_track_cost))
cnt = -1
for n in range(0, len(sort_idx)-1):
for kk in range(n+1,len(sort_idx)):
cnt = int(cnt+1)
track_id1 = tracklet_set[sort_idx[n]]
track_id2 = tracklet_set[sort_idx[kk]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
remove_ids.append(cnt)
continue
#import pdb; pdb.set_trace()
if tracklet_mat['comb_track_cost_mask'][track_id1,track_id2]==1:
cost = cost+tracklet_mat['comb_track_cost'][track_id1,track_id2]
remove_ids.append(cnt)
continue
comb_fea_label[cnt,0] = track_id1
comb_fea_label[cnt,1] = track_id2
temp_cost_list.append([track_id1,track_id2])
# t starts from 0
#import pdb; pdb.set_trace()
t1_min = int(track_interval[track_id1,0])
t1_max = int(track_interval[track_id1,1])
t2_min = int(track_interval[track_id2,0])
t2_max = int(track_interval[track_id2,1])
t_min = int(min(t1_min,t2_min))
t_max = int(max(t1_max,t2_max))
if t_max-t_min+1<=max_length:
comb_fea_mat[cnt,:,t1_min-t_min:t1_max-t_min+1,1] = 1
comb_fea_mat[cnt,0,t1_min-t_min:t1_max-t_min+1,0] = tracklet_mat['x_3d_mat'][track_id1,t1_min:t1_max+1]/loc_scales[0]
comb_fea_mat[cnt,1,t1_min-t_min:t1_max-t_min+1,0] = tracklet_mat['y_3d_mat'][track_id1,t1_min:t1_max+1]/loc_scales[1]
comb_fea_mat[cnt,2,t1_min-t_min:t1_max-t_min+1,0] = tracklet_mat['w_3d_mat'][track_id1,t1_min:t1_max+1]/loc_scales[2]
comb_fea_mat[cnt,3,t1_min-t_min:t1_max-t_min+1,0] = tracklet_mat['h_3d_mat'][track_id1,t1_min:t1_max+1]/loc_scales[3]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id1)[0]
if len(cand_idx)>0:
temp_frs = tracklet_mat['appearance_fea_mat'][cand_idx,1]
temp_sort_idx = np.argsort(temp_frs)
cand_idx = cand_idx[temp_sort_idx]
if comb_fea_mat[cnt,4:,t1_min-t_min:t1_max-t_min+1,0].shape[1]!=np.transpose(tracklet_mat['appearance_fea_mat'] \
[cand_idx,2:]).shape[1]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,4:,t1_min-t_min:t1_max-t_min+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
comb_fea_mat[cnt,:,t2_min-t_min:t2_max-t_min+1,2] = 1
comb_fea_mat[cnt,0,t2_min-t_min:t2_max-t_min+1,0] = tracklet_mat['x_3d_mat'][track_id2,t2_min:t2_max+1]/loc_scales[0]
comb_fea_mat[cnt,1,t2_min-t_min:t2_max-t_min+1,0] = tracklet_mat['y_3d_mat'][track_id2,t2_min:t2_max+1]/loc_scales[1]
comb_fea_mat[cnt,2,t2_min-t_min:t2_max-t_min+1,0] = tracklet_mat['w_3d_mat'][track_id2,t2_min:t2_max+1]/loc_scales[2]
comb_fea_mat[cnt,3,t2_min-t_min:t2_max-t_min+1,0] = tracklet_mat['h_3d_mat'][track_id2,t2_min:t2_max+1]/loc_scales[3]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id2)[0]
if len(cand_idx)>0:
temp_frs = tracklet_mat['appearance_fea_mat'][cand_idx,1]
temp_sort_idx = np.argsort(temp_frs)
cand_idx = cand_idx[temp_sort_idx]
if comb_fea_mat[cnt,4:,t2_min-t_min:t2_max-t_min+1,0].shape[1]!=np.transpose(tracklet_mat['appearance_fea_mat'] \
[cand_idx,2:]).shape[1]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,4:,t2_min-t_min:t2_max-t_min+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
else:
t_len1 = t1_max-t1_min+1
t_len2 = t2_max-t2_min+1
t_len_min = min(t_len1,t_len2)
mid_t = int(0.5*(t1_max+t2_min))
if mid_t-t1_min+1>=0.5*max_length and t2_max-mid_t+1<=0.5*max_length:
t2_end = t2_max
t1_start = t2_end-max_length+1
#t1_start = mid_t-int(0.5*max_length)+1
#t2_end = t1_start+max_length-1
elif mid_t-t1_min+1<=0.5*max_length and t2_max-mid_t+1>=0.5*max_length:
t1_start = t1_min
t2_end = t1_start+max_length-1
else: # mid_t-t1_min+1>=0.5*max_length and t2_max-mid_t+1>=0.5*max_length:
t1_start = mid_t-int(0.5*max_length)+1
t2_end = t1_start+max_length-1
comb_fea_mat[cnt,:,0:t1_max-t1_start+1,1] = 1
if comb_fea_mat[cnt,0,0:t1_max-t1_start+1,0].shape[0] \
!=tracklet_mat['xmax_mat'][track_id1,t1_start:t1_max+1].shape[0]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,0,0:t1_max-t1_start+1,0] = tracklet_mat['x_3d_mat'][track_id1,t1_start:t1_max+1]/loc_scales[0]
comb_fea_mat[cnt,1,0:t1_max-t1_start+1,0] = tracklet_mat['y_3d_mat'][track_id1,t1_start:t1_max+1]/loc_scales[1]
comb_fea_mat[cnt,2,0:t1_max-t1_start+1,0] = tracklet_mat['w_3d_mat'][track_id1,t1_start:t1_max+1]/loc_scales[2]
comb_fea_mat[cnt,3,0:t1_max-t1_start+1,0] = tracklet_mat['h_3d_mat'][track_id1,t1_start:t1_max+1]/loc_scales[3]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id1)[0]
if len(cand_idx)>0:
temp_frs = tracklet_mat['appearance_fea_mat'][cand_idx,1]
temp_sort_idx = np.argsort(temp_frs)
cand_idx = cand_idx[temp_sort_idx]
cand_idx = cand_idx[t1_start-t1_min:]
comb_fea_mat[cnt,4:,0:t1_max-t1_start+1,0] = np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
comb_fea_mat[cnt,:,t2_min-t1_start:t2_end-t1_start+1,2] = 1
if comb_fea_mat[cnt,0,t2_min-t1_start:t2_end-t1_start+1,0].shape[0] \
!=tracklet_mat['xmin_mat'][track_id2,t2_min:t2_end+1].shape[0]:
import pdb; pdb.set_trace()
comb_fea_mat[cnt,0,t2_min-t1_start:t2_end-t1_start+1,0] = \
tracklet_mat['x_3d_mat'][track_id2,t2_min:t2_end+1]/loc_scales[0]
comb_fea_mat[cnt,1,t2_min-t1_start:t2_end-t1_start+1,0] = \
tracklet_mat['y_3d_mat'][track_id2,t2_min:t2_end+1]/loc_scales[1]
comb_fea_mat[cnt,2,t2_min-t1_start:t2_end-t1_start+1,0] = \
tracklet_mat['w_3d_mat'][track_id2,t2_min:t2_end+1]/loc_scales[2]
comb_fea_mat[cnt,3,t2_min-t1_start:t2_end-t1_start+1,0] = \
tracklet_mat['h_3d_mat'][track_id2,t2_min:t2_end+1]/loc_scales[3]
cand_idx = np.where(tracklet_mat['appearance_fea_mat'][:,0]==track_id2)[0]
if len(cand_idx)>0:
temp_frs = tracklet_mat['appearance_fea_mat'][cand_idx,1]
temp_sort_idx = np.argsort(temp_frs)
cand_idx = cand_idx[temp_sort_idx]
#import pdb; pdb.set_trace()
cand_idx = cand_idx[0:t2_end-t2_min+1]
comb_fea_mat[cnt,4:,t2_min-t1_start:t2_end-t1_start+1,0] \
= np.transpose(tracklet_mat['appearance_fea_mat'][cand_idx,2:])
# remove overlap detections
t_overlap = np.where(comb_fea_mat[cnt,0,:,1]+comb_fea_mat[cnt,0,:,2]>1)
if len(t_overlap)>0:
t_overlap = t_overlap[0]
comb_fea_mat[cnt,:,t_overlap,:] = 0
if len(track_set)>0:
search_idx = np.where(np.logical_and(track_set[:,0]==track_id1, track_set[:,1]==track_id2))
if len(search_idx[0])>0:
#save_fea_mat[search_idx[0][0],:,:,:] = comb_fea_mat[n,:,:,:]
if track_set[search_idx[0][0],2]==1:
comb_fea_label[cnt,2] = 1
else:
comb_fea_label[cnt,3] = 1
if len(remove_ids)>0:
comb_fea_mat = np.delete(comb_fea_mat, np.array(remove_ids), axis=0)
comb_fea_label = np.delete(comb_fea_label, np.array(remove_ids), axis=0)
if len(comb_fea_mat)>0:
max_batch_size = 16
num_batch = int(np.ceil(comb_fea_mat.shape[0]/max_batch_size))
pred_y = np.zeros((comb_fea_mat.shape[0],2))
for n in range(num_batch):
if n!=num_batch-1:
batch_size = max_batch_size
else:
batch_size = int(comb_fea_mat.shape[0]-(num_batch-1)*max_batch_size)
#batch_size = comb_fea_mat.shape[0]
x = np.zeros((batch_size,1,max_length,1))
y = np.zeros((batch_size,1,max_length,1))
w = np.zeros((batch_size,1,max_length,1))
h = np.zeros((batch_size,1,max_length,1))
ap = np.zeros((batch_size,feature_size-4,max_length,1))
mask_1 = np.zeros((batch_size,1,max_length,2))
mask_2 = np.zeros((batch_size,feature_size-4,max_length,2))
x[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,0,:,0]
y[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,1,:,0]
w[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,2,:,0]
h[:,0,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,3,:,0]
ap[:,:,:,0] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,4:,:,0]
mask_1[:,0,:,:] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,0,:,1:]
mask_2[:,:,:,:] = comb_fea_mat[n*max_batch_size:n*max_batch_size+batch_size,4:,:,1:]
pred_y[n*max_batch_size:n*max_batch_size+batch_size,:] = sess.run(y_conv, feed_dict={batch_X_x: x,
batch_X_y: y,
batch_X_w: w,
batch_X_h: h,
batch_X_a: ap,
batch_mask_1: mask_1,
batch_mask_2: mask_2,
batch_Y: np.zeros((batch_size,2)),
keep_prob: 1.0})
for n in range(len(pred_y)):
if np.sum(comb_fea_label[n,2:4])>0:
continue
if pred_y[n,0]>pred_y[n,1]:
comb_fea_label[n,2] = 1
else:
comb_fea_label[n,3] = 1
if comb_fea_mat.shape[0]!=len(pred_y):
import pdb; pdb.set_trace()
'''
all_fea_mat[fea_id:fea_id+len(pred_y),:,:,:] = comb_fea_mat
all_fea_label[fea_id:fea_id+len(pred_y),:] = comb_fea_label
'''
cost = cost+np.sum(pred_y[:,1]-pred_y[:,0])
#import pdb; pdb.set_trace()
if pred_y.shape[0]!=len(temp_cost_list):
import pdb; pdb.set_trace()
for n in range(pred_y.shape[0]):
tracklet_mat['comb_track_cost_mask'][temp_cost_list[n][0],temp_cost_list[n][1]] = 1
tracklet_mat['comb_track_cost'][temp_cost_list[n][0],temp_cost_list[n][1]] = pred_y[n,1]-pred_y[n,0]
return cost
def get_split_cost(track_id, sess):
global track_struct
tracklet_mat = track_struct['sub_tracklet_mat']
new_cluster_cost = np.zeros((2,1))
if len(tracklet_mat['track_cluster'][track_id])<2:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
track_interval = tracklet_mat['track_interval'].copy()
change_cluster_idx = [len(tracklet_mat['track_cluster']), tracklet_mat['track_class'][track_id]]
new_cluster_set = []
new_cluster_set.append([track_id])
remain_tracks = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
remain_tracks.remove(track_id)
new_cluster_set.append(remain_tracks)
# get cost
if len(remain_tracks)>1:
sort_idx = np.argsort(track_interval[np.array(new_cluster_set[1]),1])
for n in range(0, len(sort_idx)-1):
track_id1 = new_cluster_set[1][sort_idx[n]]
track_id2 = new_cluster_set[1][sort_idx[n+1]]
#if track_id1==42:
# import pdb; pdb.set_trace()
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
#*********************************
new_cluster_cost[1,0] = comb_cost(remain_tracks, sess)
# cross cost
comb_cluster = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
sort_idx = np.argsort(track_interval[np.array(comb_cluster),1])
cross_cost = np.zeros((2,1))
cost = np.sum(new_cluster_cost)-cross_cost[1,0]
prev_cost = tracklet_mat['cluster_cost'][tracklet_mat['track_class'][track_id]]-cross_cost[0,0]
diff_cost = cost-prev_cost
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
def get_assign_cost(track_id, sess):
global track_struct
tracklet_mat = track_struct['sub_tracklet_mat']
#import pdb; pdb.set_trace()
cluster1 = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
new_cluster_cost = np.zeros((2,1))
new_cluster_set = []
new_cluster_set.append(cluster1.copy())
new_cluster_set[0].remove(track_id)
track_interval = tracklet_mat['track_interval'].copy()
# get cost
if len(new_cluster_set[0])>1:
sort_idx = np.argsort(track_interval[np.array(new_cluster_set[0]),1])
for n in range(0, len(sort_idx)-1):
track_id1 = new_cluster_set[0][sort_idx[n]]
track_id2 = new_cluster_set[0][sort_idx[n+1]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
new_cluster_cost[0,0] = comb_cost(new_cluster_set[0], sess)
track_class = track_struct['sub_tracklet_mat']['track_class'][track_id]
t_cluster_idx = track_struct['sub_tracklet_mat']['track_cluster_t_idx'][track_class]
NN_cluster = len(tracklet_mat['track_cluster'])
temp_new_cluster_cost = float("inf")*np.ones((NN_cluster,1))
prev_cost_vec = np.zeros((NN_cluster,1))
cross_cost_vec = np.zeros((NN_cluster,2))
for nn in range(len(t_cluster_idx)):
N_cluster = len(track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]])
for mm in range(N_cluster):
n = track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]][mm]
# the original cluster
if tracklet_mat['track_class'][track_id]==n:
continue
# check neighbor and conflict track
cluster2 = tracklet_mat['track_cluster'][n]
neighbor_flag = 1
conflict_flag = 0
#remove_flag = 0
temp_cluster_set = cluster2.copy()
temp_cluster_set.append(track_id)
sort_idx = np.argsort(track_interval[np.array(temp_cluster_set),1])
for m in range(0, len(sort_idx)-1):
track_id1 = temp_cluster_set[sort_idx[m]]
track_id2 = temp_cluster_set[sort_idx[m+1]]
#if cluster2[m] in remove_set:
# remove_flag = 1
# break
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
neighbor_flag = 0
break
if track_id1 in tracklet_mat['conflict_track_idx'][track_id2]:
conflict_flag = 1
break
if neighbor_flag==0 or conflict_flag==1:# or remove_flag==1:
continue
# get cost
temp_set = cluster2.copy()
temp_set.append(track_id)
temp_new_cluster_cost[n,0] = comb_cost(temp_set, sess)
prev_cost_vec[n,0] = tracklet_mat['cluster_cost'][tracklet_mat['track_class'][track_id]] \
+tracklet_mat['cluster_cost'][n]
cost_vec = temp_new_cluster_cost[:,0]+new_cluster_cost[0,0]-cross_cost_vec[:,1]
prev_cost_vec = prev_cost_vec[:,0]-cross_cost_vec[:,0]
diff_cost_vec = cost_vec-prev_cost_vec
min_idx = np.argmin(diff_cost_vec)
cost = cost_vec[min_idx]
if cost==float("inf"):
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
diff_cost = diff_cost_vec[min_idx]
new_cluster_cost[1,0] = temp_new_cluster_cost[min_idx,0]
change_cluster_idx = [tracklet_mat['track_class'][track_id],min_idx]
temp_set = tracklet_mat['track_cluster'][min_idx].copy()
temp_set.append(track_id)
new_cluster_set.append(temp_set)
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
def get_merge_cost(track_id, sess):
global track_struct
tracklet_mat = track_struct['sub_tracklet_mat']
track_interval = tracklet_mat['track_interval'].copy()
cluster1 = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
if len(cluster1)==1:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
track_class = track_struct['sub_tracklet_mat']['track_class'][track_id]
t_cluster_idx = track_struct['sub_tracklet_mat']['track_cluster_t_idx'][track_class]
NN_cluster = len(tracklet_mat['track_cluster'])
new_cluster_cost_vec = float("inf")*np.ones((NN_cluster,1))
prev_cost_vec = np.zeros((NN_cluster,1))
cross_cost_vec = np.zeros((NN_cluster,2))
for nn in range(len(t_cluster_idx)):
N_cluster = len(track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]])
for mm in range(N_cluster):
n = track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]][mm]
# the original cluster
if tracklet_mat['track_class'][track_id]==n:
continue
# check neighbor and conflict track
cluster2 = tracklet_mat['track_cluster'][n].copy()
if len(cluster2)<=1:
continue
neighbor_flag = 1
conflict_flag = 0
#remove_flag = 0
temp_cluster_set = cluster1+cluster2
sort_idx = np.argsort(track_interval[np.array(temp_cluster_set),1])
for m in range(0, len(sort_idx)-1):
track_id1 = temp_cluster_set[sort_idx[m]]
track_id2 = temp_cluster_set[sort_idx[m+1]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
neighbor_flag = 0
break
if track_id1 in tracklet_mat['conflict_track_idx'][track_id2]:
conflict_flag = 1
break
if neighbor_flag==0 or conflict_flag==1:# or remove_flag==1:
continue
# get cost
new_cluster_cost_vec[n,0] = comb_cost(cluster1+cluster2, sess)
prev_cost_vec[n,0] = tracklet_mat['cluster_cost'][tracklet_mat['track_class'][track_id]] \
+tracklet_mat['cluster_cost'][n]
prev_cost_vec = prev_cost_vec[:,0]-cross_cost_vec[:,0]
diff_cost_vec = new_cluster_cost_vec[:,0]-prev_cost_vec
min_idx = np.argmin(diff_cost_vec)
cost = new_cluster_cost_vec[min_idx,0]
if cost==float("inf"):
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
diff_cost = diff_cost_vec[min_idx]
new_cluster_cost = np.zeros((2,1))
new_cluster_cost[0,0] = cost
change_cluster_idx = [tracklet_mat['track_class'][track_id], min_idx]
new_cluster_set = []
temp_set = cluster1.copy()
temp_set = temp_set+tracklet_mat['track_cluster'][min_idx]
new_cluster_set.append(temp_set)
new_cluster_set.append([])
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
def get_switch_cost(track_id, sess):
global track_struct
tracklet_mat = track_struct['sub_tracklet_mat']
track_interval = tracklet_mat['track_interval'].copy()
cluster1 = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
S1 = []
S2 = []
for k in range(len(cluster1)):
temp_id = cluster1[k]
if tracklet_mat['track_interval'][temp_id,1]<=tracklet_mat['track_interval'][track_id,1]:
S1.append(temp_id)
else:
S2.append(temp_id)
if len(S1)==0 or len(S2)==0:
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
track_class = track_struct['sub_tracklet_mat']['track_class'][track_id]
t_cluster_idx = track_struct['sub_tracklet_mat']['track_cluster_t_idx'][track_class]
NN_cluster = len(tracklet_mat['track_cluster'])
cost_vec = float("inf")*np.ones((NN_cluster,1))
prev_cost_vec = np.zeros((NN_cluster,1))
new_cluster_cost_vec1 = float("inf")*np.ones((NN_cluster,1))
new_cluster_cost_vec2 = float("inf")*np.ones((NN_cluster,1))
cross_cost_vec = np.zeros((NN_cluster,2))
track_id_set = []
for n in range(NN_cluster):
track_id_set.append([])
for nn in range(len(t_cluster_idx)):
N_cluster = len(track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]])
for mm in range(N_cluster):
n = track_struct['sub_tracklet_mat']['time_cluster'][t_cluster_idx[nn]][mm]
# the original cluster
if tracklet_mat['track_class'][track_id]==n:
continue
# switch availability check
S3 = []
S4 = []
#remove_flag = 0
cluster2 = tracklet_mat['track_cluster'][n].copy()
for k in range(len(cluster2)):
temp_id = cluster2[k]
if tracklet_mat['track_interval'][temp_id,1]<=tracklet_mat['track_interval'][track_id,1]:
S3.append(temp_id)
else:
#********************************************
if tracklet_mat['track_interval'][temp_id,1] >=tracklet_mat['track_interval'][track_id,1] \
and tracklet_mat['track_interval'][temp_id,0] <=tracklet_mat['track_interval'][track_id,1]:
if tracklet_mat['track_interval'][temp_id,1] -tracklet_mat['track_interval'][track_id,1] \
>tracklet_mat['track_interval'][track_id,1]-tracklet_mat['track_interval'][temp_id,0]:
S4.append(temp_id)
else:
S3.append(temp_id)
else:
S4.append(temp_id)
neighbor_flag1 = 1
conflict_flag1 = 0
if len(S3)==0:
neighbor_flag1 = 1
conflict_flag1 = 0
else:
temp_cluster_set = S3+S2
sort_idx = np.argsort(track_interval[np.array(temp_cluster_set),1])
for k in range(0,len(sort_idx)-1):
track_id1 = temp_cluster_set[sort_idx[k]]
track_id2 = temp_cluster_set[sort_idx[k+1]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
neighbor_flag1 = 0
break
if track_id1 in tracklet_mat['conflict_track_idx'][track_id2]:
conflict_flag1 = 1
break
neighbor_flag2 = 1
conflict_flag2 = 0
if len(S4)==0:
neighbor_flag2 = 1
conflict_flag2 = 0
else:
temp_cluster_set = S4+S1
sort_idx = np.argsort(track_interval[np.array(temp_cluster_set),1])
for k in range(0,len(sort_idx)-1):
track_id1 = temp_cluster_set[sort_idx[k]]
track_id2 = temp_cluster_set[sort_idx[k+1]]
if track_id1 not in tracklet_mat['neighbor_track_idx'][track_id2]:
neighbor_flag2 = 0
break
if track_id1 in tracklet_mat['conflict_track_idx'][track_id2]:
conflict_flag2 = 1
break
if neighbor_flag1==0 or conflict_flag1==1 or neighbor_flag2==0 or conflict_flag2==1:
continue
# get cost
S_1 = S1+S4
S_2 = S2+S3
new_cluster_cost_vec1[n,0] = comb_cost(S_1, sess)
new_cluster_cost_vec2[n,0] = comb_cost(S_2, sess)
cost_vec[n,0] = new_cluster_cost_vec1[n,0]+new_cluster_cost_vec2[n,0]
track_id_set[n].append(S_1.copy())
track_id_set[n].append(S_2.copy())
prev_cost_vec[n,0] = tracklet_mat['cluster_cost'][tracklet_mat['track_class'][track_id]] \
+tracklet_mat['cluster_cost'][n]
cost_vec = cost_vec[:,0]-cross_cost_vec[:,1]
prev_cost_vec = prev_cost_vec[:,0]-cross_cost_vec[:,0]
diff_cost_vec = cost_vec-prev_cost_vec
min_idx = np.argmin(diff_cost_vec)
cost = cost_vec[min_idx]
if cost==float("inf"):
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
diff_cost = diff_cost_vec[min_idx]
new_cluster_cost = np.zeros((2,1))
new_cluster_cost[0,0] = new_cluster_cost_vec1[min_idx,0]
new_cluster_cost[1,0] = new_cluster_cost_vec2[min_idx,0]
change_cluster_idx = [tracklet_mat['track_class'][track_id], min_idx]
new_cluster_set = []
new_cluster_set.append(track_id_set[min_idx][0])
new_cluster_set.append(track_id_set[min_idx][1])
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
def get_break_cost(track_id, sess):
global track_struct
tracklet_mat = track_struct['sub_tracklet_mat']
new_cluster_cost = np.zeros((2,1))
cluster1 = tracklet_mat['track_cluster'][tracklet_mat['track_class'][track_id]].copy()
if len(cluster1)<=2:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
# get cost
after_ids = []
for n in range(len(cluster1)):
if tracklet_mat['track_interval'][cluster1[n],1]>tracklet_mat['track_interval'][track_id,1]:
after_ids.append(cluster1[n])
if len(after_ids)==0:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
before_ids = list(set(cluster1)-set(after_ids))
if len(before_ids)<=1:
cost = float("inf")
diff_cost = float("inf")
new_cluster_cost = []
new_cluster_set = []
change_cluster_idx = []
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
change_cluster_idx = [len(tracklet_mat['track_cluster']), tracklet_mat['track_class'][track_id]]
new_cluster_set = []
new_cluster_set.append(before_ids)
remain_tracks = after_ids
new_cluster_set.append(remain_tracks)
new_cluster_cost[0,0] = comb_cost(new_cluster_set[0], sess)
new_cluster_cost[1,0] = comb_cost(new_cluster_set[1], sess)
cost = np.sum(new_cluster_cost)
diff_cost = cost-tracklet_mat['cluster_cost'][tracklet_mat['track_class'][track_id]]
return diff_cost,new_cluster_cost,new_cluster_set,change_cluster_idx
def copy_sub_mat():
global track_struct
track_struct['sub_tracklet_mat'] = {}
cand_track_idx = np.where(track_struct['tracklet_mat']['track_id_mat']!=-1)[0]
track_struct['sub_tracklet_mat']['xmin_mat'] = track_struct['tracklet_mat']['xmin_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['ymin_mat'] = track_struct['tracklet_mat']['ymin_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['xmax_mat'] = track_struct['tracklet_mat']['xmax_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['ymax_mat'] = track_struct['tracklet_mat']['ymax_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['x_3d_mat'] = track_struct['tracklet_mat']['x_3d_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['y_3d_mat'] = track_struct['tracklet_mat']['y_3d_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['w_3d_mat'] = track_struct['tracklet_mat']['w_3d_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['h_3d_mat'] = track_struct['tracklet_mat']['h_3d_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['det_score_mat'] = track_struct['tracklet_mat']['det_score_mat'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['track_interval'] = track_struct['tracklet_mat']['track_interval'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['obj_id_mat'] = track_struct['tracklet_mat']['obj_id_mat'][cand_track_idx].copy()
track_struct['sub_tracklet_mat']['track_id_mat'] = track_struct['tracklet_mat']['track_id_mat'][cand_track_idx].copy()
#track_struct['sub_tracklet_mat']['save_obj_id_mask'] = track_struct['tracklet_mat']['save_obj_id_mask'].copy()
#track_struct['sub_tracklet_mat']['assigned_obj_id_mask'] = track_struct['tracklet_mat']['assigned_obj_id_mask'].copy()
# update comb_track_cost
change_idx = np.zeros(track_struct['track_params']['num_track'], dtype=int)
for n in range(track_struct['track_params']['num_track']):
if track_struct['tracklet_mat']['track_interval'][n,1]-track_struct['tracklet_mat']['track_interval'][n,0] \
!=track_struct['tracklet_mat']['prev_track_interval'][n,1]-track_struct['tracklet_mat']['prev_track_interval'][n,0] \
or (track_struct['tracklet_mat']['track_interval'][n,0]==0
and track_struct['tracklet_mat']['prev_track_interval'][n,0]==0
and track_struct['tracklet_mat']['track_interval'][n,1]==track_struct['track_params']['num_fr']-1
and track_struct['tracklet_mat']['prev_track_interval'][n,1]==track_struct['track_params']['num_fr']-1):
change_idx[n] = 1
track_struct['tracklet_mat']['comb_track_cost'][change_idx==1,:] = 0
track_struct['tracklet_mat']['comb_track_cost'][:,change_idx==1] = 0
track_struct['tracklet_mat']['comb_track_cost_mask'][change_idx==1,:] = 0
track_struct['tracklet_mat']['comb_track_cost_mask'][:,change_idx==1] = 0
temp_mat = track_struct['tracklet_mat']['comb_track_cost'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['comb_track_cost'] = temp_mat[:,cand_track_idx].copy()
temp_mat = track_struct['tracklet_mat']['comb_track_cost_mask'][cand_track_idx,:].copy()
track_struct['sub_tracklet_mat']['comb_track_cost_mask'] = temp_mat[:,cand_track_idx].copy()
fea_cand_idx = np.where(track_struct['tracklet_mat']['appearance_fea_mat'][:,0]!=-1)[0]
track_struct['sub_tracklet_mat']['appearance_fea_mat'] = track_struct['tracklet_mat']['appearance_fea_mat'][fea_cand_idx,:].copy()
# update track_id for sub_tracklet_mat
for n in range(len(cand_track_idx)):
temp_idx = np.where(track_struct['sub_tracklet_mat']['appearance_fea_mat'][:,0]==cand_track_idx[n])[0]
track_struct['sub_tracklet_mat']['appearance_fea_mat'][temp_idx,0] = n
return
def init_clustering():
global track_struct
# copy the sub tracklet_mat
copy_sub_mat()
N_tracklet = track_struct['sub_tracklet_mat']['xmin_mat'].shape[0]
# track cluster
track_struct['sub_tracklet_mat']['track_cluster'] = []
# track class
track_struct['sub_tracklet_mat']['track_class'] = np.arange(N_tracklet, dtype=int)
# time cluster
track_struct['sub_tracklet_mat']['time_cluster'] = []
for n in range(track_struct['track_params']['num_time_cluster']):
track_struct['sub_tracklet_mat']['time_cluster'].append([])
track_struct['sub_tracklet_mat']['track_cluster_t_idx'] = []
for n in range(N_tracklet):
idx = np.where(track_struct['sub_tracklet_mat']['xmin_mat'][n,:]!=-1)[0]
track_struct['sub_tracklet_mat']['track_interval'][n,0] = np.min(idx)
track_struct['sub_tracklet_mat']['track_interval'][n,1] = np.max(idx)
track_struct['sub_tracklet_mat']['track_cluster'].append([n])
if n in remove_set:
track_struct['sub_tracklet_mat']['track_cluster_t_idx'].append([-1])
else:
min_time_cluster_idx = int(np.floor(max(track_struct['sub_tracklet_mat']['track_interval'][n,0]
-track_struct['track_params']['t_dist_thresh']-5,0)
/track_struct['track_params']['time_cluster_dist']))
max_time_cluster_idx = int(np.floor(min(track_struct['sub_tracklet_mat']['track_interval'][n,1]
+track_struct['track_params']['t_dist_thresh']+5,
track_struct['sub_tracklet_mat']['xmin_mat'].shape[1]-1)
/track_struct['track_params']['time_cluster_dist']))
track_struct['sub_tracklet_mat']['track_cluster_t_idx'].append(list(range(min_time_cluster_idx,max_time_cluster_idx+1)))
for k in range(min_time_cluster_idx,max_time_cluster_idx+1):
track_struct['sub_tracklet_mat']['time_cluster'][k].append(n)
# get center position of each detection location
mask = track_struct['sub_tracklet_mat']['xmin_mat']==-1
track_struct['sub_tracklet_mat']['center_x'] = \
(track_struct['sub_tracklet_mat']['xmin_mat']+track_struct['sub_tracklet_mat']['xmax_mat'])/2
track_struct['sub_tracklet_mat']['center_y'] = \
(track_struct['sub_tracklet_mat']['ymin_mat']+track_struct['sub_tracklet_mat']['ymax_mat'])/2
track_struct['sub_tracklet_mat']['w'] = \
track_struct['sub_tracklet_mat']['xmax_mat']-track_struct['sub_tracklet_mat']['xmin_mat']+1
track_struct['sub_tracklet_mat']['h'] = \
track_struct['sub_tracklet_mat']['ymax_mat']-track_struct['sub_tracklet_mat']['ymin_mat']+1
track_struct['sub_tracklet_mat']['center_x'][mask] = -1
track_struct['sub_tracklet_mat']['center_y'][mask] = -1
track_struct['sub_tracklet_mat']['w'][mask] = -1
track_struct['sub_tracklet_mat']['h'][mask] = -1
# neighbor_track_idx and conflict_track_idx
track_struct['sub_tracklet_mat']['neighbor_track_idx'] = []
track_struct['sub_tracklet_mat']['conflict_track_idx'] = []
for n in range(N_tracklet):
track_struct['sub_tracklet_mat']['neighbor_track_idx'].append([])
track_struct['sub_tracklet_mat']['conflict_track_idx'].append([])
for n in range(N_tracklet-1):
for m in range(n+1, N_tracklet):
t_min1 = track_struct['sub_tracklet_mat']['track_interval'][n,0]
t_max1 = track_struct['sub_tracklet_mat']['track_interval'][n,1]
t_min2 = track_struct['sub_tracklet_mat']['track_interval'][m,0]
t_max2 = track_struct['sub_tracklet_mat']['track_interval'][m,1]
overlap_len = min(t_max2,t_max1)-max(t_min1,t_min2)+1
overlap_r = overlap_len/(t_max1-t_min1+1+t_max2-t_min2+1-overlap_len)
if overlap_len>0 and overlap_r>track_struct['track_params']['track_overlap_thresh']:
track_struct['sub_tracklet_mat']['conflict_track_idx'][n].append(m)
track_struct['sub_tracklet_mat']['conflict_track_idx'][m].append(n)
continue
if overlap_len>0 and overlap_r<=track_struct['track_params']['track_overlap_thresh']:
# check the search region
t1 = int(max(t_min1,t_min2))
t2 = int(min(t_max2,t_max1))
if (t_min1<=t_min2 and t_max1>=t_max2) or (t_min1>=t_min2 and t_max1<=t_max2) or overlap_len>4:
track_struct['sub_tracklet_mat']['conflict_track_idx'][n].append(m)
track_struct['sub_tracklet_mat']['conflict_track_idx'][m].append(n)
continue
cand_t = np.array(range(t1,t2+1))
dist_x = abs(track_struct['sub_tracklet_mat']['center_x'][n,cand_t] \
-track_struct['sub_tracklet_mat']['center_x'][m,cand_t])
dist_y = abs(track_struct['sub_tracklet_mat']['center_y'][n,cand_t] \
-track_struct['sub_tracklet_mat']['center_y'][m,cand_t])
w1 = track_struct['sub_tracklet_mat']['w'][n,cand_t]
h1 = track_struct['sub_tracklet_mat']['h'][n,cand_t]
w2 = track_struct['sub_tracklet_mat']['w'][m,cand_t]
h2 = track_struct['sub_tracklet_mat']['h'][m,cand_t]
min_len = np.min([np.min(w1),np.min(h1),np.min(w2),np.min(h2)])
min_dist_x1 = np.min(dist_x/min_len)
min_dist_y1 = np.min(dist_y/min_len)
min_dist_x2 = np.min(dist_x/min_len)
min_dist_y2 = np.min(dist_y/min_len)
if min_dist_x1<track_struct['track_params']['search_radius'] \
and min_dist_y1<track_struct['track_params']['search_radius'] \
and min_dist_x2<track_struct['track_params']['search_radius'] \
and min_dist_y2<track_struct['track_params']['search_radius']:
track_struct['sub_tracklet_mat']['neighbor_track_idx'][n].append(m)
track_struct['sub_tracklet_mat']['neighbor_track_idx'][m].append(n)
if overlap_len<=0 and min(abs(t_min1-t_max2),abs(t_min2-t_max1)) \
<track_struct['track_params']['t_dist_thresh']:
if t_min1>=t_max2:
t1 = int(t_min1)
t2 = int(t_max2)
else:
t1 = int(t_max1)
t2 = int(t_min2)
#***********************************
tr_t1 = np.array(range(int(t_min1),int(t_max1+1)))
tr_x1 = track_struct['sub_tracklet_mat']['center_x'][n,int(t_min1):int(t_max1+1)]
tr_y1 = track_struct['sub_tracklet_mat']['center_y'][n,int(t_min1):int(t_max1+1)]
if len(tr_t1)>10:
if t_min1>=t_max2:
tr_t1 = tr_t1[0:10]
tr_x1 = tr_x1[0:10]
tr_y1 = tr_y1[0:10]
else:
tr_t1 = tr_t1[-10:]
tr_x1 = tr_x1[-10:]
tr_y1 = tr_y1[-10:]
ts_x1 = track_lib.linear_pred_v2(tr_t1, tr_x1, np.array([t2]))