-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_tracking.py
384 lines (282 loc) · 12.3 KB
/
sort_tracking.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
import sys
import os
import numpy as np
import open3d as o3d
import time
import math
import operator
from sklearn.cluster import MeanShift
from sklearn.neighbors import KDTree
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
import clusteringModule as clu
import lineSegmentation as seg
from TrackingModule import track
import loadData
####################################################
########### Setting ################################
####################################################
pi = 3.141592653589793238
def get_angle(input_list):
angle = math.atan2(input_list[1], input_list[0])
return angle
# Set mod
mod = sys.modules[__name__]
# Set Track list
Track_list = []
# Expand iteration limit
sys.setrecursionlimit(5000)
# Set Car Standard
carz_min, carz_max = 0, 2
carx_min, carx_max = 1.5, 5
cary_min, cary_max = 1.5, 5
# Set Visualizer and Draw x, y Axis
#vis = o3d.visualization.Visualizer()
#vis.create_window()
Axis_Points = [[0,0,0], [20,0,0],[0,20,0]]
Axis_Lines = [[0,1],[0,2]]
colors = [[0,0,0] for i in range(len(Axis_Lines))]
line_set = o3d.geometry.LineSet(points = o3d.utility.Vector3dVector(Axis_Points), lines = o3d.utility.Vector2iVector(Axis_Lines))
line_set.colors = o3d.utility.Vector3dVector(colors)
# Load binary data
path = './2011_09_26/2011_09_26_drive_0005_sync/velodyne_points/data/'
f = open("./2011_09_26/2011_09_26_drive_0005_sync/velodyne_points/timestamps.txt","r")
file_list = loadData.load_data(path)
num = 0
##################################################################################
########################### Main Loop ############################################
##################################################################################
pre_time_stamp = None
# get points from all lists
for files in file_list:
res = np.empty([0,6])
car_count = 0
# Draw Axis
#vis.add_geometry(line_set)
#vis.run()
# Get dt
line = f.readline()
line = (line.split(" ")[1]).split(":")
time_stamp = 3600 * float(line[0]) + 60 * float(line[1]) + float(line[2])
if pre_time_stamp:
dt = time_stamp - pre_time_stamp
data = np.fromfile(path+files, dtype = np.float32)
data = data.reshape(-1,4)
data = data[:,0:3]
# Convert numpy into pointcloud
cloud = o3d.geometry.PointCloud()
cloud.points = o3d.utility.Vector3dVector(data)
# Downsampling pointcloud
cloud_downsample = cloud.voxel_down_sample(voxel_size=0.1)
#print(cloud_downsample.segment_plane(0.4,300,300)[1])
#outerBox = [[20,-10,-1.8],[20,-10,-1.8]]
#cloud_downsample.crop()
# Convert pcd to numpy array
cloud_downsample = np.asarray(cloud_downsample.points)
# Crop Pointcloud -20m < x < 20m && -20m < y < 20m && z > -1.80m
cloud_downsample = cloud_downsample[((cloud_downsample[:, 0] <= 15))]
cloud_downsample = cloud_downsample[((cloud_downsample[:, 0] >= -15))]
cloud_downsample = cloud_downsample[((cloud_downsample[:, 1] <= 10))]
cloud_downsample = cloud_downsample[((cloud_downsample[:, 1] >= -10))]
# threshold z value cut the road
cloudoutliers = cloud_downsample[((cloud_downsample[:, 2] >= -1.3))] # -1.56
# Clustering Pointcloud
# adjust the threshold into Clustering
start = time.time()
tree = KDTree(cloudoutliers, leaf_size = 100)
clusters = clu.euclideanCluster(cloudoutliers, tree, 0.5)
#print("number of estimated clusters : ", len(clusters))
#print("How much time for Clustering")
#print(time.time() - start)
cluster = np.empty([0,3])
clustersCloud = np.empty(shape = [0,3])
# Visualize Clusters
for i in range(len(clusters)):
###########################
# Find the Cars
# 1) Extract each cluster
clusterCloud = cloudoutliers[clusters[i][:],:]
#clustersCloud_pcd.points = np.append(clustersCloud_pcd.points,clusterCloud_pcd.points)
# 2) Find Cars with weak condition
z_max=z_min=x_max=x_min=y_max=y_min=0
z_max = np.max(clusterCloud[:,2])
z_min = np.min(clusterCloud[:,2])
z_for_slicing = 4/5*z_min + 1/5*z_max
'''
#clusterCloud = clusterCloud[:,0:2]
#clusterCloud[:,2] = z_for_slicing
tempcloud = o3d.geometry.PointCloud()
tempcloud.points = o3d.utility.Vector3dVector(clusterCloud)
tempcloud.compute_convex_hull()
print(type(clusterCloud))
print(clusterCloud.shape)
'''
# slicing by z values
clusterCloud = clusterCloud[(clusterCloud[:,2] >= z_for_slicing - 0.07)]#0.15
clusterCloud = clusterCloud[(clusterCloud[:,2] <= z_for_slicing + 0.07)]
x_max = np.max(clusterCloud[:,0])
x_min = np.min(clusterCloud[:,0])
y_max = np.max(clusterCloud[:,1])
y_min = np.min(clusterCloud[:,1])
x_len = abs(x_min - x_max)
y_len = abs(y_min - y_max)
z_len = abs(z_min - z_max)
if carx_min < x_len < carx_max and cary_min < y_len < cary_max and carz_min < z_len < carz_max:
car_count += 1
# Convert Numpy to Pointcloud
clusterCloud_pcd = o3d.geometry.PointCloud()
clusterCloud_pcd.points = o3d.utility.Vector3dVector(clusterCloud)
convexhull = clusterCloud[(clusterCloud_pcd.compute_convex_hull()[1])[:],:]
clusterCloud_2D = convexhull[:,0:2]
points_x = clusterCloud_2D[:,0]
points_y = clusterCloud_2D[:,1]
# Line Segmentation to extract two lines
inliers1_list, outliers1_list = seg.RansacLine(clusterCloud_2D, 120, 0.1)
line1_inliers = clusterCloud_2D[inliers1_list[:], :]
line1_outliers = clusterCloud_2D[outliers1_list[:], :]
#outliers = clusterCloud_2D[outliers1_list[:],:]
inliers2_list, outliers2_list = seg.RansacLine(line1_outliers, 60, 0.2)
line2_inliers = line1_outliers[inliers2_list[:],:]
############################################################################
#######################################Linear Regression ###################
############################################################################
line_fitter1 = LinearRegression()
line_fitter2 = LinearRegression()
len1 = len(line1_inliers[:][:,0])
len2 = len(line2_inliers[:][:,0])
xline1 = line1_inliers[:][:,0].reshape(len1,1)
yline1 = line1_inliers[:][:,1].reshape(len1,1)
xline2 = line2_inliers[:][:,0].reshape(len2,1)
yline2 = line2_inliers[:][:,1].reshape(len2,1)
line1_fit = line_fitter1.fit(xline1,yline1)
line2_fit = line_fitter2.fit(xline2,yline2)
line1dy = line1_fit.coef_
# line1bias = line1_fit.intercept_
line1pred = line1_fit.predict(xline1).reshape([len1,1])
line2dy = line2_fit.coef_
line2pred = line2_fit.predict(xline2).reshape([len2,1])
line1dict = {}
line2dict = {}
for i in range(0,len1):
line1dict[line1_inliers[i][0]] = line1_inliers[i][:]
for i in range(0,len2):
line2dict[line2_inliers[i][0]] = line2_inliers[i][:]
line1dict_sorted = sorted(line1dict.items())
line2dict_sorted = sorted(line2dict.items())
len1 = len(line1dict_sorted)
len2 = len(line2dict_sorted)
line1_sorted = np.empty([0,2])
line2_sorted = np.empty([0,2])
for j in range(0,len1):
line1_sorted = np.append(line1_sorted, [line1dict_sorted[j][1]],axis = 0)
for j in range(0, len2):
line2_sorted = np.append(line2_sorted, [line2dict_sorted[j][1]],axis = 0)
x1, y1 = line1_sorted[0][0], line1_sorted[0][1]
x2, y2 = line1_sorted[len1-1][0], line1_sorted[len1-1][1]
x3, y3 = line2_sorted[0][0], line2_sorted[0][1]
x1x3 = ((x1-x3)**2+(y1-y3)**2)**0.5
x2x3 = ((x2-x3)**2+(y2-y3)**2)**0.5
if(x1x3 < 0.4):
x3, y3 = line2_sorted[len2-1][0], line2_sorted[len2-1][1]
delx, dely = x3-x1, y3-y1
x4 = x2+delx
y4 = y2+dely
centroid_x = (x1+x2+x3+x4)/4
centroid_y = (y1+y2+y3+y4)/4
w = x1x3
elif(x2x3 < 0.4):
x3, y3 = line2_sorted[len2-1][0], line2_sorted[len2-1][1]
delx, dely = x3-x2, y3-y2
x4 = x1+delx
y4 = y1+dely
centroid_x = (x1+x2+x3+x4)/4
centroid_y = (y1+y2+y3+y4)/4
w = x2x3
else:
if(x1x3 < x2x3):
centroid_x = (x2+x3)/2
centroid_y = (y2+y3)/2
w = x1x3
else:
centroid_x = (x1+x3)/2
centroid_y = (y1+y3)/2
w = x2x3
yaw = get_angle([1,line1dy])
l = (abs(x1-x2)**2+abs(y1-y2)**2)**0.5
h = z_max - z_min + 0.5
if(l<w):
temp = w
w = l
l = temp
yaw = get_angle([1,line2dy])
ang1 = get_angle([1, line1dy])*180/pi
ang2 = get_angle([1, line2dy])*180/pi
print(abs(ang1-ang2))
if(65<abs(ang1-ang2)<110 ):
res = np.append(res, [[centroid_x,centroid_y, yaw, w, l, h]], axis = 0)
# plt.figure()
# plt.plot(points_x, points_y, 'g*')
# plt.plot(line1_inliers[:,0],line1_inliers[:,1], 'o')
# plt.plot(line2_inliers[:,0],line2_inliers[:,1], 'ro')
# plt.scatter(centroid_x,centroid_y,color ='blue')
# plt.show()
'''
temp_pcd = o3d.geometry.PointCloud()
temp_pcd.points = o3d.utility.Vector3dVector(clusterCloud)
if i%3 == 0:
temp_pcd.paint_uniform_color([1,0,0])
elif i%3 == 1:
temp_pcd.paint_uniform_color([0,1,0])
else:
temp_pcd.paint_uniform_color([0,0,1])
vis.add_geometry(temp_pcd)
vis.run()
'''
########################################################################
############################## Tracking ################################
########################################################################
print("how many meaured?" , len(res))
print(res[:])
# z_meas == res
z_processed = np.zeros(len(res))
########## Track Update #############
if Track_list:
for i in range(0,len(Track_list)):
Track_list[i].unscented_kalman_filter(res, z_processed, dt)
########## Create Track #############
for i in range(0, len(z_processed)):
if z_processed[i] == 1:
continue
# z_meas[i] that are not used : Create new track
Track = track(res[i])
Track_list.append(Track)
########## Track Management #########
if Track_list:
for i in range(0, len(Track_list)):
# Activate Track
if Track_list[i].Activated == 0 and Track_list[i].Age >= 5:
Track_list[i].Activated = 1
# deActivate Track
if Track_list[i].Activated == 1 and Track_list[i].DelCnt >= 5:
Track_list[i].Activated = 0
# Delete Track
if Track_list[i].DelCnt >= 20:
del Track_list[i]
# Initialize Tracks' processed check
Track_list[i].processed = 0
# Visualization
plt.figure()
plt.xlim(-20,20)
plt.ylim(-20,20)
plt.plot(res[:,0], res[:,1], 'ro')
for i in range(0, len(Track_list)):
plt.plot(Track_list[i].state[0], Track_list[i].state[1], 'b*')
plt.text(Track_list[i].state[0], Track_list[i].state[1], 'Track{}'.format(i+1))
plt.show()
for i in range(0, len(Track_list)):
print("Track value: ".format(i), Track_list[i].state)
pre_time_stamp = time_stamp
num += 1
input("Press Enter to continue...")
#vis.clear_geometries()
#vis.destroy_window()