-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvilinear_homography.py
1699 lines (1261 loc) · 72.1 KB
/
curvilinear_homography.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 12 09:58:31 2022
@author: derek
"""
import os
import _pickle as pickle
import pandas as pd
import numpy as np
import torch
import glob
import cv2
import time
import string
import re
import copy
import sys
from scipy import interpolate
#%% Utility functions
def line_to_point(line,point):
"""
Given a line defined by two points, finds the distance from that line to the third point
line - (x0,y0,x1,y1) as floats
point - (x,y) as floats
Returns
-------
distance - float >= 0
"""
numerator = np.abs((line[2]-line[0])*(line[1]-point[1]) - (line[3]-line[1])*(line[0]-point[0]))
denominator = np.sqrt((line[2]-line[0])**2 +(line[3]-line[1])**2)
return numerator / (denominator + 1e-08)
class Curvilinear_Homography():
def safe_name(func):
"""
Wrapper function, catches camera names that aren't capitalized
"""
def new_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except KeyError:
#print(args,kwargs)
if type(kwargs["name"]) == list:
kwargs["name"] = [item.upper() for item in kwargs["name"]]
elif type(kwargs["name"]) == str:
kwargs["name"] = kwargs["name"].upper()
return func(*args, **kwargs)
return new_func
#%% Initialization and Setup Functions
"""
3 coordinate systems are utilized in Curvilinear_Homography:
-image coordinates
- space coordinates (state plane coordinates) in feet
- roadway coordianates / curvilinear coordinates in feet
After fitting, each value of self.correspondence contains:
H - np array of size [3,3] used for image to space perspective transform
H_inv - used for space to image perspective transform on ground plane
P - np array of size [3,4] used for space to image transform
corr_pts - list of [x,y] points in image space that are fit for transform
space_pts - corresponding list of [x,y] points in space (state plane coordinates in feet)
state_plane_pts - same as space_pts but [x,y,id] (name is included)
vps - z vanishing point [x,y] in image coordinates
extents - xmin,xmax,ymin,ymax in roadway coordinates
extents_space - list of array of [x,y] points defining boundary in state plane coordinates
"""
def __init__(self,
save_file = None,
space_dir = None,
im_dir = None):
"""
Initializes homography object.
save_file - None or str - if str, specifies path to cached homography object
space_dir - None or str - path to directory with csv files of attributes labeled in space coordinates
im_dir - None or str - path to directory with cpkl files of attributes labeled in image coordinates
"""
# intialize correspondence
self.correspondence = {}
if save_file is not None and os.path.exists(save_file):
with open(save_file,"rb") as f:
# everything in correspondence is pickleable without object definitions to allow compatibility after class definitions change
self.correspondence,self.median_tck,self.median_u,self.guess_tck = pickle.load(f)
# reload parameters of curvilinear axis spline
# rather than the spline itself for better pickle reloading compatibility
elif space_dir is None or im_dir is None:
raise IOError("Either save_file or space_dir and im_dir must not be None")
else:
self.generate(space_dir,im_dir)
# fit the axis spline once and collect extents
self._fit_spline(space_dir)
#self._get_extents()
self.save("new_hg_save.cpkl")
#self._cache_spline_points()
# object class info doesn't really belong in homography but it's unclear
# where else it should go, and this avoids having to pass it around
# for use in height estimation
self.class_dims = {
"sedan":[16,6,4],
"midsize":[18,6.5,5],
"van":[20,6,6.5],
"pickup":[20,6,5],
"semi":[55,9,14],
"truck (other)":[25,9,14],
"truck": [25,9,14],
"motorcycle":[7,3,4],
"trailer":[16,7,3],
"other":[18,6.5,5]
}
self.class_dict = { "sedan":0,
"midsize":1,
"van":2,
"pickup":3,
"semi":4,
"truck (other)":5,
"truck": 5,
"motorcycle":6,
"trailer":7,
0:"sedan",
1:"midsize",
2:"van",
3:"pickup",
4:"semi",
5:"truck (other)",
6:"motorcycle",
7:"trailer"
}
def save(self,save_file):
with open(save_file,"wb") as f:
pickle.dump([self.correspondence,self.median_tck,self.median_u,self.guess_tck],f)
def generate(self,
space_dir,
im_dir,
downsample = 1,
max_proj_error = 0.25,
scale_factor = 3,
ADD_PROJ = False):
"""
Loads all available camera homographies from the specified paths.
after running, self.correspondence is a dict with one key for each <camera>_<direction>
space_dir - str - path to directory with csv files of attributes labeled in space coordinates
im_dir - str - path to directory with cpkl files of attributes labeled in image coordinates
downsample - int - specifies downsampling ratio for image coordinates
max_proj_error - float - max allowable positional error (ft) between point and selected corresponding point on spline,
lower will exclude more points from homography computation
scale_factor - float - sampling frequency (ft) along spline, lower is slower but more accurate
ADD_PROJ - bool - if true, compute points along yellow line to use in homography
"""
print("Generating homography")
ae_x = []
ae_y = []
ae_id = []
for direction in ["EB","WB"]:
### State space, do once
for file in os.listdir(space_dir):
if direction.lower() not in file:
continue
# load all points
dataframe = pd.read_csv(os.path.join(space_dir,file))
try:
dataframe = dataframe[dataframe['point_pos'].notnull()]
attribute_name = file.split(".csv")[0]
feature_idx = dataframe["point_id"].tolist()
st_id = [attribute_name + "_" + item for item in feature_idx]
st_x = dataframe["st_x"].tolist()
st_y = dataframe["st_y"].tolist()
ae_x += st_x
ae_y += st_y
ae_id += st_id
except:
dataframe = dataframe[dataframe['side'].notnull()]
attribute_name = file.split(".csv")[0]
feature_idx = dataframe["id"].tolist()
side = dataframe["side"].tolist()
st_id = [attribute_name + str(side[i]) + "_" + str(feature_idx[i]) for i in range(len(feature_idx))]
st_x = dataframe["st_x"].tolist()
st_y = dataframe["st_y"].tolist()
ae_x += st_x
ae_y += st_y
ae_id += st_id
# Find a-d end point of all d2 lane markers
d2 = {}
d3 = {}
ae_spl_x = []
ae_spl_y = []
for i in range(len(ae_x)):
if "d2" in ae_id[i]:
if ae_id[i].split("_")[-1] in ["a","d"]:
num = ae_id[i].split("_")[-2]
if num not in d2.keys():
d2[num] = [(ae_x[i],ae_y[i])]
else:
d2[num].append((ae_x[i],ae_y[i]))
elif "d3" in ae_id[i]:
if ae_id[i].split("_")[-1] in ["a","d"]:
num = ae_id[i].split("_")[-2]
if num not in d3.keys():
d3[num] = [(ae_x[i],ae_y[i])]
else:
d3[num].append((ae_x[i],ae_y[i]))
elif "yeli" in ae_id[i]:
ae_spl_x.append(ae_x[i])
ae_spl_y.append(ae_y[i])
# stack d2 and d3 into arrays
d2_ids = []
d2_values = []
for key in d2.keys():
val = d2[key]
d2_ids.append(key)
d2_values.append( [(val[0][0] + val[1][0])/2.0 , (val[0][1] + val[1][1])/2.0 ])
d3_ids = []
d3_values = []
for key in d3.keys():
val = d3[key]
d3_ids.append(key)
d3_values.append( [(val[0][0] + val[1][0])/2.0 , (val[0][1] + val[1][1])/2.0 ])
d2_values = torch.from_numpy(np.stack([np.array(item) for item in d2_values]))
d3_values = torch.from_numpy(np.stack([np.array(item) for item in d3_values]))
d2_exp = d2_values.unsqueeze(1).expand(d2_values.shape[0],d3_values.shape[0],2)
d3_exp = d3_values.unsqueeze(0).expand(d2_values.shape[0],d3_values.shape[0],2)
dist = torch.sqrt(torch.pow(d2_exp - d3_exp , 2).sum(dim = -1))
min_matches = torch.min(dist, dim = 1)[1]
if ADD_PROJ:
try:
with open("ae_cache_{}.cpkl".format(direction),"rb") as f:
additional_points = pickle.load(f)
except:
# For each d2 lane marker, find the closest d3 lane marker
proj_lines = []
for i in range(len(min_matches)):
j = min_matches[i]
pline = [d3_values[j],d2_values[i],d3_ids[j],d2_ids[i]]
proj_lines.append(pline)
# compute the yellow line spline in state plane coordinates
ae_data = np.stack([np.array(ae_spl_x),np.array(ae_spl_y)])
ae_data = ae_data[:,np.argsort(ae_data[1,:])]
ae_tck, ae_u = interpolate.splprep(ae_data, s=0, per=False)
span_dist = np.sqrt((ae_spl_x[0] - ae_spl_x[-1])**2 + (ae_spl_y[0] - ae_spl_y[-1])**2)
ae_x_prime, ae_y_prime = interpolate.splev(np.linspace(0, 1, int(span_dist*scale_factor)), ae_tck)
additional_points = []
# for each d2 lane marker, find the intersection between the d2-d3 line and the yellow line spline
for p_idx, proj_line in enumerate(proj_lines):
print("On proj line {} of {}".format(p_idx,len(proj_lines)))
min_dist = np.inf
min_point = None
line = [proj_line[0][0],proj_line[0][1],proj_line[1][0],proj_line[1][1]]
for i in range(len(ae_x_prime)):
point = [ae_x_prime[i],ae_y_prime[i]]
dist = line_to_point(line, point)
if dist < min_dist:
min_dist = dist
min_point = point
if min_dist > max_proj_error:
print("Issue")
else:
name = "{}_{}".format(proj_line[2],proj_line[3])
min_point.append(name)
additional_points.append(min_point)
with open("ae_cache_{}.cpkl".format(direction),"wb") as f:
pickle.dump(additional_points,f)
for point in additional_points:
ae_x.append(point[0])
ae_y.append(point[1])
ae_id.append(point[2])
# get all cameras
cam_data_paths = glob.glob(os.path.join(im_dir,"*.cpkl"))
for cam_data_path in cam_data_paths:
# specify path to camera imagery file
cam_im_path = cam_data_path.split(".cpkl")[0] + ".png"
camera = cam_data_path.split(".cpkl")[0].split("/")[-1]
# load all points
with open(cam_data_path, "rb") as f:
im_data = pickle.load(f)
for direction in ["EB","WB"]:
# get all non-curve matching points
point_data = im_data[direction]["points"]
filtered = filter(lambda x: x[2].split("_")[1] not in ["yeli","yelo","whli","whlo"],point_data)
im_x = []
im_y = []
im_id = []
for item in filtered:
im_x.append(item[0])
im_y.append(item[1])
im_id.append(item[2])
if len(im_x) == 0:
continue
if ADD_PROJ:
# compute the yellow line spline in image coordinates
curve_data = im_data[direction]["curves"]
filtered = filter(lambda x: "yeli" in x[2], curve_data)
x = []
y = []
for item in filtered:
x.append(item[0])
y.append(item[1])
data = np.stack([np.array(x),np.array(y)])
data = data[:,np.argsort(data[0,:])]
tck, u = interpolate.splprep(data, s=0, per=False)
x_prime, y_prime = interpolate.splev(np.linspace(0, 1, 4000), tck)
if False:
im = cv2.imread(cam_im_path)
for i in range(len(x_prime)):
cv2.circle(im,(int(x_prime[i]),int(y_prime[i])), 2, (255,0,0,),-1)
cv2.imshow("frame",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
# find all d2 and d3 points
# find the intersection of each d2d3 line and the yellow line spline for d2-d3 pairs in image
d2 = {}
d3 = {}
for i in range(len(im_x)):
if "d2" in im_id[i]:
if im_id[i].split("_")[-1] in ["a","d"]:
num = im_id[i].split("_")[-2]
if num not in d2.keys():
d2[num] = [(im_x[i],im_y[i])]
else:
d2[num].append((im_x[i],im_y[i]))
elif "d3" in im_id[i]:
if im_id[i].split("_")[-1] in ["a","d"]:
num = im_id[i].split("_")[-2]
if num not in d3.keys():
d3[num] = [(im_x[i],im_y[i])]
else:
d3[num].append((im_x[i],im_y[i]))
# stack d2 and d3 into arrays
d2_ids = []
d2_values = []
for key in d2.keys():
d2[key] = [(d2[key][0][0] + d2[key][1][0])/2.0 , (d2[key][0][1] + d2[key][1][1])/2.0 ]
d3_ids = []
d3_values = []
for key in d3.keys():
d3[key] = [(d3[key][0][0] + d3[key][1][0])/2.0 , (d3[key][0][1] + d3[key][1][1])/2.0 ]
additional_im_points = []
for proj_point in additional_points:
d3_id = proj_point[2].split("_")[0]
d2_id = proj_point[2].split("_")[1]
if d3_id not in d3.keys() or d2_id not in d2.keys():
continue
im_line = [d3[d3_id][0], d3[d3_id][1], d2[d2_id][0], d2[d2_id][1]]
min_dist = np.inf
min_point = None
for i in range(len(x_prime)):
point = [x_prime[i],y_prime[i]]
dist = line_to_point(im_line, point)
if dist < min_dist:
min_dist = dist
min_point = point
if min_dist > 2:
print("Issue")
else:
name = proj_point[2]
min_point.append(name)
additional_im_points.append(min_point)
for point in additional_im_points:
im_x.append(point[0])
im_y.append(point[1])
im_id.append(point[2])
### Joint
# assemble ordered list of all points visible in both image and space
include_im_x = []
include_im_y = []
include_im_id = []
include_ae_x = []
include_ae_y = []
include_ae_id = []
for i in range(len(ae_id)):
for j in range(len(im_id)):
if ae_id[i] == im_id[j]:
include_im_x.append( im_x[j])
include_im_y.append( im_y[j])
include_im_id.append(im_id[j])
include_ae_x.append( ae_x[i])
include_ae_y.append( ae_y[i])
include_ae_id.append(ae_id[i])
# compute homography
vp = im_data[direction]["z_vp"]
corr_pts = np.stack([np.array(include_im_x),np.array(include_im_y)]).transpose(1,0)
space_pts = np.stack([np.array(include_ae_x),np.array(include_ae_y)]).transpose(1,0)
if len(corr_pts) < 4 or len(space_pts) < 4:
continue
cor = {}
#cor["vps"] = vp
cor["corr_pts"] = corr_pts
cor["space_pts"] = space_pts
cor["H"],_ = cv2.findHomography(corr_pts,space_pts)
cor["H_inv"],_ = cv2.findHomography(space_pts,corr_pts)
# P is a [3,4] matrix
# column 0 - vanishing point for space x-axis (axis 0) in image coordinates (im_x,im_y,im_scale_factor)
# column 1 - vanishing point for space y-axis (axis 1) in image coordinates (im_x,im_y,im_scale_factor)
# column 2 - vanishing point for space z-axis (axis 2) in image coordinates (im_x,im_y,im_scale_factor)
# column 3 - space origin in image coordinates (im_x,im_y,scale_factor)
# columns 0,1 and 3 are identical to the columns of H,
# We simply insert the z-axis column (im_x,im_y,1) as the new column 2
P = np.zeros([3,4])
P[:,0] = cor["H_inv"][:,0]
P[:,1] = cor["H_inv"][:,1]
P[:,3] = cor["H_inv"][:,2]
P[:,2] = np.array([vp[0],vp[1],1]) * 10e-09
cor["P"] = P
self._fit_z_vp(cor,im_data,direction)
cor["state_plane_pts"] = [include_ae_x,include_ae_y,include_ae_id]
cor_name = "{}_{}".format(camera,direction)
self.correspondence[cor_name] = cor
# use other side if no homography defined
if "{}_{}".format(camera,"EB") not in self.correspondence.keys():
if "{}_{}".format(camera,"WB") in self.correspondence.keys():
self.correspondence["{}_{}".format(camera,"EB")] = self.correspondence["{}_{}".format(camera,"WB")]
if "{}_{}".format(camera,"WB") not in self.correspondence.keys():
if "{}_{}".format(camera,"EB") in self.correspondence.keys():
self.correspondence["{}_{}".format(camera,"WB")] = self.correspondence["{}_{}".format(camera,"EB")]
def _fit_z_vp(self,cor,im_data,direction):
print("fitting Z coordinate scale")
P_orig = cor["P"].copy()
max_scale = 10000
granularity = 1e-12
upper_bound = max_scale
lower_bound = -max_scale
# create a grid of 100 evenly spaced entries between upper and lower bound
C_grid = np.linspace(lower_bound,upper_bound,num = 100,dtype = np.float64)
step_size = C_grid[1] - C_grid[0]
iteration = 1
while step_size > granularity:
best_error = np.inf
best_C = None
# for each value of P, get average reprojection error
for C in C_grid:
# scale P
P = P_orig.copy()
P[:,2] *= C
# search for optimal scaling of z-axis row
vp_lines = im_data[direction]["z_vp_lines"]
# get bottom point (point # 2)
points = torch.stack([ torch.tensor([vpl[2] for vpl in vp_lines]),
torch.tensor([vpl[3] for vpl in vp_lines]) ]).transpose(1,0)
t_points = torch.stack([ torch.tensor([vpl[0] for vpl in vp_lines]),
torch.tensor([vpl[1] for vpl in vp_lines]) ]).transpose(1,0)
heights = torch.tensor([vpl[4] for vpl in vp_lines]).unsqueeze(1)
# project to space
d = points.shape[0]
# convert points into size [dm,3]
points = points.view(-1,2).double()
points = torch.cat((points,torch.ones([points.shape[0],1],device=points.device).double()),1) # add 3rd row
H = torch.from_numpy(cor["H"]).transpose(0,1).to(points.device)
new_pts = torch.matmul(points,H)
# divide each point 0th and 1st column by the 2nd column
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
# drop scale factor column
new_pts = new_pts[:,:2]
# reshape to [d,m,2]
new_pts = new_pts.view(d,2)
# add third column for height
new_pts_shifted = torch.cat((new_pts,heights.double()),1)
# add fourth column for scale factor
new_pts_shifted = torch.cat((new_pts_shifted,torch.ones(heights.shape)),1)
new_pts_shifted = torch.transpose(new_pts_shifted,0,1).double()
# project to image
P = torch.from_numpy(P).double().to(points.device)
new_pts = torch.matmul(P,new_pts_shifted).transpose(0,1)
# divide each point 0th and 1st column by the 2nd column
new_pts[:,0] = new_pts[:,0] / new_pts[:,2]
new_pts[:,1] = new_pts[:,1] / new_pts[:,2]
# drop scale factor column
new_pts = new_pts[:,:2]
# reshape to [d,m,2]
repro_top = new_pts.view(d,-1,2).squeeze()
# get error
error = torch.pow((repro_top - t_points),2).sum(dim = 1).sqrt().mean()
# if this is the best so far, store it
if error < best_error:
best_error = error
best_C = C
# define new upper, lower with width 2*step_size centered on best value
#print("On loop {}: best C so far: {} avg error {}".format(iteration,best_C,best_error))
lower_bound = best_C - 2*step_size
upper_bound = best_C + 2*step_size
C_grid = np.linspace(lower_bound,upper_bound,num = 100,dtype = np.float64)
step_size = C_grid[1] - C_grid[0]
#print("New C_grid: {}".format(C_grid.round(4)))
iteration += 1
P_new = P_orig.copy()
P_new[:,2] *= best_C
cor["P"] = P_new
#print("Best Error: {}".format(best_error))
def _fit_spline(self,space_dir,use_MM_offset = False):
"""
Spline fitting is done by:
1. Assemble all points labeled along a yellow line in either direction
2. Fit a spline to each of EB, WB inside and outside
3. Sample the spline at fine intervals
4. Use finite difference method to determine the distance along the spline for each fit point
5. Refit the splines, this time parameterizing the spline by these distances (u parameter in scipy.splprep)
6. Sample each spline at fine intervals
7. Move along one spline and at each point, find the closest point on each other spline
8. Define a point on the median/ midpoint axis as the average on these 4 splines
9. Use the set of median points to define a new spline
10. Use the finite difference method to reparameterize this spline according to distance along it
11. Optionally, compute a median spline distance offset from mile markers
12. Optionally, recompute the same spline, this time accounting for the MM offset
space_dir - str - path to directory with csv files of attributes labeled in space coordinates
use_MM_offset - bool - if True, offset according to I-24 highway mile markers
"""
print("Fitting median spline..")
samples_per_foot = 10
splines = {}
for direction in ["EB","WB"]:
for line_side in ["i","o"]:
### State space, do once
ae_x = []
ae_y = []
ae_id = []
# 1. Assemble all points labeled along a yellow line in either direction
for file in os.listdir(space_dir):
if direction.lower() not in file:
continue
# load all points
dataframe = pd.read_csv(os.path.join(space_dir,file))
try:
dataframe = dataframe[dataframe['point_pos'].notnull()]
attribute_name = file.split(".csv")[0]
feature_idx = dataframe["point_id"].tolist()
st_id = [attribute_name + "_" + item for item in feature_idx]
st_x = dataframe["st_x"].tolist()
st_y = dataframe["st_y"].tolist()
ae_x += st_x
ae_y += st_y
ae_id += st_id
except:
dataframe = dataframe[dataframe['side'].notnull()]
attribute_name = file.split(".csv")[0]
feature_idx = dataframe["id"].tolist()
side = dataframe["side"].tolist()
st_id = [attribute_name + str(side[i]) + "_" + str(feature_idx[i]) for i in range(len(feature_idx))]
st_x = dataframe["st_x"].tolist()
st_y = dataframe["st_y"].tolist()
ae_x += st_x
ae_y += st_y
ae_id += st_id
# Find a-d end point of all d2 lane markers
ae_spl_x = []
ae_spl_y = []
ae_spl_u = [] # u parameterizes distance along spline
for i in range(len(ae_x)):
if "yel{}".format(line_side) in ae_id[i]:
ae_spl_x.append(ae_x[i])
ae_spl_y.append(ae_y[i])
# 2. Fit a spline to each of EB, WB inside and outside
# compute the yellow line spline in state plane coordinates (sort points by y value since road is mostly north-south)
ae_data = np.stack([np.array(ae_spl_x),np.array(ae_spl_y)])
ae_data = ae_data[:,np.argsort(ae_data[1,:],)[::-1]]
# 3. Sample the spline at fine intervals
# get spline and sample points on spline
ae_tck, ae_u = interpolate.splprep(ae_data, s=0, per=False)
span_dist = np.sqrt((ae_spl_x[0] - ae_spl_x[-1])**2 + (ae_spl_y[0] - ae_spl_y[-1])**2)
ae_x_prime, ae_y_prime = interpolate.splev(np.linspace(0, 1, int(span_dist*samples_per_foot)), ae_tck)
# 4. Use finite difference method to determine the distance along the spline for each fit point
fd_dist = np.concatenate( (np.array([0]), ((ae_x_prime[1:] - ae_x_prime[:-1])**2 + (ae_y_prime[1:] - ae_y_prime[:-1])**2)**0.5),axis = 0) # by convention fd_dist[0] will be 0, so fd_dist[i] = sum(int_dist[0:i])
integral_dist = np.cumsum(fd_dist)
# for each fit point, find closest point on spline, and assign it the corresponding integral distance
for p_idx in range(len(ae_spl_x)):
px = ae_spl_x[p_idx]
py = ae_spl_y[p_idx]
dist = ((ae_x_prime - px)**2 + (ae_y_prime - py)**2)**0.5
min_dist,min_idx= np.min(dist),np.argmin(dist)
ae_spl_u.append(integral_dist[min_idx])
# 5. Refit the splines, this time parameterizing the spline by these distances (u parameter in scipy.splprep)
#ae_spl_u.reverse()
tck, u = interpolate.splprep(ae_data.astype(float), s=0, u = ae_spl_u)
splines["{}_{}".format(direction,line_side)] = [tck,u]
# 6. Sample each spline at fine intervals
for key in splines:
tck,u = splines[key]
span_dist = np.abs(u[0] - u[-1])
x_prime, y_prime = interpolate.splev(np.linspace(u[0], u[-1], int(3*span_dist)), tck)
splines[key].append(x_prime)
splines[key].append(y_prime)
med_spl_x = []
med_spl_y = []
# 7. Move along one spline and at each point, find the closest point on each other spline
# by default, we'll use EB_o as the base spline
main_key = "EB_o"
main_spl = splines[main_key]
main_x = main_spl[2]
main_y = main_spl[3]
for p_idx in range(len(main_x)):
points_to_average = [np.array([px,py])]
px,py = main_x[p_idx],main_y[p_idx]
for key in splines:
if key != main_key:
arr_x,arr_y = splines[key][2], splines[key][3]
dist = np.sqrt((arr_x - px)**2 + (arr_y - py)**2)
min_dist,min_idx= np.min(dist),np.argmin(dist)
points_to_average.append( np.array([arr_x[p_idx],arr_y[p_idx]]))
med_point = sum(points_to_average)/len(points_to_average)
# 8. Define a point on the median/ midpoint axis as the average on these 4 splines
med_spl_x.append(med_point[0])
med_spl_y.append(med_point[1])
# 9. Use the set of median points to define a new spline
med_data = np.stack([np.array(med_spl_x),np.array(med_spl_y)])
med_tck,med_u = interpolate.splprep(med_data, s=0, per=False)
# 10. Use the finite difference method to reparameterize this spline according to distance along it
span_dist = np.sqrt((med_spl_x[0] - med_spl_x[-1])**2 + (med_spl_y[0] - med_spl_y[-1])**2)
med_x_prime, med_y_prime = interpolate.splev(np.linspace(0, 1, int(span_dist*samples_per_foot)), med_tck)
med_fd_dist = np.concatenate( (np.array([0]), ((med_x_prime[1:] - med_x_prime[:-1])**2 + (med_y_prime[1:] - med_y_prime[:-1])**2)**0.5),axis = 0) # by convention fd_dist[0] will be 0, so fd_dist[i] = sum(int_dist[0:i])
med_integral_dist = np.cumsum(med_fd_dist)
# for each fit point, find closest point on spline, and assign it the corresponding integral distance
med_spl_u = []
for p_idx in range(len(med_spl_x)):
px = med_spl_x[p_idx]
py = med_spl_y[p_idx]
dist = ((med_x_prime - px)**2 + (med_y_prime - py)**2)**0.5
min_dist,min_idx= np.min(dist),np.argmin(dist)
med_spl_u.append(med_integral_dist[min_idx])
final_tck, final_u = interpolate.splprep(ae_data.astype(float), s=0, u = ae_spl_u)
self.median_tck = final_tck
self.median_u = final_u
# get the inverse spline g(x) = u for guessing initial spline point
ae_spl_u = np.array(ae_spl_u)
print(ae_data.shape,ae_spl_u.shape)
self.guess_tck = interpolate.splrep(ae_data[0],ae_spl_u)
if use_MM_offset:
# 11. Optionally, compute a median spline distance offset from mile markers
self.MM_offset = self._fit_MM_offset()
# 12. Optionally, recompute the same spline, this time accounting for the MM offset
ae_spl_u += self.MM_offset
final_tck, final_u = interpolate.splprep(ae_data.astype(float), s=0, u = ae_spl_u)
self.median_tck = final_tck
self.median_u = final_u
# def _cache_spline_points(self,granularity = 0.1):
# """
# Caches u,x, and y for a grid with specified granularity
# granularity - float
# RETURN: None, but sets self.spline_cache as [n,3] tensor of u,x,y
# """
# umin = min(self.median_u)
# umax = max(self.median_u)
# count = int((umax-umin)*1/granularity)
# u_prime = np.linspace(umin,umax,count)
# med_x_prime, med_y_prime = interpolate.splev(u_prime, self.median_tck)
# self.spline_cache = torch.stack([torch.from_numpy(arr) for arr in [u_prime,med_x_prime,med_y_prime]])
def closest_spline_point(self,points, epsilon = 0.01, max_iterations = 100):
"""
Given a tensor of points in 3D space, find the closest point on the median spline
for each point as follows:
1. Query self.guess_tck spline to get f(x) = u initial guess
2. Use Newton's method to find the point where dist = min
points - [d,3] tensor of points in state plane coordinates
epsilon - float - keep iterating while max change > epsilon or ii.
max_iterations - int - ii. keep iterating until n_iterations = max_iterations
RETURNS: [d] tensor of coordinates along spline axis
"""
start = time.time()
# intial guess at closest u values
points = points.data.numpy()
guess_u = interpolate.splev(points[:,0],self.guess_tck)
it = 0
max_change = np.inf
while it < max_iterations and max_change > epsilon:
spl_x,spl_y = interpolate.splev(guess_u,self.median_tck)
spl_xx,spl_yy = interpolate.splev(guess_u,self.median_tck, der = 1)
spl_xxx,spl_yyy = interpolate.splev(guess_u,self.median_tck, der = 2)
dist_proxy = (spl_x - points[:,0])**2 + (spl_y - points[:,1])**2
dist_proxy_deriv = (spl_x-points[:,0])*spl_xx + (spl_y-points[:,1])*spl_yy
dist_proxy_deriv2 = (2*spl_xx**2)+2*(spl_x-points[:,0])*spl_xxx + (2*spl_yy**2)+2*(spl_y-points[:,1])*spl_yyy
new_u = guess_u - dist_proxy_deriv/dist_proxy_deriv2
max_change = np.max(np.abs(new_u-guess_u))
it += 1
guess_u = new_u
#print("Max step: {}".format(max_change))
#print("Newton method took {}s for {} points".format(time.time() - start,points.shape[0]))
return guess_u
def _fit_MM_offset(self):
return 0
def _generate_extents_file(self,im_dir,output_path = "cam_extents.config"):
"""
Produce a text file as utilized by tracking with name=xmin,xmax,ymin,ymax for each camera
im_dir - str - path to directory with cpkl files of attributes labeled in image coordinates
output_path - str - desired output .config file, defaulting to current directory
RETURN: None
"""
# 1. load all extent image points into a dictionary per side
# 2. convert all extent points into state coordinates
# 3. Find min enclosing extents for each camera
# 4. Look for gaps
# 5. write extents to output file
data = {}
# 1. load all extent image points into a dictionary per side
# get all cameras
cam_data_paths = glob.glob(os.path.join(im_dir,"*.cpkl"))
for cam_data_path in cam_data_paths:
# specify path to camera imagery file
#cam_im_path = cam_data_path.split(".cpkl")[0] + ".png"
camera = cam_data_path.split(".cpkl")[0].split("/")[-1]
# load all points
with open(cam_data_path, "rb") as f:
im_data = pickle.load(f)
for direction in ["EB","WB"]:
fov_data = im_data[direction]["FOV"]
if len(fov_data) > 0:
fov_data = torch.stack([torch.tensor([item[0],item[1]]) for item in fov_data])
data[camera + "_" + direction] = fov_data
# 2. convert all extent points into state coordinates
for key in data.keys():
if key not in self.correspondence.keys():
continue
key_data = data[key]
name = [key.split("_")[0] for _ in key_data]
data[key] = self.im_to_state(key_data.float().unsqueeze(1),name = name, heights = 0, refine_heights = False)
# 3. Find min enclosing extents for each camera
extents = {}
for key in data.keys():
key_data = data[key]
minx = torch.min(key_data[:,0]).item()
maxx = torch.max(key_data[:,0]).item()
miny = torch.min(key_data[:,1]).item()
maxy = torch.max(key_data[:,1]).item()
extents[key] = [minx,maxx,miny,maxy]
# 4. Look for gaps
if False:
minx_total = min([extents[key][0] for key in extents.keys()])
maxx_total = max([extents[key][1] for key in extents.keys()])
miny_total = min([extents[key][2] for key in extents.keys()])
maxy_total = max([extents[key][3] for key in extents.keys()])
extents_im = np.zeros([int(maxx_total - minx_total),int(maxy_total - miny_total)]).astype(np.uint8)
for cam_fov in extents.values():
cv2.rectangle(extents_im,(int(cam_fov[0]),int(cam_fov[1])),(int(cam_fov[2]),int(cam_fov[3])),(255,255,0),-1)
scale = extents_im.shape[0]/2000
res = (int(extents_im.shape[0]//scale), int(extents_im.shape[1]//scale))
extents_im = cv2.resize(extents_im,res)
cv2.imshow("Extents",extents_im)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 5. write extents to output file
keys = list(extents.keys())
keys.sort()
with open(output_path,"w",encoding='utf-8') as f:
for key in keys:
key_data = extents[key]
line = "{}={},{},{},{}\n".format(key,int(key_data[0]),int(key_data[1]),int(key_data[2]),int(key_data[3]))
f.write(line)
def _generate_mask_images(self,im_dir,mask_save_dir = "mask"):
cam_data_paths = glob.glob(os.path.join(im_dir,"*.cpkl"))
for cam_data_path in cam_data_paths:
# specify path to camera imagery file
#cam_im_path = cam_data_path.split(".cpkl")[0] + ".png"
camera = cam_data_path.split(".cpkl")[0].split("/")[-1]