Posetools¶
Pose trajectories from video, in one contract across three detector families.
The extractor family¶
Three extractors return the same tidy result --- per-frame timestamps, a landmarks array with confidence in the last channel, detection flags, and an optional CSV --- so an analysis written against one runs against the others, and two detectors can be compared on a shared clock:
| function | family | topology | needs |
|---|---|---|---|
extract_pose_landmarks |
MediaPipe | 33 landmarks | [pose], runs on CPU |
extract_pose_landmarks_yolo |
YOLO11-pose | 17 COCO keypoints | [yolo] (Ultralytics, AGPL) |
extract_pose_landmarks_rtmpose |
RTMPose via rtmlib | 17 COCO keypoints | [rtmpose] (ONNX runtime, Apache) |
Which to reach for, measured on a dark dance stage rather than read from
leaderboards: localisation has converged across every serious model, so the axis
that separates them is detection rate. MediaPipe holds ~99% at ~45 fps on CPU for
a single person and remains the answer without a GPU; yolo11m is the GPU knee
(99.7% at ~114 fps); RTMPose matches it at 100% with a separate person detector
that never loses the person, and is the Apache-licensed family.
More than one body¶
With two bodies in frame, choosing the highest-confidence detection per frame teleports the trajectory whenever the choice flips --- between two dancers, or between a dancer and their life-size projection on a videoconference screen, which is a person to any detector. Two tools answer this:
extract_pose_tracks_yoloreturns every identity's trajectory separately as track fragments (track=Trueon the extractor follows the most persistent identity instead). A fragment is trustworthy within itself; over a long recording a body is many fragments.associate_fragmentschains fragments into persistent movers using position and time only, refusing where honesty demands: a crossing at a fragment boundary becomes a recorded break for a human to adjudicate, never a guess.
Drawing what was tracked¶
skeleton_timeline draws posture at sampled moments on a real time axis ---
stick figures, torso-normalised so the strip reads body shape rather than place
in the room, with honest gaps where tracking dropped. The derived-signal helpers
below (midpoint, filtering, and the rest) turn landmark arrays into the
trajectories the Effort layer and other analyses consume.
API reference¶
Landmark-trajectory pose tools.
This module implements the array-level pose workflow used in several of the fourMs sound--motion studies: video file -> tidy per-landmark trajectory arrays (and optionally CSV) -> derived motion signals (limb speed, impact events).
It complements — and does not replace — the rendering-oriented
MgVideo.pose() pipeline in :mod:musicalgestures._pose (overlaid skeleton
video, average-pose image, trajectory image, keypoint CSV) and the per-frame
:class:musicalgestures._pose_estimator.PoseEstimator interface. Use this
module when you want plain numpy trajectories for downstream signal analysis
(quantity of motion, cross-modal alignment, event detection) rather than
rendered output.
Only :func:extract_pose_landmarks needs MediaPipe (an optional dependency,
imported lazily). The derived-signal helpers (:func:midpoint,
:func:limb_speed_from_landmarks, :func:impact_events) are numpy-only and
also work on landmark/point trajectories from any other source (OpenPose,
YOLO-pose, motion capture).
Landmark indices follow the 33-landmark MediaPipe Pose (BlazePose GHUM)
topology used by the mediapipe 0.10.x wheels (e.g. 0 = nose, 11/12 =
left/right shoulder, 13/14 = elbows, 15/16 = wrists); see
:data:musicalgestures._pose_estimator.MEDIAPIPE_LANDMARK_NAMES for the full
index -> name mapping.
extract_pose_landmarks ¶
extract_pose_landmarks(filename, fps=None, width=None, t0=0.0, duration=None, model_complexity=1, world_landmarks=False, min_detection_confidence=0.5, min_tracking_confidence=0.5, max_frames=None, target_name=None, quiet=True, verbose=True)
Run MediaPipe Pose over a whole video and return tidy per-landmark trajectories.
The video is decoded through an FFmpeg raw-video pipe (optionally resampled
to a lower frame rate and resized), each frame is passed to MediaPipe Pose,
and the 33 landmarks are collected into plain numpy arrays: pixel
coordinates in the analysis frame plus the per-landmark visibility
score, with all-NaN rows on frames where no pose was detected, and a
detection-rate summary. This is the consolidated version of several
near-identical study extractors; downstream method choices (filtering, QoM,
alignment) are deliberately not baked in here.
MediaPipe is an optional dependency (pip install musicalgestures[pose])
and is imported lazily, so importing this module works without it. Both
mediapipe API families are supported: the legacy Solutions API
(mp.solutions.pose.Pose, wheels up to ~0.10.14) and the Tasks API
(PoseLandmarker in VIDEO running mode, newer 0.10.x wheels where the
Solutions API was removed). With the Tasks API the model file is
auto-downloaded and cached in musicalgestures/models/ on first use
(shared with MgVideo.pose()).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the input video file. |
required |
fps
|
float
|
Analysis frame rate. Frames are resampled to this rate by FFmpeg before pose estimation (e.g. 12.5 to halve a 25 fps video). Defaults to None (native frame rate). |
None
|
width
|
int
|
Resize the analysis frames to this width in pixels, keeping the aspect ratio. Smaller frames are much faster and are usually sufficient for trajectory-level analysis (the studies used 256-640 px). Defaults to None (native resolution). |
None
|
t0
|
float
|
Start time of the analysis window in seconds.
The window is cut by FFmpeg (input-side seek), so the rest of the
file is never decoded. Returned timestamps stay on the source
clock, i.e. |
0.0
|
duration
|
float
|
Length of the analysis window in seconds
(from |
None
|
model_complexity
|
int
|
MediaPipe model variant: 0 (lite), 1 (full) or 2 (heavy). Defaults to 1. |
1
|
world_landmarks
|
bool
|
Whether to also collect MediaPipe's 3D world landmarks (metres, hip-centred). Defaults to False. |
False
|
min_detection_confidence
|
float
|
MediaPipe person-detection confidence threshold (also used as the presence threshold with the Tasks API). Defaults to 0.5. |
0.5
|
min_tracking_confidence
|
float
|
MediaPipe landmark-tracking confidence threshold. Defaults to 0.5. |
0.5
|
max_frames
|
int
|
Stop after this many analysed frames (handy for quick tests). Defaults to None (whole video). |
None
|
target_name
|
str
|
If given, also write the trajectories to
this path as a tidy CSV with columns |
None
|
quiet
|
bool
|
Suppress MediaPipe's native C++/GL console logs during inference. Defaults to True. |
True
|
verbose
|
bool
|
Print a one-line detection-rate summary per video. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with keys:
|
Source
Consolidated from the author's study extractors: stillstanding (mp_extract_westney.py, pose_motion.py) and Westney-comparisons (concert_mediapipe.py, reh_pose.py, a1_labstage.py) (Jensenius).
Source code in musicalgestures/_posetools.py
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 | |
extract_pose_tracks_yolo ¶
extract_pose_tracks_yolo(filename, fps=None, width=None, t0=0.0, duration=None, model='yolo11s-pose.pt', conf=0.25, max_frames=None, tracker='bytetrack.yaml', verbose=True)
Every person's trajectory separately, with identities held across frames.
The single-person extractors follow the highest-confidence detection per frame, and with two bodies in frame that selection flips between them --- measured on a dance corpus, where it teleported the trajectory between two real dancers, and between a dancer and their life-size projected partner on a screen. This runs the same YOLO pose models through Ultralytics' tracker, so each body keeps an identity, and returns one trajectory per identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the input video file. |
required |
fps
|
float
|
Analysis frame rate. Defaults to None (native). |
None
|
width
|
int
|
Analysis width in pixels. Defaults to None. |
None
|
t0
|
float
|
Start of the analysis window in seconds. |
0.0
|
duration
|
float
|
Length of the window in seconds. |
None
|
model
|
str
|
An Ultralytics pose model. Defaults to "yolo11s-pose.pt". |
'yolo11s-pose.pt'
|
conf
|
float
|
Detection confidence threshold. Defaults to 0.25. |
0.25
|
max_frames
|
int
|
Stop after this many analysed frames. |
None
|
tracker
|
str
|
Ultralytics tracker configuration. Defaults to "bytetrack.yaml". |
'bytetrack.yaml'
|
verbose
|
bool
|
Print a one-line summary. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
dict
|
|
|
dict
|
zero-confidence keypoints as NaN), plus |
|
dict
|
|
Source code in musicalgestures/_posetools.py
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 | |
extract_pose_landmarks_yolo ¶
extract_pose_landmarks_yolo(filename, fps=None, width=None, t0=0.0, duration=None, model='yolo11n-pose.pt', conf=0.25, max_frames=None, target_name=None, track=False, tracker='bytetrack.yaml', verbose=True)
Run a YOLO pose model over a whole video: the Ultralytics twin of
:func:extract_pose_landmarks, on the same trajectory-array contract.
Same decode pipe, same result dictionary, so the two detectors can be
compared on a shared clock with the anchor-and-match tooling --- the point
of having a twin. The differences are the topology and the third channel:
YOLO emits the 17-point COCO set (COCO_KEYPOINT_NAMES), and the third
value per keypoint is the model's keypoint confidence rather than
MediaPipe's visibility. A keypoint the model marks with zero confidence has
no measured position (the raw output pins it to the image origin), so its
coordinates are returned as NaN rather than as a fabricated point. When
several people are in frame, the highest-confidence detection is followed;
multi-person trajectories are out of scope for the twin contract.
Ultralytics is an optional dependency (pip install musicalgestures[yolo])
and is imported lazily. The model file is downloaded on first use into
musicalgestures/models/ and reused after that.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the input video file. |
required |
fps
|
float
|
Analysis frame rate, resampled by FFmpeg. Defaults to None (native frame rate). |
None
|
width
|
int
|
Resize the analysis frames to this width, keeping the aspect ratio. Defaults to None (native resolution). |
None
|
t0
|
float
|
Start of the analysis window in seconds (input-side seek; returned timestamps stay on the source clock). Defaults to 0.0. |
0.0
|
duration
|
float
|
Length of the analysis window in seconds. Defaults to None (until the end of the file). |
None
|
model
|
str
|
An Ultralytics pose model: a bare released name
(downloaded and cached in |
'yolo11n-pose.pt'
|
conf
|
float
|
Detection confidence threshold. Defaults to 0.25, the Ultralytics default. |
0.25
|
max_frames
|
int
|
Stop after this many analysed frames. Defaults to None (whole video). |
None
|
target_name
|
str
|
If given, also write the trajectories as a
tidy CSV with columns |
None
|
track
|
bool
|
Follow one stable identity through Ultralytics'
tracker instead of the highest-confidence detection per frame. With
two bodies in frame the per-frame selection flips between them ---
two dancers, or a dancer and their projection on a screen --- and
tracking is the cure: the identity present in the most frames (ties
to higher confidence) is followed throughout. For every identity
separately, use :func: |
False
|
tracker
|
str
|
Ultralytics tracker configuration, used when
|
'bytetrack.yaml'
|
verbose
|
bool
|
Print a one-line detection-rate summary. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
As :func: |
dict
|
(F, 17, 3), |
|
dict
|
None (YOLO pose has no world-coordinate output). |
Source code in musicalgestures/_posetools.py
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 | |
fragment_embeddings ¶
fragment_embeddings(filename, tracks_data, bins=12, verbose=True)
One appearance vector per track fragment, from a single sequential pass.
The v2 half of fragment re-association
(plans/2026-08-30-reid-v2-design.md): appearance is what survives an
occlusion, and this collects it the way this project's drives prefer --- one
sequential decode rather than thousands of seeks. For every stored detection
row, the torso region (the box the shoulder and hip keypoints span, padded)
is cut from the frame and summarised as a hue--saturation histogram; a
fragment's embedding is the median over its rows, so a few bad crops do not
speak for the fragment.
A colour histogram is deliberately the first tool: the problem is closed over one session --- same people, same clothes, one camera --- and the within-fragment consistency check in the test suite is the gate for whether it suffices before anything heavier is considered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
The video the fragments were tracked in. |
required | |
tracks_data
|
dict
|
As returned by :func: |
required |
bins
|
int
|
Histogram bins per channel. Defaults to 12. |
12
|
verbose
|
bool
|
Print a one-line summary. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Fragment id to a normalised embedding vector. |
Source code in musicalgestures/_posetools.py
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 | |
associate_fragments ¶
associate_fragments(tracks_data, n_movers=2, max_gap_s=2.0, max_speed=None, embeddings=None, appearance_max_gap_s=120.0, min_separation=None)
Chain track fragments into persistent movers, refusing where honesty demands.
Identity tracking over a long session yields fragments --- trustworthy within
themselves, unlinked between themselves. This chains them into n_movers
persistent movers using position and time only, under three rules from the
design (plans/2026-08-30-fragment-reassociation-design.md):
- Exclusivity: fragments overlapping in time are different movers.
- Plausibility: a fragment continues a mover only when the time gap is at
most
max_gap_sand the bridging speed of the shoulder midpoint is belowmax_speed--- whose default is measured from the material itself (three times the 95th percentile of within-fragment speeds), never guessed. - Refusal: when more than one mover could accept a fragment, or none can, that moment becomes a recorded break, never a guess. Chains restart after a break, and nothing claims continuity across one. Position alone cannot disambiguate two movers who cross exactly at a fragment boundary; the break list is where an analyst resolves those moments by watching the video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracks_data
|
dict
|
As returned by :func: |
required |
n_movers
|
int
|
Persistent movers to chain. Defaults to 2. |
2
|
max_gap_s
|
float
|
Longest silence a chain may bridge. Defaults to 2. |
2.0
|
max_speed
|
float
|
Fastest plausible bridge, in the landmarks' units per second. Defaults to None: measured from the fragments. |
None
|
embeddings
|
dict
|
Appearance vector per fragment id, as from
:func: |
None
|
appearance_max_gap_s
|
float
|
Longest silence an appearance link may bridge. Defaults to 120. |
120.0
|
min_separation
|
float
|
How much closer the best appearance match must be than the second best. Defaults to None: measured as the 95th percentile of within-fragment embedding spread, so the bar comes from the material's own appearance stability. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
dict
|
|
|
dict
|
concatenated |
|
dict
|
|
|
dict
|
candidate movers; and |
|
dict
|
With |
|
dict
|
every break, so appearance links each segment's movers into persistent |
|
dict
|
chains by the same strictly-more-separated rule --- each segment mover |
|
dict
|
gains a |
|
dict
|
|
|
dict
|
mover whose margin does not clear the bar starts a new chain; no |
|
dict
|
cross-break identity is ever guessed. |
Source code in musicalgestures/_posetools.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 | |
extract_pose_landmarks_rtmpose ¶
extract_pose_landmarks_rtmpose(filename, fps=None, width=None, t0=0.0, duration=None, mode='balanced', max_frames=None, target_name=None, device=None, verbose=True)
RTMPose over a whole video: the Apache-licensed twin, same contract.
The third member of the extractor family, riding rtmlib (RTMPose through
ONNX runtime --- no MMPose stack) and emitting the same 17-point COCO
topology as the YOLO twin, so all three extractors feed the same
detector-agreement tooling. Benchmarked on a dark dance stage, RTMPose's
separate person detector held 100 per cent detection where small single-stage
models flickered; it is also the family under an Apache licence.
rtmlib is an optional dependency (pip install musicalgestures[rtmpose])
and is imported lazily. Model files download to rtmlib's own cache on first
use. When several people are in frame, the highest-scoring detection is
followed, exactly as the YOLO twin does; identity tracking stays the YOLO
path's feature for now.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the input video file. |
required |
fps
|
float
|
Analysis frame rate, resampled by FFmpeg. |
None
|
width
|
int
|
Analysis width in pixels, aspect preserved. |
None
|
t0
|
float
|
Start of the analysis window in seconds. |
0.0
|
duration
|
float
|
Length of the window in seconds. |
None
|
mode
|
str
|
rtmlib's size: "lightweight", "balanced" or "performance". Defaults to "balanced". |
'balanced'
|
max_frames
|
int
|
Stop after this many analysed frames. |
None
|
target_name
|
str
|
Also write a tidy CSV, as the twins do. |
None
|
device
|
str
|
"cuda" or "cpu". Defaults to None: cuda when
onnxruntime reports a CUDA execution provider, else cpu --- and the
choice is recorded in the result's |
None
|
verbose
|
bool
|
Print a one-line summary. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
As :func: |
Source code in musicalgestures/_posetools.py
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 | |
skeleton_timeline ¶
skeleton_timeline(landmarks, times, ax=None, n_figures=24, min_conf=0.3, height=1.0, color='#7a3b8f', lw=1.2)
A timeline of stick figures: posture at sampled moments, drawn on time.
The keyframe display's skeletal descendant: n_figures moments spread evenly
over the material, each drawn as a stick figure at its place on the time axis,
so a raised arm or a deep bend is visible AS posture where a motiongram shows
only that something moved. Each figure is normalised by its own torso length
and centred in its slot, so the timeline reads posture and not position ---
where the body was in the room is the spatial maps' job.
A moment with no usable detection --- fewer than half its keypoints above
min_conf at the nearest detected frame --- is skipped rather than guessed,
so gaps in the timeline are honest gaps in the tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
landmarks
|
(frames, 17, 3) trajectories in the COCO topology, confidence in the third channel, as the YOLO extractors return. |
required | |
times
|
(frames,) timestamps in seconds. |
required | |
ax
|
A matplotlib axes to draw on. Created when None. |
None
|
|
n_figures
|
int
|
Moments to draw. Defaults to 24. |
24
|
min_conf
|
float
|
Keypoint confidence below which a point does not exist. Defaults to 0.3. |
0.3
|
height
|
float
|
Figure height in axis y-units. Defaults to 1. |
1.0
|
color
|
Line colour. |
'#7a3b8f'
|
|
lw
|
float
|
Line width. |
1.2
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of figures actually drawn. |
Source code in musicalgestures/_posetools.py
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 | |
midpoint ¶
midpoint(a, b)
Element-wise midpoint of two landmark trajectories.
Typical use is the shoulder midpoint (MediaPipe landmarks 11 and 12) as an
upper-torso proxy point, e.g. midpoint(lm[:, 11, :2], lm[:, 12, :2]).
NaNs (detection dropouts) propagate: the midpoint is NaN wherever either
input is NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
First trajectory, any shape (e.g. (F, 2)). |
required |
b
|
ndarray
|
Second trajectory, broadcast-compatible with |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: |
Source
Stillstanding study, pose_motion.py (shoulder-midpoint torso micromotion) (Jensenius).
Source code in musicalgestures/_posetools.py
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 | |
limb_speed_from_landmarks ¶
limb_speed_from_landmarks(xy, confidence, fps, conf_gate=0.5, merge='max_lr', smooth_taps=3)
Confidence-gated image-plane speed of one or more candidate limbs.
For each candidate limb (e.g. the left and right wrist), frames whose
landmark confidence/visibility falls below conf_gate are masked out
(NaN), and the limb speed is formed as the central-difference magnitude of
the pixel path (px/s). Candidate limbs are then merged by element-wise
maximum — so that motion of either limb registers, mirroring the
bilateral merge used for inertial hand data — and lightly smoothed with a
short NaN-aware moving average. Peaks of the resulting signal mark, e.g.,
strike downstrokes of the striking wrist.
Caveats (from the cymbal study): these are 2D apparent kinematics from a single camera — motion toward/away from the lens is foreshortened and pixel speed is not metric speed. Moreover, a limb-speed peak marks maximum downstroke speed, which systematically precedes the contact/arrest that an audio onset or an acceleration peak registers; account for this bias when comparing event times across modalities.
Peak-picking on the returned signal is left to the caller (a general
adaptive peak-picker, pick_peaks, is provided by the sibling
core-signal-methods PR in musicalgestures._peaks; the cymbal study used
a relative threshold of 0.4 x the take's peak with a 0.2 s minimum
interval).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xy
|
ndarray
|
Pixel positions, shape (F, L, 2) for L candidate limbs or (F, 2) for a single limb. |
required |
confidence
|
ndarray
|
Per-frame landmark confidence
(MediaPipe |
required |
fps
|
float
|
Frame rate of the trajectory (Hz). |
required |
conf_gate
|
float
|
Frames with confidence below this value are masked (NaN) before differentiation. Defaults to 0.5. |
0.5
|
merge
|
str
|
|
'max_lr'
|
smooth_taps
|
int
|
Length of the NaN-aware moving-average
smoother applied after merging. Use 0 or 1 to disable. Defaults
to 3. With |
3
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Speed in px/s: shape (F,) when merged, else (F, L). NaN where the position (or a central-difference neighbour) is masked or missing. |
Source
Cymbal-comparison study, markerless striking-wrist speed (reimplemented from the paper's method description; defaults are the paper's provisional values) (Jensenius).
Source code in musicalgestures/_posetools.py
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 | |
impact_events ¶
impact_events(pos_by_point, fps, rel_thresh=0.12, min_interval_s=0.1)
Detect candidate impact events from point trajectories via acceleration peaks.
Each candidate point's position (2D or 3D; e.g. the two hand points of a
mocap model, or the two wrist landmarks) is differentiated twice with
central differences to obtain its acceleration vector, the vector magnitude
is taken, and the points are merged by element-wise maximum so that a
strike by either hand registers (bilateral max). Impacts are then
peak-picked on the merged acceleration magnitude with a relative threshold
of rel_thresh x the signal's maximum and a minimum inter-impact
interval of min_interval_s.
The threshold parameters are taken directly (the small relative-threshold
peak picker is implemented inline here); a general adaptive peak-picker,
pick_peaks, is provided by the sibling core-signal-methods PR in
musicalgestures._peaks. The defaults (0.12 x peak, 100 ms) are
validated against the original cymbal dataset (Zenodo 21360429, 2026
revalidation) for 120 Hz mocap hand data and should be tuned per dataset.
Note the study's caveat: double-differentiating (model-reconstructed)
positions is noisy and also responds to the backswing, not only the
collision — treat the detected peaks as candidate impacts and validate
against another modality (e.g. audio onsets) where possible. For
whole-image visual impact detection from video (no landmarks), see
MgVideo.impacts() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_by_point
|
ndarray
|
Point positions, shape (F, P, D) for P candidate points in D spatial dimensions (2 or 3), or (F, D) for a single point. Units are the caller's (m or px); NaNs (dropouts) propagate into the acceleration and are never picked as peaks. |
required |
fps
|
float
|
Sampling rate of the trajectories (Hz). |
required |
rel_thresh
|
float
|
Relative peak threshold as a fraction of the merged acceleration magnitude's maximum. Defaults to 0.12. |
0.12
|
min_interval_s
|
float
|
Minimum interval between detected impacts in seconds (stronger peaks win). Defaults to 0.10. |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with keys:
|
Source
Cymbal-comparison study, kinematic impact detection from Xsens hand points (reimplemented from the paper's method description; defaults are the paper's provisional values) (Jensenius).
Source code in musicalgestures/_posetools.py
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 | |
fuse_pose_views ¶
fuse_pose_views(views, reference=0, torso=(11, 12, 23, 24), smooth=(7, 2), max_gap=None)
Fuse MediaPipe world landmarks from two or more uncalibrated camera views.
This is not calibrated triangulation: there is no camera calibration and no motion-capture ground truth. Each view gives a monocular metric 3D pose in its own gravity-aligned, hip-centred frame. The views are brought into a common frame by a single Umeyama (rotation + scale) similarity estimated from the rigid torso landmarks, then fused per landmark by a visibility-weighted average. The result is a consensus skeleton more robust than any single monocular view, plus a cross-view residual in millimetres as a quality measure.
One transform is estimated per view for the whole take, not one per frame: the per-frame fits are averaged (rotation through the nearest rotation to their arithmetic mean, scale through the median), which is what makes the alignment a property of the camera placement rather than of the pose. The translation term of each fit is deliberately discarded -- views are re-centred on the torso centroid instead, which stays stable when a view drops landmarks.
The residual is a consistency measure, not an accuracy one. Views that agree closely can still agree on a wrong pose, so a low residual says the cameras saw the same thing, not that the thing was right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
Sequence | Mapping
|
Two or more views of the same take, either a sequence or a mapping of name -> view. Each view is one of:
Views may differ in length; the shortest one sets the number of fused frames. They must agree on the number of landmarks. |
required |
reference
|
int or str
|
Which view defines the common frame,
by key when |
0
|
torso
|
sequence of int
|
Landmark indices used to estimate the alignment. Defaults to (11, 12, 23, 24) -- MediaPipe's shoulders and hips, the most rigid and best-detected group. |
(11, 12, 23, 24)
|
smooth
|
tuple
|
|
(7, 2)
|
max_gap
|
int
|
Longest run of missing frames that may be filled by interpolation before alignment. Longer dropouts are left as NaN, so a repair cannot pass for a measurement. Defaults to None, which fills every gap of any length and holds the ends flat -- the behaviour of the study scripts this is consolidated from, kept as the default so their results reproduce. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with keys:
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two views are given, if a view dict has no
|
Examples:
>>> side = mg.extract_pose_landmarks("side.mp4", world_landmarks=True)
>>> above = mg.extract_pose_landmarks("above.mp4", world_landmarks=True)
>>> fused = mg.fuse_pose_views({"side": side, "above": above},
... reference="side")
>>> fused["residual_mm"]
Source
Consolidated from the author's Westney-comparisons study scripts concert_fuse3d.py and reh_fuse3d.py, which were byte-identical but for a hardcoded list of pieces (Jensenius). Reproduces their published fusion on all four concert excerpts to within float32 storage precision.
Source code in musicalgestures/_posetools.py
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 | |