-
Notifications
You must be signed in to change notification settings - Fork 0
/
gait_from_lidar.py
executable file
·1478 lines (1162 loc) · 57.5 KB
/
gait_from_lidar.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
import os
import json
from itertools import groupby
from skimage.io import imread, imshow
from skimage import io
from skimage.draw import disk
from skimage.morphology import (erosion, dilation, closing, opening, area_closing, area_opening)
from skimage.color import rgb2gray
from sklearn import metrics
from sklearn.cluster import DBSCAN
from sklearn.cluster import KMeans
from scipy.interpolate import BSpline, make_interp_spline
from scipy.cluster.hierarchy import fclusterdata
from scipy.signal import find_peaks, medfilt
from collections import defaultdict
import numpy as np
import math
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
matplotlib.use('Agg')
class LidarPreprocessor:
degrees1 = 268
phi1 = (np.pi * degrees1) / 180.0
SIN1 = np.sin(phi1)
COS1 = np.cos(phi1)
tx1 = 3833 + 85 #+ 108
ty1 = 3497 + 35 #- 174
HH1 = np.array([
[COS1, -SIN1, tx1],
[SIN1, COS1, ty1],
[0, 0, 1]
])
degrees2 = 180 # 183
phi2 = (np.pi * degrees2) / 180.0
SIN2 = np.sin(phi2)
COS2 = np.cos(phi2)
tx2 = 7730 # 7859 #+ 108
ty2 = 3525-340 # 3226 #- 174
HH2 = np.array([
[COS2, -SIN2, tx2],
[SIN2, COS2, ty2],
[0, 0, 1]
])
STRUCTURING_ELEMENT = np.array([
[0,1,0],
[1,1,1],
[0,1,0]
])
SE_RADIUS = 3
@staticmethod
def merge_lidar_dataframes(dfs):
df1, df2, df3 = dfs
# merge dataframes on timestamps
df_merged = pd.merge_asof(df1, df2, on='timestamp')
df_merged = pd.merge_asof(df_merged, df3, on='timestamp')
df_merged = df_merged.rename(columns={
"fgx_x": "fgx_df1",
"fgy_x": "fgy_df1",
"fgx_y": "fgx_df2",
"fgy_y": "fgy_df2",
"fgx": "fgx_df3",
"fgy": "fgy_df3",
"x_x": "x_df1",
"y_x": "y_df1",
"x_y": "x_df2",
"y_y": "y_df2",
"x": "x_df3",
"y": "y_df3",
})
return df_merged
# TODO: rename to calculate_affine_transformation
@staticmethod
def calculate_homogeneous_transformation(points_im1, points_im2):
'''
Computes Homogeneous transformation H. Points from im1 are transformed to the coordinate system of im2.
To compute H, provide 3 point correspondences.
@param points_im1 [collection<number>] 3 points
@param points_im2 [collection<number>] 3 points
@return 3x3 Homogeneous transformation
'''
x1, y1, x2, y2, x3, y3 = points_im1
A = [
[x1, y1, 1, 0, 0, 0],
[0, 0, 0, x1, y1, 1],
[x2, y2, 1, 0, 0, 0],
[0, 0, 0, x2, y2, 1],
[x3, y3, 1, 0, 0, 0],
[0, 0, 0, x3, y3, 1]
]
A = np.array(A, dtype=np.float32)
v = np.array(points_im2)
Asq = np.matmul(A.transpose(), A)
Asq_inv = np.linalg.inv(Asq)
A_pseud = np.matmul(Asq_inv, A.transpose())
a, b, c, d, e, f = A_pseud.dot(v)
H = [
[a, b, c],
[d, e, f],
[0, 0, 1]
]
H = np.array(H, dtype=np.float32)
return H#np.linalg.inv(H)
@staticmethod
def calculate_rigid_transformation(points_im1, points_im2):
'''
Computes Homogeneous transformation H. Points from im1 are transformed to the coordinate system of im2.
To compute H, provide 3 point correspondences.
@param points_im1 [collection<number>] 3 points
@param points_im2 [collection<number>] 3 points
@return 3x3 Homogeneous transformation
'''
x1, y1, x2, y2 = points_im1
A = [
[x1, -y1, 1, 0],
[y1, x1, 0, 1],
[x2, -y2, 1, 0],
[y2, x2, 0, 1],
]
A = np.array(A, dtype=np.float32)
v = np.array(points_im2)
Asq = np.matmul(A.transpose(), A)
Asq_inv = np.linalg.inv(Asq)
A_pseud = np.matmul(Asq_inv, A.transpose())
a, b, c, d = A_pseud.dot(v)
H = [
[a, -b, c],
[b, a, d],
[0, 0, 1]
]
H = np.array(H, dtype=np.float32)
return H#np.linalg.inv(H)
@staticmethod
def apply_homogeneous_transformation(xs, ys, H):
'''
@param xs coordnates
@param ys coordinates
@param H homogeneous transformation
@return algined coordinates
'''
xy = np.array([xs, ys]).T
xy = np.hstack((xy, np.ones((xy.shape[0], 1))))
xy = np.dot(xy, H.T)
xy = xy[:, 0:2] # remove the z-coordinates
return xy[:,0], xy[:,1]
@staticmethod
def align_dataframes(df_merged, frame_id, use_foreground=True):
column_name1 = "fgx_df1"
column_name2 = "fgy_df1"
column_name3 = "fgx_df2"
column_name4 = "fgy_df2"
if not use_foreground:
column_name1 = "x_df1"
column_name2 = "y_df1"
column_name3 = "x_df2"
column_name4 = "y_df2"
H21 = LidarPreprocessor.calculate_homogeneous_transformation(ps11, ps21)
H31 = LidarPreprocessor.calculate_homogeneous_transformation(ps1, ps2)
aligned_xs1x, aligned_ys1x = LidarPreprocessor.apply_homogeneous_transformation(
df_merged.iloc[frame_id][column_name1], df_merged.iloc[frame_id][column_name2], H21
)
aligned_xs2x, aligned_ys2x = LidarPreprocessor.apply_homogeneous_transformation(
df_merged.iloc[frame_id][column_name3], df_merged.iloc[frame_id][column_name4], H31
)
return aligned_xs1x, aligned_ys1x, aligned_xs2x, aligned_ys2x
@staticmethod
def align_dataframes_with_homographies(df_merged, frame_id, H1, H2, use_foreground=True):
column_name1 = "fgx_df1"
column_name2 = "fgy_df1"
column_name3 = "fgx_df2"
column_name4 = "fgy_df2"
if not use_foreground:
column_name1 = "x_df1"
column_name2 = "y_df1"
column_name3 = "x_df2"
column_name4 = "y_df2"
aligned_xs1x, aligned_ys1x = LidarPreprocessor.apply_homogeneous_transformation(
df_merged.iloc[frame_id][column_name1], df_merged.iloc[frame_id][column_name2], H1
)
aligned_xs2x, aligned_ys2x = LidarPreprocessor.apply_homogeneous_transformation(
df_merged.iloc[frame_id][column_name3], df_merged.iloc[frame_id][column_name4], H2
)
return aligned_xs1x, aligned_ys1x, aligned_xs2x, aligned_ys2x
@staticmethod
def multi_dil(im, num, element=STRUCTURING_ELEMENT):
for i in range(num):
im = dilation(im, element)
return im
@staticmethod
def multi_ero(im, num, element=STRUCTURING_ELEMENT):
for i in range(num):
im = erosion(im, element)
return im
@staticmethod
def multi_closing(im, num, element=STRUCTURING_ELEMENT):
for i in range(num):
im = closing(im, element)
return im
@staticmethod
def multi_opening(im, num, element=STRUCTURING_ELEMENT):
for i in range(num):
im = opening(im, element)
return im
@staticmethod
def create_frame_imgs_from_df(df, save_path, number_of_images=100):
'''
@param frames
@param save_path "p64/L1904940"
'''
if not os.path.exists(save_path):
os.mkdir(save_path)
print(f"Directory {save_path} created")
for fdx in range(number_of_images):
row = df.iloc[fdx]
x, y = row["x"], row["y"]
fig = plt.figure()
plt.axis('off')
plt.scatter(x, y, alpha=1)
plt.savefig(os.path.join(save_path, f"frame{fdx}.jpg"))
plt.close(fig)
return save_path
@staticmethod
def load_lidar_scan_images(base_path, img_count=100):
'''
@param fileapth "p64/L1904940/"
'''
return [io.imread(os.path.join(base_path, f"frame{idx}.jpg")) for idx in range(img_count)]
@staticmethod
def remove_background_from_frames_opt(df, mask_aaa, mask_bbb):
x_coordinates, y_coordinates = df["x"].to_numpy(), df["y"].to_numpy()
minimal_expected_distance_mm = 300
def remove_background(x, y):
_xss = np.zeros(len(x))
_yss = np.zeros(len(y))
# compute this using tensors
for pix, p in enumerate(zip(x, y)):
v = np.min(np.sqrt((mask_aaa - p[0])**2 + (mask_bbb - p[1])**2))
if v > minimal_expected_distance_mm:
_xss[pix] = x[pix]
_yss[pix] = y[pix]
return _xss, _yss
clean_xs = []
clean_ys = []
for k in range(len(x_coordinates)):
clean_x, clean_y = remove_background(x_coordinates[k], y_coordinates[k])
print(f"Removed background for frame {k+1} / {len(x_coordinates)}")
clean_xs.append(clean_x)
clean_ys.append(clean_y)
return clean_xs, clean_ys
@staticmethod
def compute_background_mask(df, imgs, min_density=0.4, max_density=0.6, img_count=100):
erodeds = []
for idx in range(img_count):
img = imgs[idx]
binary = rgb2gray(img)
eroded = LidarPreprocessor.multi_closing(
binary, LidarPreprocessor.SE_RADIUS, LidarPreprocessor.STRUCTURING_ELEMENT
)
erodeds.append(eroded)
min_x = 10_000
min_y = 10_000
max_x = -1
max_y = -1
for fdx in range(img_count):
row = df.iloc[fdx]
x, y = row["x"], row["y"]
if np.min(x) < min_x:
min_x = np.min(x)
if np.min(y) < min_y:
min_y = np.min(y)
if np.max(x) > max_x:
max_x = np.max(x)
if np.max(y) > max_y:
max_y = np.max(y)
mean_erodeds = np.mean(erodeds, axis=0)
T = (1 - mean_erodeds)
T = (T > min_density) & (T < max_density)
# obtain nonzero coordinates of 270 clock-wise rotated mask.
a, b = np.rot90(T, 3).nonzero()
# transform index coordinates (a, b) to [0, 1]^2 space
aa = a - np.min(a)
aa = aa / np.max(aa)
bb = b - np.min(b)
bb = bb / np.max(bb)
# transform [0, 1] index coordinates to [min_x, max_x] x [min_y, max_y] space
aaa = aa * (max_x - min_x) + (min_x)
bbb = bb * (max_y - min_y) + (min_y)
mask = np.array(zip(aaa, bbb))
return aaa, bbb, mask
@staticmethod
def compute_xy_for_df(df):
frames = df["sample"].to_numpy()
step_nb = len(frames[0])
angle_min = math.radians((-270 / 2))
angle_increment = math.radians(270) / step_nb
angles = angle_min + np.arange(step_nb) * angle_increment
xs = []
ys = []
max_scan_distance_mm = 7_000
for frame in frames:
filtered_frame = frame.copy()
filtered_frame[np.where(filtered_frame > max_scan_distance_mm)] = 0
xy = filtered_frame * np.array([np.cos(angles), np.sin(angles)])
xs.append(xy[0])
ys.append(xy[1])
return xs, ys
@staticmethod
def save_foreground_files(foreground_xs, foreground_ys, basepath, prefix):
with open(os.path.join(basepath, f"{prefix}_ok_foreground_xs.npy"), 'wb') as file:
np.save(file, foreground_xs)
with open(os.path.join(basepath, f"{prefix}_ok_foreground_ys.npy"), 'wb') as file:
np.save(file, foreground_ys)
@staticmethod
def preprocess_walk(dataset, save_df=False):
parquet_filepath1, parquet_filepath2, parquet_filepath3 = dataset["filepaths"]
participant = dataset["participant"]
walk_uuid = dataset["walk-uuid"]
trial_nr = dataset["trial-nr"]
# load dataframes
df1 = pd.read_parquet(parquet_filepath1, engine='pyarrow')
df2 = pd.read_parquet(parquet_filepath2, engine='pyarrow')
df3 = pd.read_parquet(parquet_filepath3, engine='pyarrow')
df1 = df1.reset_index()
df2 = df2.reset_index()
df3 = df3.reset_index()
# Compute cartestion coordinates from polar coordinates
print("Computing Cartesian coordinates...")
df1['x'], df1['y'] = LidarPreprocessor.compute_xy_for_df(df1)
df2['x'], df2['y'] = LidarPreprocessor.compute_xy_for_df(df2)
df3['x'], df3['y'] = LidarPreprocessor.compute_xy_for_df(df3)
# Create images
print("Creating frame iamges...")
img_df1_path = LidarPreprocessor.create_frame_imgs_from_df(df1, save_path=os.path.join(participant, "200"))
img_df2_path = LidarPreprocessor.create_frame_imgs_from_df(df2, save_path=os.path.join(participant, "201"))
img_df3_path = LidarPreprocessor.create_frame_imgs_from_df(df3, save_path=os.path.join(participant, "203"))
# Compute background masks
print("Computing Background masks...")
imgs_df1 = LidarPreprocessor.load_lidar_scan_images(base_path=img_df1_path)
mask_aaa1, mask_bbb1, _ = LidarPreprocessor.compute_background_mask(df1, imgs_df1, min_density=0.41, max_density=0.45) # L3
imgs_df2 = LidarPreprocessor.load_lidar_scan_images(base_path=img_df2_path)
mask_aaa2, mask_bbb2, _ = LidarPreprocessor.compute_background_mask(df2, imgs_df2, min_density=0.41, max_density=0.45) # L3
imgs_df3 = LidarPreprocessor.load_lidar_scan_images(base_path=img_df3_path)
mask_aaa3, mask_bbb3, _ = LidarPreprocessor.compute_background_mask(df3, imgs_df3, min_density=0.41, max_density=0.45) # L3
# Extract foreground from lidar scans
print("Removing background from scans...")
df1['fgx'], df1['fgy'] = LidarPreprocessor.remove_background_from_frames_opt(df1, mask_aaa1, mask_bbb1)
df2['fgx'], df2['fgy'] = LidarPreprocessor.remove_background_from_frames_opt(df2, mask_aaa2, mask_bbb2)
df3['fgx'], df3['fgy'] = LidarPreprocessor.remove_background_from_frames_opt(df3, mask_aaa3, mask_bbb3)
out_parquet_filepath1 = os.path.join(participant, f"df_{participant}_{walk_uuid}_{trial_nr}_200.parquet")
out_parquet_filepath2 = os.path.join(participant, f"df_{participant}_{walk_uuid}_{trial_nr}_201.parquet")
out_parquet_filepath3 = os.path.join(participant, f"df_{participant}_{walk_uuid}_{trial_nr}_203.parquet")
# Persist parquet files
if save_df:
print("Saving updates parquet files...")
df1.to_parquet(out_parquet_filepath1)
df2.to_parquet(out_parquet_filepath2)
df3.to_parquet(out_parquet_filepath3)
return df1, df2, df3
class LidarClustering:
@staticmethod
def cluster_point_cloud(X, max_tolerated_distance_in_mm):
motion_groupings = []
cluster_centers = []
labels = fclusterdata(X, max_tolerated_distance_in_mm, criterion='distance', method="median")
cluster_count = 0
for k in set(labels):
L_k = X[np.where(labels==k)[0]]
xs_lk, ys_lk = list(zip(*L_k))
if len(L_k) < 500 and len(L_k) > 10:
cluster_count += 1
cluster_centers.append([np.mean(xs_lk), np.mean(ys_lk)])
motion_groupings.append([xs_lk, ys_lk])
return motion_groupings, cluster_centers, cluster_count
@staticmethod
def group_motions_in_frame(df_merged, frame_id, apply_filtering=True):
try:
aligned_fgxs1x, aligned_fgys1x, aligned_fgxs2x, aligned_fgys2x = LidarPreprocessor.align_dataframes_with_homographies(
df_merged, frame_id, LidarPreprocessor.HH1, LidarPreprocessor.HH2
)
fgx_df3, fgy_df3 = df_merged.iloc[frame_id]["fgx_df3"], df_merged.iloc[frame_id]["fgy_df3"]
fg_xs = np.concatenate((aligned_fgxs1x, aligned_fgxs2x, fgx_df3))
fg_ys = np.concatenate((aligned_fgys1x, aligned_fgys2x, fgy_df3))
X = np.array(list(zip(fg_xs, fg_ys)))
if apply_filtering:
X = np.array(list(filter(
lambda x: (x[1] > 1500) and (x[1] < 3300) and (x[0] < 7710) and (x[0] > 400), X
)))
if len(X) == 0:
return [], [], 0
#TODO: refactor
motion_groupings, cluster_centers, cluter_count = LidarClustering.cluster_point_cloud(
X, max_tolerated_distance_in_mm=200
)
if cluter_count == 1:
motion_groupings, cluster_centers, cluter_count = LidarClustering.cluster_point_cloud(
X, max_tolerated_distance_in_mm=175
)
if cluter_count == 1:
motion_groupings, cluster_centers, cluter_count = LidarClustering.cluster_point_cloud(
X, max_tolerated_distance_in_mm=150
)
return motion_groupings, cluster_centers, cluter_count
except ValueError as err:
print(err)
print(f"Error, no cluster for frame {frame_id}")
return [], [], 0
@staticmethod
def split_cluster_into_two(grouped_points):
# correction, if there is only one cc detected
xs_lk, ys_lk = grouped_points[0]
X = np.array(list(zip(*(xs_lk, ys_lk))))
k_means = KMeans(init='k-means++', n_clusters=2, n_init=1)
k_means.fit(X)
k_means_labels = k_means.labels_
k_means_cluster_centers = k_means.cluster_centers_
k_means_labels_unique = np.unique(k_means_labels)
new_grouped_points = []
new_cluster_centers = []
labels = list(set(k_means_labels))
x0 = X[np.where(k_means_labels==labels[0])]
x01, x02 = list(zip(*x0))
x1 = X[np.where(k_means_labels==labels[1])]
x11, x12 = list(zip(*x1))
new_grouped_points.append([x01, x02])
new_grouped_points.append([x11, x12])
new_cluster_centers.append([np.mean(x01), np.mean(x02)])
new_cluster_centers.append([np.mean(x11), np.mean(x12)])
return new_grouped_points, new_cluster_centers, 2
@staticmethod
def group_points(local_points, max_distance):
groups = []
points = local_points.copy()
while points:
far_points = []
ref = points.pop()
groups.append([ref])
for point in points:
d = LidarClustering.get_distance(ref, point)
if d < max_distance:
groups[-1].append(point)
else:
far_points.append(point)
points = far_points
# perform average operation on each group
return groups, [list(np.mean(x, axis=1).astype(int)) for x in groups]
@staticmethod
def get_distance(ref, point):
x1, y1 = ref
x2, y2 = point
return math.hypot(x2 - x1, y2 - y1)
@staticmethod
def merge_clusters(grouped_points, group_cluster_centers, frame_id):
max_leg_radius_in_mm = 150
new_grouped_points = []
new_cluster_centers = []
for current_max_distance in [100, 150, 200, 250, 300, 350, 400]:
grouped_clusters, _ = LidarClustering.group_points(group_cluster_centers, current_max_distance)
if len(grouped_clusters) < 3:
break
merged_groups_xs = np.concatenate([grouped_points[k][0] for k in range(len(grouped_points))])
merged_groups_ys = np.concatenate([grouped_points[k][1] for k in range(len(grouped_points))])
X = np.array(list(zip(*[merged_groups_xs, merged_groups_ys])))
if len(grouped_clusters) == 2:
#print("group1", grouped_clusters[0])
#print("group2", grouped_clusters[1])
#print("groups", grouped_clusters)
cluster_centers = [np.mean(np.array(clusters), axis=0) for clusters in grouped_clusters]
else:
#print(f"Using k-means fallback {frame_id}")
k_means = KMeans(init='k-means++', n_clusters=2, n_init=1)
k_means.fit(X)
k_means_labels = k_means.labels_
cluster_centers = k_means.cluster_centers_
k_means_labels_unique = np.unique(k_means_labels)
points_in_group1 = X[np.where(np.linalg.norm(X-cluster_centers[0], axis=1) < max_leg_radius_in_mm)]
points_in_group2 = X[np.where(np.linalg.norm(X-cluster_centers[1], axis=1) < max_leg_radius_in_mm)]
xs_g1, ys_g1 = list(zip(*points_in_group1))
xs_g2, ys_g2 = list(zip(*points_in_group2))
new_grouped_points.append([xs_g1, ys_g1])
new_grouped_points.append([xs_g2, ys_g2])
new_cluster_centers.append([np.mean(xs_g1), np.mean(ys_g1)])
new_cluster_centers.append([np.mean(xs_g2), np.mean(ys_g2)])
return new_grouped_points, new_cluster_centers, 2
@staticmethod
def group_points_in_frames(df_merged, verbose=True):
grouped_points_per_frame = []
group_cluster_centers_per_frame = []
more_than_two = []
for frame_id in range(len(df_merged)):
grouped_points, group_cluster_centers, cluster_count = LidarClustering.group_motions_in_frame(df_merged, frame_id=frame_id)
if cluster_count > 2:
#print(f"More than 2 clusters, fixing Frame {frame_id}")
grouped_points, group_cluster_centers, cluster_count = LidarClustering.merge_clusters(
grouped_points, group_cluster_centers, frame_id
)
elif frame_id > 0 and cluster_count == 1: # and (len(group_cluster_centers_per_frame[frame_id-1]) == 2):
#print(f"Splitting Frame {frame_id} into two clusters")
grouped_points, group_cluster_centers, cluster_count = LidarClustering.split_cluster_into_two(grouped_points)
more_than_two.append(cluster_count)
grouped_points_per_frame.append(grouped_points)
group_cluster_centers_per_frame.append(group_cluster_centers)
if verbose:
print(f"Clustered Frame {frame_id + 1} / {len(df_merged)} with a cluster count of {cluster_count}")
return grouped_points_per_frame, group_cluster_centers_per_frame, more_than_two
class LidarFeet:
@staticmethod
def assign_clusters_to_feet(group_cluster_centers_per_frame):
start_indices = []
end_indices = []
matches = []
left_leg = defaultdict(lambda: None)
rigth_leg = defaultdict(lambda: None)
walking_directions_left_leg = defaultdict(lambda: None)
walking_directions_right_leg = defaultdict(lambda: None)
for frame_id, (left_group, right_group) in enumerate(zip(group_cluster_centers_per_frame, group_cluster_centers_per_frame[1:])):
if len(left_group) == 0 and len(right_group) > 0:
start_indices.append(frame_id)
#print(f"start found at frame {frame_id}")
elif len(left_group) > 0 and len(right_group) == 0:
end_indices.append(frame_id)
#print(f"end found at frame {frame_id}")
elif len(left_group) == 2 and len(right_group) == 2:
distances = np.linalg.norm(np.array(right_group) - left_group[0], axis=1)
index_of_clostest_dist = np.where(distances == np.min(distances))[0][0]
match1 = [left_group[0], right_group[index_of_clostest_dist]]
match2 = [left_group[1], right_group[1 - index_of_clostest_dist]]
a = np.array(match1[1]) - np.array(match1[0])
an = a / np.linalg.norm(a)
b = np.array(match2[1]) - np.array(match2[0])
bn = b / np.linalg.norm(b)
walking_direction = an + bn
walking_direction = walking_direction / np.linalg.norm(walking_direction)
feet_y = [left_group[0][1], left_group[1][1]]
top_feet_y_index = np.where(feet_y == np.max(feet_y))[0][0]
#if frame_id == 3504 or frame_id == 3505 or frame_id == 3506:
# print(f"Frame {frame_id} {walking_direction} => {walking_direction[0] < 0}")
# TODO: instead of checking sign, compute vector between clusters (pointing to top foot. dotprot(topfoot_vec, dir) negative => right to left, otherwise left to right
if walking_direction[0] < 0:
# top y belongs to right foot
# bot y belongs to left foot
rigth_leg[frame_id] = left_group[top_feet_y_index]
left_leg[frame_id] = left_group[1 - top_feet_y_index]
else:
# top y belongs to left foot
# bot y belongs to right foot
rigth_leg[frame_id] = left_group[1 - top_feet_y_index]
left_leg[frame_id] = left_group[top_feet_y_index]
#print(walking_direction)
walking_directions_left_leg[frame_id] = a
walking_directions_right_leg[frame_id] = b
matches.append([match1, match2])
# TODO 1. compute best correspondences,
# TODO 2. compute direction vector,
# TODO 3. assign leg
else:
pass
return left_leg, rigth_leg, start_indices, end_indices, walking_directions_left_leg, walking_directions_right_leg
@staticmethod
def should_swap_feed_assignments(left_leg, rigth_leg, frame_id):
lf = np.array(left_leg[frame_id])
rf = np.array(rigth_leg[frame_id])
# in these experiments, rf is always above lf and therefore this sanity check holds true
# TODO: generalize this to utilize walking direction and compute dot pructs.
if rf[1] > lf[1]:
return False
lf_prev = np.array(left_leg[frame_id - 1])
rf_prev = np.array(rigth_leg[frame_id - 1])
# original distances
d1 = np.linalg.norm(lf - lf_prev)
d2 = np.linalg.norm(rf - rf_prev)
# swapped distances
d3 = np.linalg.norm(lf - rf_prev)
d4 = np.linalg.norm(rf - lf_prev)
return (d1 + d2) > (d3 + d4)
@staticmethod
def correct_feet_clusters(left_leg, rigth_leg, walking_directions_left_leg, walking_directions_right_leg, frame_count):
right_foot_valid = defaultdict(lambda: True)
left_foot_valid = defaultdict(lambda: True)
for frame_id in range(frame_count):
lf = np.array(left_leg[frame_id])
rf = np.array(rigth_leg[frame_id])
lf_prev = np.array(left_leg[frame_id - 1])
rf_prev = np.array(rigth_leg[frame_id - 1])
if lf.any() and rf.any() and lf_prev.any() and rf_prev.any():
if LidarFeet.should_swap_feed_assignments(left_leg, rigth_leg, frame_id):
tmp = left_leg[frame_id]
left_leg[frame_id] = rigth_leg[frame_id]
rigth_leg[frame_id] = tmp
else:
pass
#print(f"Missing data for frame {frame_id}")
#print("lf = ", lf)
#print("rf = ", rf)
return left_leg, rigth_leg
class DatasetProcessor:
TASKS = {
"00_free_walk_1": "bf8a163b-8f55-4885-b3ab-cf8d26f3904c",
"01_free_heel": "4b53b220-1c96-4af5-a3a5-0d377fd22b2a",
"06_fast_long": "65142a2d-b26d-45a0-906e-89d63fca32e5",
"07_normal_long": "f32593da-4168-4684-8653-567186458004",
"08_slow_long": "f32593da-4168-4684-8653-567186458004", # TODO: ask Aaron
"13_fga_01": "c6c44aa1-bcfc-4f8d-a451-e42dac3c6a2b" # TODO: ask Aaron
}
@staticmethod
def find_start_end_of_sequence(vec, min_interval_length=10):
# find sequences and lengths
# seqs = [(key, length), ...]
seqs = [(key, len(list(val))) for key, val in groupby(vec)]
# find start positions of sequences
# seqs = [(key, start, length), ...]
seqs = [(key, sum(s[1] for s in seqs[:i]), len) for i, (key, len) in enumerate(seqs)]
#print([[s[1], s[1] + s[2] - 1] for s in seqs if s[0] == 1])
#print([[s[1], s[1] + s[2] - 1] for s in seqs if s[0] == 1 and s[2] > 2])
start_ends = [[s[1], s[1] + s[2] - 1] for s in seqs if s[0] == 1 and s[2] > 2]
filtered_start_ends = list(filter(lambda interval: interval[1] - interval[0] > min_interval_length, start_ends))
return filtered_start_ends
@staticmethod
def draw_walk_interval_pictures(more_than_two, dataset):
walk_interval_image_filepath = DatasetProcessor.create_walking_interval_filepath(dataset)
participant = dataset["participant"]
task = dataset["task"]
trial_nr = dataset["trial_nr"]
title_text = f"{participant}_{task}_{trial_nr}"
import matplotlib
from matplotlib import pyplot as plt
from IPython.display import display, HTML
matplotlib.use('Agg')
fig = plt.figure(figsize=(8, 5))
plt.title(title_text)
start_endpoints = DatasetProcessor.find_start_end_of_sequence(np.array(more_than_two) > 0)
plt.plot(np.array(more_than_two) > 0, ".-")
for pse in start_endpoints:
plt.text(pse[0], 1.01, str(pse[0]), color="red", fontsize=5)
plt.text(pse[1], 1.01, str(pse[1]), color="red", fontsize=5)
plt.savefig(walk_interval_image_filepath)
plt.close(fig)
@staticmethod
def create_experiment_json(base_dir_name="gailo"):
experiments = []
def participant_number(item):
splits = item.split("_")
if len(splits) == 1:
return 0
return int(splits[1])
patient_directories = list(os.walk(base_dir_name))[0][1]
patient_directories = list(sorted(patient_directories, key=lambda item: participant_number(item)))
print(patient_directories)
for participant in patient_directories:
trial_tasks = set()
files_in_dir = list(os.walk(f"{base_dir_name}/{participant}"))[0][2]
for file_in_dir in files_in_dir:
_, _, _, task, trial_nr, *rest = file_in_dir.split("_")
trial_tasks.add(f"{task}_{trial_nr}")
for trial_task in list(trial_tasks):
task, trial_nr = trial_task.split("_")
experiments.append({
"base_path": base_dir_name,
"participant": participant,
"task": task,
"trial_nr": trial_nr,
"walk_range": []
})
with open(f"{base_dir_name}_experiments.json", 'w') as f:
json.dump(experiments, f, indent=2)
@staticmethod
def load_dataset(dataset):
base_path = dataset["base_path"]
participant = dataset["participant"]
task = dataset["task"]
trial_nr = dataset["trial_nr"]
df1 = pd.read_parquet(f"{base_path}/{participant}/df_{participant}_{task}_{trial_nr}_200.parquet", engine='pyarrow')
df2 = pd.read_parquet(f"{base_path}/{participant}/df_{participant}_{task}_{trial_nr}_201.parquet", engine='pyarrow')
df3 = pd.read_parquet(f"{base_path}/{participant}/df_{participant}_{task}_{trial_nr}_203.parquet", engine='pyarrow')
dfs = [df1, df2, df3]
return dfs
@staticmethod
def leg_dataset_filepath(dataset):
base_path = dataset["base_path"]
participant = dataset["participant"]
task = dataset["task"]
trial_nr = dataset["trial_nr"]
filename = f"df_{participant}_{task}_{trial_nr}_with_legs.parquet"
return os.path.join(base_path, participant, filename)
@staticmethod
def save_leg_df(df_merged, left_leg, rigth_leg, grouped_points, grouped_cluster_centers, more_than_two, output_filepath):
lf_x = [None for _ in range(len(df_merged))]
lf_y = [None for _ in range(len(df_merged))]
rf_x = [None for _ in range(len(df_merged))]
rf_y = [None for _ in range(len(df_merged))]
for idx, centroid in left_leg.items():
if centroid:
lf_x[idx] = centroid[0]
lf_y[idx] = centroid[1]
for idx, centroid in rigth_leg.items():
if centroid:
rf_x[idx] = centroid[0]
rf_y[idx] = centroid[1]
df_merged["lf_x"] = lf_x
df_merged["lf_y"] = lf_y
df_merged["rf_x"] = rf_x
df_merged["rf_y"] = rf_y
df_merged["grouped_points"] = grouped_points
df_merged["grouped_cluster_centers"] = grouped_cluster_centers
df_merged["cluster_count"] = more_than_two
df_merged.to_parquet(output_filepath)
@staticmethod
def create_leg_fileapth(dataset):
base_path = dataset["base_path"]
participant = dataset["participant"]
task = dataset["task"]
trial_nr = dataset["trial_nr"]
return f"{base_path}/{participant}/df_{participant}_{task}_{trial_nr}_with_legs.parquet"
@staticmethod
def create_walking_interval_filepath(dataset):
participant = dataset["participant"]
task = dataset["task"]
trial_nr = dataset["trial_nr"]
return f"debug/{participant}_{task}_{trial_nr}.jpeg"
@staticmethod
def select_datasets_by_task(datasets, task: str):
return list(filter(lambda dataset: dataset["task"] == task, datasets))
@staticmethod
def participants_with_all_for(datasets):
t01 = DatasetProcessor.select_datasets_by_task(datasets, TASKS["00_free_walk_1"])
t02 = DatasetProcessor.select_datasets_by_task(datasets, TASKS["01_free_heel"])
t03 = DatasetProcessor.select_datasets_by_task(datasets, TASKS["06_fast_long"])
t04 = DatasetProcessor.select_datasets_by_task(datasets, TASKS["07_normal_long"])
project_participants = lambda ds: set([d["participant"] for d in ds])
pt01 = project_participants(t01)
pt02 = project_participants(t02)
pt03 = project_participants(t03)
pt04 = project_participants(t04)
z01 = pt01.intersection(pt02)
z02 = z01.intersection(pt03)
return z02.intersection(pt04)
@staticmethod
def compute_clusters(datasets):
clustering_errors = []
grouping_data = {}
for dataset_idx, dataset in enumerate(datasets):
try:
print(f"Processing dataset {dataset} {dataset_idx + 1} / {len(datasets)} ...")
dfs = DatasetProcessor.load_dataset(dataset)
df_merged = LidarPreprocessor.merge_lidar_dataframes(dfs)
grouped_points_per_frame, group_cluster_centers_per_frame, more_than_two = LidarClustering.group_points_in_frames(df_merged)
grouping_data[dataset_idx] = [grouped_points_per_frame, group_cluster_centers_per_frame, more_than_two]
except Exception as err:
print(err)
clustering_errors.append(dataset)
print("Error for dataset: ", dataset)
return grouping_data, clustering_errors
@staticmethod
def compute_legs_from(datasets, grouping_data):
error_datasets = []
for dataset_idx, dataset in enumerate(datasets):
try:
print(f"Processing leg data for dataset {dataset} {dataset_idx + 1} / {len(datasets)} ...")
dfs = DatasetProcessor.load_dataset(dataset)
df_merged = LidarPreprocessor.merge_lidar_dataframes(dfs)
grouped_points_per_frame, group_cluster_centers_per_frame, more_than_two = grouping_data[dataset_idx]
left_leg, rigth_leg, start_indices, end_indices, walking_directions_left_leg, walking_directions_right_leg = LidarFeet.assign_clusters_to_feet(group_cluster_centers_per_frame)
left_leg, rigth_leg = LidarFeet.correct_feet_clusters(
left_leg, rigth_leg, walking_directions_left_leg, walking_directions_right_leg, len(group_cluster_centers_per_frame)
)
output_filepath = DatasetProcessor.create_leg_fileapth(dataset)
DatasetProcessor.save_leg_df(
df_merged,
left_leg,
rigth_leg,
grouped_points_per_frame,
group_cluster_centers_per_frame,
more_than_two,
output_filepath
)
DatasetProcessor.draw_walk_interval_pictures(more_than_two, dataset)
print(f"Saved leg data to {output_filepath}")
except Exception as err:
error_datasets.append(dataset)
print(err)
print("Error for dataset: ", dataset)
return error_datasets
@staticmethod
def preprocess_walks(datasets):
error_datasets = []
dfs_list = []
for dataset_idx, dataset in enumerate(datasets):
try: