-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcommon.py
More file actions
1510 lines (1182 loc) · 55.6 KB
/
common.py
File metadata and controls
1510 lines (1182 loc) · 55.6 KB
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 python
# -*- coding: utf-8 -*-
'''
###########################################################################
## OTHER SHARED UTILITIES ##
###########################################################################
Functions shared between modules, and other utilities
'''
## INIT
import toml
import json
import numpy as np
np.set_printoptions(legacy='1.21') # otherwise prints np.float64(3.0) rather than 3.0
import pandas as pd
from scipy import interpolate
from scipy.optimize import linear_sum_assignment
import re
import cv2
import c3d
import sys
import itertools as it
import logging
from anytree import PreOrderIter
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTabWidget, QVBoxLayout
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="c3d")
## AUTHORSHIP INFORMATION
__author__ = "David Pagnon"
__copyright__ = "Copyright 2021, Maya-Mocap"
__credits__ = ["David Pagnon"]
__license__ = "BSD 3-Clause License"
from importlib.metadata import version
__version__ = version('pose2sim')
__maintainer__ = "David Pagnon"
__email__ = "contact@david-pagnon.com"
__status__ = "Development"
## CONSTANTS
angle_dict = { # lowercase!
# joint angles
'right ankle': [['RKnee', 'RAnkle', 'RBigToe', 'RHeel'], 'dorsiflexion', 90, 1],
'left ankle': [['LKnee', 'LAnkle', 'LBigToe', 'LHeel'], 'dorsiflexion', 90, 1],
'right knee': [['RAnkle', 'RKnee', 'RHip'], 'flexion', -180, 1],
'left knee': [['LAnkle', 'LKnee', 'LHip'], 'flexion', -180, 1],
'right hip': [['RKnee', 'RHip', 'Hip', 'Neck'], 'flexion', 0, -1],
'left hip': [['LKnee', 'LHip', 'Hip', 'Neck'], 'flexion', 0, -1],
# 'lumbar': [['Neck', 'Hip', 'RHip', 'LHip'], 'flexion', -180, -1],
# 'neck': [['Head', 'Neck', 'RShoulder', 'LShoulder'], 'flexion', -180, -1],
'right shoulder': [['RElbow', 'RShoulder', 'Hip', 'Neck'], 'flexion', 0, -1],
'left shoulder': [['LElbow', 'LShoulder', 'Hip', 'Neck'], 'flexion', 0, -1],
'right elbow': [['RWrist', 'RElbow', 'RShoulder'], 'flexion', 180, -1],
'left elbow': [['LWrist', 'LElbow', 'LShoulder'], 'flexion', 180, -1],
'right wrist': [['RElbow', 'RWrist', 'RIndex'], 'flexion', -180, 1],
'left wrist': [['LElbow', 'LIndex', 'LWrist'], 'flexion', -180, 1],
# segment angles
'right foot': [['RBigToe', 'RHeel'], 'horizontal', 0, -1],
'left foot': [['LBigToe', 'LHeel'], 'horizontal', 0, -1],
'right shank': [['RAnkle', 'RKnee'], 'horizontal', 0, -1],
'left shank': [['LAnkle', 'LKnee'], 'horizontal', 0, -1],
'right thigh': [['RKnee', 'RHip'], 'horizontal', 0, -1],
'left thigh': [['LKnee', 'LHip'], 'horizontal', 0, -1],
'pelvis': [['LHip', 'RHip'], 'horizontal', 0, -1],
'trunk': [['Neck', 'Hip'], 'horizontal', 0, -1],
'shoulders': [['LShoulder', 'RShoulder'], 'horizontal', 0, -1],
'head': [['Head', 'Neck'], 'horizontal', 0, -1],
'right arm': [['RElbow', 'RShoulder'], 'horizontal', 0, -1],
'left arm': [['LElbow', 'LShoulder'], 'horizontal', 0, -1],
'right forearm': [['RWrist', 'RElbow'], 'horizontal', 0, -1],
'left forearm': [['LWrist', 'LElbow'], 'horizontal', 0, -1],
'right hand': [['RIndex', 'RWrist'], 'horizontal', 0, -1],
'left hand': [['LIndex', 'LWrist'], 'horizontal', 0, -1]
}
colors = [(255, 0, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (0, 0, 0), (255, 255, 255),
(125, 0, 0), (0, 125, 0), (0, 0, 125), (125, 125, 0), (125, 0, 125), (0, 125, 125),
(255, 125, 125), (125, 255, 125), (125, 125, 255), (255, 255, 125), (255, 125, 255), (125, 255, 255), (125, 125, 125),
(255, 0, 125), (255, 125, 0), (0, 125, 255), (0, 255, 125), (125, 0, 255), (125, 255, 0), (0, 255, 0)]
thickness = 2
## CLASSES
class plotWindow():
'''
Display several figures in tabs
Taken from https://github.com/superjax/plotWindow/blob/master/plotWindow.py
USAGE:
pw = plotWindow()
f = plt.figure()
plt.plot(x1, y1)
pw.addPlot("1", f)
f = plt.figure()
plt.plot(x2, y2)
pw.addPlot("2", f)
'''
def __init__(self, parent=None):
self.app = QApplication.instance()
if not self.app:
self.app = QApplication(sys.argv)
self.MainWindow = QMainWindow()
self.MainWindow.setWindowTitle("Multitabs figure")
self.canvases = []
self.figure_handles = []
self.toolbar_handles = []
self.tab_handles = []
self.current_window = -1
self.tabs = QTabWidget()
self.MainWindow.setCentralWidget(self.tabs)
self.MainWindow.resize(1280, 720)
self.MainWindow.show()
def addPlot(self, title, figure):
new_tab = QWidget()
layout = QVBoxLayout()
new_tab.setLayout(layout)
figure.subplots_adjust(left=0.1, right=0.99, bottom=0.1, top=0.91, wspace=0.2, hspace=0.2)
new_canvas = FigureCanvas(figure)
new_toolbar = NavigationToolbar(new_canvas, new_tab)
layout.addWidget(new_canvas)
layout.addWidget(new_toolbar)
self.tabs.addTab(new_tab, title)
self.toolbar_handles.append(new_toolbar)
self.canvases.append(new_canvas)
self.figure_handles.append(figure)
self.tab_handles.append(new_tab)
def show(self):
self.app.exec_()
## FUNCTIONS
def read_trc(trc_path):
'''
Read a TRC file and extract its contents.
INPUTS:
- trc_path (str): The path to the TRC file.
OUTPUTS:
- tuple: A tuple containing the Q coordinates, frames column, time column, marker names, and header.
'''
try:
with open(trc_path, 'r') as trc_file:
header = [next(trc_file) for _ in range(5)]
markers = header[3].split('\t')[2::3]
markers = [m.strip() for m in markers if m.strip()] # remove last \n character
trc_df = pd.read_csv(trc_path, sep="\t", skiprows=4, encoding='utf-8')
frames_col, time_col = trc_df.iloc[:, 0], trc_df.iloc[:, 1]
Q_coords = trc_df.drop(trc_df.columns[[0, 1]], axis=1)
Q_coords = Q_coords.loc[:, ~Q_coords.columns.str.startswith('Unnamed')] # remove unnamed columns
Q_coords.columns = np.array([[m,m,m] for m in markers]).ravel().tolist()
return Q_coords, frames_col, time_col, markers, header
except Exception as e:
raise ValueError(f"Error reading TRC file at {trc_path}: {e}")
def extract_trc_data(trc_path):
'''
Extract marker names and coordinates from a trc file.
INPUTS:
- trc_path: Path to the trc file
OUTPUTS:
- marker_names: List of marker names
- marker_coords: Array of marker coordinates (n_frames, t+3*n_markers)
'''
# marker names
with open(trc_path, 'r') as file:
lines = file.readlines()
marker_names_line = lines[3]
marker_names = marker_names_line.strip().split('\t')[2::3]
# time and marker coordinates
trc_data_np = np.genfromtxt(trc_path, skip_header=5, delimiter = '\t')[:,1:]
return marker_names, trc_data_np
def common_items_in_list(list1, list2):
'''
Do two lists have any items in common at the same index?
Returns True or False
'''
for i, j in enumerate(list1):
if j == list2[i]:
return True
return False
def bounding_boxes(js_file, margin_percent=0.1, around='extremities'):
'''
Compute the bounding boxes of the people in the json file.
Either around the extremities (with a margin)
or around the center of the person (with a margin).
INPUTS:
- js_file: json file
- margin_percent: margin around the person
- around: 'extremities' or 'center'
OUTPUT:
- bounding_boxes: list of bounding boxes [x_min, y_min, x_max, y_max]
'''
bounding_boxes = []
with open(js_file, 'r') as json_f:
js = json.load(json_f)
for people in range(len(js['people'])):
if len(js['people'][people]['pose_keypoints_2d']) < 3: continue
else:
x = js['people'][people]['pose_keypoints_2d'][0::3]
y = js['people'][people]['pose_keypoints_2d'][1::3]
x_min, x_max = min(x), max(x)
y_min, y_max = min(y), max(y)
if around == 'extremities':
dx = (x_max - x_min) * margin_percent
dy = (y_max - y_min) * margin_percent
bounding_boxes.append([x_min-dx, y_min-dy, x_max+dx, y_max+dy])
elif around == 'center':
x_mean, y_mean = np.mean(x), np.mean(y)
x_size = (x_max - x_min) * (1 + margin_percent)
y_size = (y_max - y_min) * (1 + margin_percent)
bounding_boxes.append([x_mean - x_size/2, y_mean - y_size/2, x_mean + x_size/2, y_mean + y_size/2])
return bounding_boxes
def retrieve_calib_params(calib_file, json_pose_dirs_names=None):
'''
Compute projection matrices from toml calibration file.
INPUT:
- calib_file: calibration .toml file.
OUTPUT:
- S: (h,w) vectors as list of 2x1 arrays
- K: intrinsic matrices as list of 3x3 arrays
- dist: distortion vectors as list of 4x1 arrays
- inv_K: inverse intrinsic matrices as list of 3x3 arrays
- optim_K: intrinsic matrices for undistorting points as list of 3x3 arrays
- R: rotation rodrigue vectors as list of 3x1 arrays
- T: translation vectors as list of 3x1 arrays
'''
calib = toml.load(calib_file)
cal_keys = [c for c in calib.keys()
if c not in ['metadata', 'capture_volume', 'charuco', 'checkerboard']
and isinstance(calib[c],dict)]
if json_pose_dirs_names is None:
pose_keys = cal_keys
else:
pose_keys = [name.replace('_json', '') for name in json_pose_dirs_names]
missing_cams = [cam for cam in pose_keys if cam not in cal_keys]
if missing_cams:
logging.error(f"The following cameras are present in the pose folders but missing in the calibration file: {missing_cams}.")
raise ValueError(f"The following cameras are present in the pose folders but missing in the calibration file: {missing_cams}.")
S, K, dist, optim_K, inv_K, R, R_mat, T = [], [], [], [], [], [], [], []
for cam in list(cal_keys):
if cam not in pose_keys:
continue
S.append(np.array(calib[cam]['size']))
K.append(np.array(calib[cam]['matrix']))
dist.append(np.array(calib[cam]['distortions']))
optim_K.append(cv2.getOptimalNewCameraMatrix(K[-1], dist[-1], [int(s) for s in S[-1]], 1, [int(s) for s in S[-1]])[0])
inv_K.append(np.linalg.inv(K[-1]))
R.append(np.array(calib[cam]['rotation']))
R_mat.append(cv2.Rodrigues(R[-1])[0])
T.append(np.array(calib[cam]['translation']))
calib_params = {'S': S, 'K': K, 'dist': dist, 'inv_K': inv_K, 'optim_K': optim_K, 'R': R, 'R_mat': R_mat, 'T': T}
return calib_params
def computeP(calib_file, json_pose_dirs_names=None, undistort=False):
'''
Compute projection matrices from toml calibration file.
INPUT:
- calib_file: calibration .toml file.
- undistort: boolean
OUTPUT:
- P: projection matrix as list of arrays
'''
calib = toml.load(calib_file)
cal_keys = [c for c in calib.keys()
if c not in ['metadata', 'capture_volume', 'charuco', 'checkerboard']
and isinstance(calib[c],dict)]
if json_pose_dirs_names is None:
pose_keys = cal_keys
else:
pose_keys = [name.replace('_json', '') for name in json_pose_dirs_names]
missing_cams = [cam for cam in pose_keys if cam not in cal_keys]
if missing_cams:
logging.error(f"The following cameras are present in the pose folders but missing in the calibration file: {missing_cams}.")
raise ValueError(f"The following cameras are present in the pose folders but missing in the calibration file: {missing_cams}.")
P = []
for cam in list(cal_keys):
if cam not in pose_keys:
continue
K = np.array(calib[cam]['matrix'])
if undistort:
S = np.array(calib[cam]['size'])
dist = np.array(calib[cam]['distortions'])
optim_K = cv2.getOptimalNewCameraMatrix(K, dist, [int(s) for s in S], 1, [int(s) for s in S])[0]
Kh = np.block([optim_K, np.zeros(3).reshape(3,1)])
else:
Kh = np.block([K, np.zeros(3).reshape(3,1)])
R, _ = cv2.Rodrigues(np.array(calib[cam]['rotation']))
T = np.array(calib[cam]['translation'])
H = np.block([[R,T.reshape(3,1)], [np.zeros(3), 1 ]])
P.append(Kh @ H)
return P
def weighted_triangulation(P_all,x_all,y_all,likelihood_all):
'''
Triangulation with direct linear transform,
weighted with likelihood of joint pose estimation.
INPUTS:
- P_all: list of arrays. Projection matrices of all cameras
- x_all,y_all: x, y 2D coordinates to triangulate
- likelihood_all: likelihood of joint pose estimation
OUTPUT:
- Q: array of triangulated point (x,y,z,1.)
'''
A = np.empty((0,4))
for c in range(len(x_all)):
P_cam = P_all[c]
A = np.vstack((A, (P_cam[0] - x_all[c]*P_cam[2]) * likelihood_all[c] ))
A = np.vstack((A, (P_cam[1] - y_all[c]*P_cam[2]) * likelihood_all[c] ))
if np.shape(A)[0] >= 4:
S, U, Vt = cv2.SVDecomp(A)
V = Vt.T
Q = np.array([V[0][3]/V[3][3], V[1][3]/V[3][3], V[2][3]/V[3][3], 1])
else:
Q = np.array([np.nan,np.nan,np.nan,1])
return Q
def reprojection(P_all, Q):
'''
Reprojects 3D point on all cameras.
INPUTS:
- P_all: list of arrays. Projection matrix for all cameras
- Q: array of triangulated point (x,y,z,1.)
OUTPUTS:
- x_calc, y_calc: list of coordinates of point reprojected on all cameras
'''
x_calc, y_calc = [], []
for c in range(len(P_all)):
P_cam = P_all[c]
x_calc.append(P_cam[0] @ Q / (P_cam[2] @ Q))
y_calc.append(P_cam[1] @ Q / (P_cam[2] @ Q))
return x_calc, y_calc
def euclidean_distance(q1, q2):
'''
Euclidean distance between 2 points (N-dim).
INPUTS:
- q1: list of N_dimensional coordinates of point
or list of N points of N_dimensional coordinates
- q2: idem
OUTPUTS:
- euc_dist: float. Euclidian distance between q1 and q2
'''
q1 = np.array(q1)
q2 = np.array(q2)
dist = q2 - q1
if np.isnan(dist).all():
dist = np.empty_like(dist)
dist[...] = np.inf
if len(dist.shape)==1:
euc_dist = np.sqrt(np.nansum( [d**2 for d in dist]))
else:
euc_dist = np.sqrt(np.nansum( [d**2 for d in dist], axis=1))
return euc_dist
def pad_shape(arr, target_len, fill_value=np.nan):
'''
Pads an array to the target length with specified fill values
INPUTS:
- arr: Input array to be padded.
- target_len: The target length of the first dimension after padding.
- fill_value: The value to use for padding (default: np.nan).
OUTPUTS:
- Padded array with shape (target_len, ...) matching the input dimensions.
'''
if len(arr) < target_len:
pad_shape = (target_len - len(arr),) + arr.shape[1:]
padding = np.full(pad_shape, fill_value)
return np.concatenate((arr, padding))
return arr
def trimmed_mean(arr, trimmed_extrema_percent=0.5):
'''
Trimmed mean calculation for an array.
INPUTS:
- arr (np.array): The input array.
- trimmed_extrema_percent (float): The percentage of values to be trimmed from both ends.
OUTPUTS:
- float: The trimmed mean of the array.
'''
# Sort the array
sorted_arr = np.sort(arr)
# Determine the indices for the 25th and 75th percentiles (if trimmed_percent = 0.5)
lower_idx = int(len(sorted_arr) * (trimmed_extrema_percent/2))
upper_idx = int(len(sorted_arr) * (1 - trimmed_extrema_percent/2))
# Slice the array to exclude the 25% lowest and highest values
trimmed_arr = sorted_arr[lower_idx:upper_idx]
# Return the mean of the remaining values
if len(trimmed_arr) == 0:
trimmed_mean_arr = np.mean(arr)
else:
trimmed_mean_arr = np.mean(trimmed_arr)
return trimmed_mean_arr
def world_to_camera_persp(r, t):
'''
Converts rotation R and translation T
from Qualisys world centered perspective
to OpenCV camera centered perspective,
and inversely.
Qc = RQ+T --> Q = R-1.Qc - R-1.T
INPUTS:
- r: rotation matrix (3x3)
- t: translation vector (3x1)
OUTPUTS:
- r: rotation matrix (3x3)
- t: translation vector (3x1)
'''
r = r.T
t = - r @ t
return r, t
def rotate_cam(r, t, ang_x=0, ang_y=0, ang_z=0):
'''
Apply rotations around x, y, z in cameras coordinates
Angle in radians
'''
r,t = np.array(r), np.array(t)
if r.shape == (3,3):
rt_h = np.block([[r,t.reshape(3,1)], [np.zeros(3), 1 ]])
elif r.shape == (3,):
rt_h = np.block([[cv2.Rodrigues(r)[0],t.reshape(3,1)], [np.zeros(3), 1 ]])
r_ax_x = np.array([1,0,0, 0,np.cos(ang_x),-np.sin(ang_x), 0,np.sin(ang_x),np.cos(ang_x)]).reshape(3,3)
r_ax_y = np.array([np.cos(ang_y),0,np.sin(ang_y), 0,1,0, -np.sin(ang_y),0,np.cos(ang_y)]).reshape(3,3)
r_ax_z = np.array([np.cos(ang_z),-np.sin(ang_z),0, np.sin(ang_z),np.cos(ang_z),0, 0,0,1]).reshape(3,3)
r_ax = r_ax_z @ r_ax_y @ r_ax_x
r_ax_h = np.block([[r_ax,np.zeros(3).reshape(3,1)], [np.zeros(3), 1]])
r_ax_h__rt_h = r_ax_h @ rt_h
r = r_ax_h__rt_h[:3,:3]
t = r_ax_h__rt_h[:3,3]
return r, t
def quat2rod(quat, scalar_idx=0):
'''
Converts quaternion to Rodrigues vector
INPUT:
- quat: quaternion. np.array of size 4
- scalar_idx: index of scalar part of quaternion. Default: 0, sometimes 3
OUTPUT:
- rod: Rodrigues vector. np.array of size 3
'''
if scalar_idx == 0:
w, qx, qy, qz = np.array(quat)
if scalar_idx == 3:
qx, qy, qz, w = np.array(quat)
else:
print('Error: scalar_idx should be 0 or 3')
rodx = qx * np.tan(w/2)
rody = qy * np.tan(w/2)
rodz = qz * np.tan(w/2)
rod = np.array([rodx, rody, rodz])
return rod
def quat2mat(quat, scalar_idx=0):
'''
Converts quaternion to rotation matrix
INPUT:
- quat: quaternion. np.array of size 4
- scalar_idx: index of scalar part of quaternion. Default: 0, sometimes 3
OUTPUT:
- mat: 3x3 rotation matrix
'''
if scalar_idx == 0:
w, qx, qy, qz = np.array(quat)
elif scalar_idx == 3:
qx, qy, qz, w = np.array(quat)
else:
print('Error: scalar_idx should be 0 or 3')
r11 = 1 - 2 * (qy**2 + qz**2)
r12 = 2 * (qx*qy - qz*w)
r13 = 2 * (qx*qz + qy*w)
r21 = 2 * (qx*qy + qz*w)
r22 = 1 - 2 * (qx**2 + qz**2)
r23 = 2 * (qy*qz - qx*w)
r31 = 2 * (qx*qz - qy*w)
r32 = 2 * (qy*qz + qx*w)
r33 = 1 - 2 * (qx**2 + qy**2)
mat = np.array([r11, r12, r13, r21, r22, r23, r31, r32, r33]).reshape(3,3).T
return mat
def sort_stringlist_by_last_number(string_list):
'''
Sort a list of strings based on the last number in the string.
Works if other numbers in the string, if strings after number. Ignores alphabetical order.
Example: ['json1', 'zero', 'js4on2.b', 'aaaa', 'eypoints_0000003.json', 'ajson0', 'json10']
gives: ['ajson0', 'json1', 'js4on2.b', 'eypoints_0000003.json', 'json10', 'aaaa', 'zero']
'''
def sort_by_last_number(s):
numbers = re.findall(r'\d+', s)
if numbers:
return (False, int(numbers[-1]))
else:
return (True, s)
return sorted(string_list, key=sort_by_last_number)
def natural_sort_key(s):
'''
Sorts list of strings with numbers in natural order (alphabetical and numerical)
Example: ['item_1', 'item_2', 'item_10', 'stuff_1']
'''
s=str(s)
return [int(c) if c.isdigit() else c.lower() for c in re.split(r'(\d+)', s)]
def zup2yup(Q):
'''
Turns Z-up system coordinates into Y-up coordinates
INPUT:
- Q: pandas dataframe
N 3D points as columns, ie 3*N columns in Z-up system coordinates
and frame number as rows
OUTPUT:
- Q: pandas dataframe with N 3D points in Y-up system coordinates
'''
# X->Y, Y->Z, Z->X
cols = list(Q.columns)
cols = np.array([[cols[i*3+1],cols[i*3+2],cols[i*3]] for i in range(int(len(cols)/3))]).flatten()
Q = Q[cols]
return Q
def create_c3d_file(c3d_path, marker_names, trc_data_np):
'''
Create a c3d file from the data extracted from a trc file.
INPUTS:
- c3d_path: Path to the c3d file
- marker_names: List of marker names
- trc_data_np: Array of marker coordinates (n_frames, t+3*n_markers)
OUTPUTS:
- c3d file
'''
# retrieve frame rate
times = trc_data_np[:,0]
frame_rate = round((len(times)-1) / (times[-1] - times[0]))
# write c3d file
writer = c3d.Writer(point_rate=frame_rate, analog_rate=0, point_scale=1.0, point_units='mm', gen_scale=-1.0)
writer.set_point_labels(marker_names)
writer.set_screen_axis(X='+Z', Y='+Y')
for frame in trc_data_np:
residuals = np.full((len(marker_names), 1), 0.0)
cameras = np.zeros((len(marker_names), 1))
coords = frame[1:].reshape(-1,3)*1000
points = np.hstack((coords, residuals, cameras))
writer.add_frames([(points, np.array([]))])
writer.set_start_frame(0)
writer._set_last_frame(len(trc_data_np)-1)
with open(c3d_path, 'wb') as handle:
writer.write(handle)
def convert_to_c3d(trc_path):
'''
Make Visual3D compatible c3d files from a trc path
INPUT:
- trc_path: string, trc file to convert
OUTPUT:
- c3d file
'''
c3d_path = trc_path.replace('.trc', '.c3d')
marker_names, trc_data_np = extract_trc_data(trc_path)
create_c3d_file(c3d_path, marker_names, trc_data_np)
return c3d_path
def interpolate_zeros_nans(col, *args):
'''
Interpolate missing points (of value zero),
unless more than N contiguous values are missing.
INPUTS:
- col: pandas column of coordinates
- args[0] = N: max number of contiguous bad values, above which they won't be interpolated
- args[1] = kind: 'linear', 'slinear', 'quadratic', 'cubic'. Default: 'cubic'
OUTPUT:
- col_interp: interpolated pandas column
'''
if len(args)==2:
N, kind = args
if len(args)==1:
N = np.inf
kind = args[0]
if not args:
N = np.inf
# Interpolate nans
mask = ~(np.isnan(col) | col.eq(0)) # true where nans or zeros
idx_good = mask.index[mask].tolist()
if len(idx_good) <= 4:
return col
if 'kind' not in locals(): # 'linear', 'slinear', 'quadratic', 'cubic'
f_interp = interpolate.interp1d(idx_good, col[idx_good], kind="linear", bounds_error=False)
else:
f_interp = interpolate.interp1d(idx_good, col[idx_good], kind=kind, fill_value='extrapolate', bounds_error=False)
col_interp = col.where(mask, f_interp(col.index)) #replace at false index with interpolated values
# Reintroduce nans if length of sequence > N
idx_notgood = mask.index[~mask].tolist()
gaps = np.where(np.diff(idx_notgood) > 1)[0] + 1 # where the indices of true are not contiguous
sequences = np.split(idx_notgood, gaps)
if sequences[0].size>0:
for seq in sequences:
if len(seq) > N: # values to exclude from interpolation are set to false when they are too long
col_interp.loc[seq] = np.nan
return col_interp
def points_to_angles(points_list):
'''
If len(points_list)==2, computes clockwise angle of ab vector w.r.t. horizontal (e.g. RBigToe, RHeel)
If len(points_list)==3, computes clockwise angle from a to c around b (e.g. Neck, Hip, Knee)
If len(points_list)==4, computes clockwise angle between vectors ab and cd (e.g. Neck Hip, RKnee RHip)
Points can be 2D or 3D.
If parameters are float, returns a float between 0.0 and 360.0
If parameters are arrays, returns an array of floats between 0.0 and 360.0
INPUTS:
- points_list: list of arrays of points
OUTPUTS:
- ang_deg: float or array of floats. The angle(s) in degrees.
'''
if len(points_list) < 2: # if not enough points, return None
return np.nan
points_array = np.array(points_list)
dimensions = points_array.shape[-1]
if len(points_list) == 2:
vector_u = points_array[0] - points_array[1]
if len(points_array.shape)==2:
vector_v = np.array([1, 0, 0]) # Here vector X, could be any horizontal vector
else:
vector_v = np.array([[1, 0, 0],] * points_array.shape[1])
elif len(points_list) == 3:
vector_u = points_array[0] - points_array[1]
vector_v = points_array[2] - points_array[1]
elif len(points_list) == 4:
vector_u = points_array[1] - points_array[0]
vector_v = points_array[3] - points_array[2]
else:
return np.nan
if dimensions == 2:
vector_u = vector_u[:2]
vector_v = vector_v[:2]
ang = np.arctan2(vector_u[1], vector_u[0]) - np.arctan2(vector_v[1], vector_v[0])
else:
cross_product = np.cross(vector_u, vector_v)
dot_product = np.einsum('ij,ij->i', vector_u, vector_v) # np.dot(vector_u, vector_v) # does not work with time series
ang = np.arctan2(np.linalg.norm(cross_product,axis=1), dot_product)
ang_deg = np.degrees(ang)
# ang_deg = np.array(np.degrees(np.unwrap(ang*2)/2))
return ang_deg
def fixed_angles(points_list, ang_name):
'''
Add offset and multiplying factor to angles
INPUTS:
- points_list: list of arrays of points
- ang_name: str. The name of the angle to consider.
OUTPUTS:
- ang: float. The angle in degrees.
'''
ang_params = angle_dict[ang_name]
ang = points_to_angles(points_list)
ang += ang_params[2]
ang *= ang_params[3]
if ang_name in ['pelvis', 'shoulders']:
ang = np.where(ang>90, ang-180, ang)
ang = np.where(ang<-90, ang+180, ang)
else:
ang = np.where(ang>180, ang-360, ang)
ang = np.where(ang<-180, ang+360, ang)
return ang
def mean_angles(Q_coords, ang_to_consider = ['right knee', 'left knee', 'right hip', 'left hip']):
'''
Compute the mean angle time series from 3D points for a given list of angles.
INPUTS:
- Q_coords (DataFrame): The triangulated coordinates of the markers.
- ang_to_consider (list): The list of angles to consider (requires angle_dict).
OUTPUTS:
- ang_mean: The mean angle time series.
'''
ang_to_consider = ['right knee', 'left knee', 'right hip', 'left hip']
angs = []
for ang_name in ang_to_consider:
ang_params = angle_dict[ang_name]
ang_mk = ang_params[0]
if 'Neck' not in Q_coords.columns:
df_MidShoulder = pd.DataFrame((Q_coords['RShoulder'].values + Q_coords['LShoulder'].values) /2)
df_MidShoulder.columns = ['Neck']*3
Q_coords = pd.concat((Q_coords.reset_index(drop=True), df_MidShoulder), axis=1)
pts_for_angles = []
for pt in ang_mk:
# pts_for_angles.append(Q_coords.iloc[:,markers.index(pt)*3:markers.index(pt)*3+3])
pts_for_angles.append(Q_coords[pt])
ang = fixed_angles(pts_for_angles, ang_name)
ang = np.abs(ang)
angs.append(ang)
ang_mean = np.mean(angs, axis=0)
return ang_mean
def add_neck_hip_coords(kpt_name, p_X, p_Y, p_scores, kpt_ids, kpt_names):
'''
Add neck (midshoulder) and hip (midhip) coordinates if neck and hip are not available
INPUTS:
- kpt_name: name of the keypoint to add (neck, hip)
- p_X: list of x coordinates after flipping if needed
- p_Y: list of y coordinates
- p_scores: list of confidence scores
- kpt_ids: list of keypoint ids (see skeletons.py)
- kpt_names: list of keypoint names (see skeletons.py)
OUTPUTS:
- p_X: list of x coordinates with added missing coordinate
- p_Y: list of y coordinates with added missing coordinate
- p_scores: list of confidence scores with added missing score
'''
names, ids = kpt_names.copy(), kpt_ids.copy()
names.append(kpt_name)
ids.append(len(p_X))
if kpt_name == 'Neck':
mid_X = (np.abs(p_X[ids[names.index('LShoulder')]]) + np.abs(p_X[ids[names.index('RShoulder')]])) /2
mid_Y = (p_Y[ids[names.index('LShoulder')]] + p_Y[ids[names.index('RShoulder')]])/2
mid_score = (p_scores[ids[names.index('LShoulder')]] + p_scores[ids[names.index('RShoulder')]])/2
elif kpt_name == 'Hip':
mid_X = (np.abs(p_X[ids[names.index('LHip')]]) + np.abs(p_X[ids[names.index('RHip')]]) ) /2
mid_Y = (p_Y[ids[names.index('LHip')]] + p_Y[ids[names.index('RHip')]])/2
mid_score = (p_scores[ids[names.index('LHip')]] + p_scores[ids[names.index('RHip')]])/2
else:
raise ValueError("kpt_name must be 'Neck' or 'Hip'")
p_X = np.append(p_X, mid_X)
p_Y = np.append(p_Y, mid_Y)
p_scores = np.append(p_scores, mid_score)
return p_X, p_Y, p_scores
def best_coords_for_measurements(Q_coords, keypoints_names, fastest_frames_to_remove_percent=0.2, close_to_zero_speed=0.2, large_hip_knee_angles=45):
'''
Compute the best coordinates for measurements, after removing:
- 20% fastest frames (may be outliers)
- frames when speed is close to zero (person is out of frame): 0.2 m/frame, or 50 px/frame
- frames when hip and knee angle below 45° (imprecise coordinates when person is crouching)
INPUTS:
- Q_coords: pd.DataFrame. The XYZ coordinates of each marker
- keypoints_names: list. The list of marker names
- fastest_frames_to_remove_percent: float
- close_to_zero_speed: float (sum for all keypoints: about 50 px/frame or 0.2 m/frame)
- large_hip_knee_angles: int
- trimmed_extrema_percent
OUTPUT:
- Q_coords_low_speeds_low_angles: pd.DataFrame. The best coordinates for measurements
'''
# Add MidShoulder column
df_MidShoulder = pd.DataFrame((Q_coords['RShoulder'].values + Q_coords['LShoulder'].values) /2)
df_MidShoulder.columns = ['MidShoulder']*3
Q_coords = pd.concat((Q_coords.reset_index(drop=True), df_MidShoulder), axis=1)
# Add Hip column if not present
n_markers_init = len(keypoints_names)
if 'Hip' not in keypoints_names:
df_Hip = pd.DataFrame((Q_coords['RHip'].values + Q_coords['LHip'].values) /2)
df_Hip.columns = ['Hip']*3
Q_coords = pd.concat((Q_coords.reset_index(drop=True), df_Hip), axis=1)
n_markers = len(keypoints_names)
# Using 80% slowest frames
sum_speeds = pd.Series(np.nansum([np.linalg.norm(Q_coords[kpt].diff(), axis=1) for kpt in keypoints_names], axis=0))
sum_speeds = sum_speeds[sum_speeds>close_to_zero_speed] # Removing when speeds close to zero (out of frame)
if len(sum_speeds)==0:
logging.warning('All frames have speed close to zero. Make sure the person is moving and correctly detected, or change close_to_zero_speed to a lower value. Not restricting the speeds to be above any threshold.')
Q_coords_low_speeds = Q_coords
else:
min_speed_indices = sum_speeds.abs().nsmallest(int(len(sum_speeds) * (1-fastest_frames_to_remove_percent))).index
Q_coords_low_speeds = Q_coords.iloc[min_speed_indices].reset_index(drop=True)
# Only keep frames with hip and knee flexion angles below 45%
# (if more than 50 of them, else take 50 smallest values)
try:
ang_mean = mean_angles(Q_coords_low_speeds, ang_to_consider = ['right knee', 'left knee', 'right hip', 'left hip'])
Q_coords_low_speeds_low_angles = Q_coords_low_speeds[ang_mean < large_hip_knee_angles]
if len(Q_coords_low_speeds_low_angles) < 50:
Q_coords_low_speeds_low_angles = Q_coords_low_speeds.iloc[pd.Series(ang_mean).nsmallest(50).index]
except:
Q_coords_low_speeds_low_angles = Q_coords_low_speeds
logging.warning(f"At least one among the RAnkle, RKnee, RHip, RShoulder, LAnkle, LKnee, LHip, LShoulder markers is missing for computing the knee and hip angles. Not restricting these angles to be below {large_hip_knee_angles}°.")
if Q_coords_low_speeds_low_angles.empty:
logging.warning('The selected person might not move, or is crouching for the whole sequence, or is not well detected. Taking all available data instead of filtering them.')
Q_coords_low_speeds_low_angles = Q_coords.copy()
if n_markers_init < n_markers:
Q_coords_low_speeds_low_angles = Q_coords_low_speeds_low_angles.iloc[:,:-3]
return Q_coords_low_speeds_low_angles
def compute_height(Q_coords, keypoints_names, fastest_frames_to_remove_percent=0.1, close_to_zero_speed=50, large_hip_knee_angles=45, trimmed_extrema_percent=0.5):
'''
Compute the height of the person from the trc data.
INPUTS:
- Q_coords: pd.DataFrame. The XYZ coordinates of each marker
- keypoints_names: list. The list of marker names
- fastest_frames_to_remove_percent: float. Frames with high speed are considered as outliers
- close_to_zero_speed: float. Sum for all keypoints: about 50 px/frame or 0.2 m/frame
- large_hip_knee_angles5: float. Hip and knee angles below this value are considered as imprecise
- trimmed_extrema_percent: float. Proportion of the most extreme segment values to remove before calculating their mean)
OUTPUT:
- height: float. The estimated height of the person
'''
# Retrieve most reliable coordinates, adding MidShoulder and Hip columns if not present
Q_coords_low_speeds_low_angles = best_coords_for_measurements(Q_coords, keypoints_names,
fastest_frames_to_remove_percent=fastest_frames_to_remove_percent, close_to_zero_speed=close_to_zero_speed, large_hip_knee_angles=large_hip_knee_angles)
# Automatically compute the height of the person
feet_pairs = [['RHeel', 'RAnkle'], ['LHeel', 'LAnkle']]
try:
rfoot, lfoot = [euclidean_distance(Q_coords_low_speeds_low_angles[pair[0]],Q_coords_low_speeds_low_angles[pair[1]]) for pair in feet_pairs]
except:
rfoot, lfoot = 0.10, 0.10
logging.warning('The Heel marker is missing from your model. Considering Foot to Heel size as 10 cm.')
ankle_to_shoulder_pairs = [['RAnkle', 'RKnee'], ['RKnee', 'RHip'], ['RHip', 'RShoulder'],
['LAnkle', 'LKnee'], ['LKnee', 'LHip'], ['LHip', 'LShoulder']]
try:
rshank, rfemur, rback, lshank, lfemur, lback = [euclidean_distance(Q_coords_low_speeds_low_angles[pair[0]],Q_coords_low_speeds_low_angles[pair[1]]) for pair in ankle_to_shoulder_pairs]
except:
logging.error('At least one of the following markers is missing for computing the height of the person:\
RAnkle, RKnee, RHip, RShoulder, LAnkle, LKnee, LHip, LShoulder.\n\
Make sure that the person is entirely visible, or use a calibration file instead, or set "to_meters=false".')
raise ValueError('At least one of the following markers is missing for computing the height of the person:\
RAnkle, RKnee, RHip, RShoulder, LAnkle, LKnee, LHip, LShoulder.\
Make sure that the person is entirely visible, or use a calibration file instead, or set "to_meters=false".')
try: