PoseEstimator¶
Pose estimator interface and backends for MGT-python.
This module provides:
- :class:
PoseEstimator– an abstract base class (ABC) defining the common interface that all pose backends must implement. - :class:
MediaPipePoseEstimator– a concrete backend powered by Google MediaPipe Pose (33 landmarks, CPU-friendly, zero model download). - :class:
OpenPosePoseEstimator– a thin wrapper around the legacy OpenPose / Caffe-model implementation already present in :mod:musicalgestures._pose.
The shared interface means that backends are interchangeable::
from musicalgestures._pose_estimator import MediaPipePoseEstimator
est = MediaPipePoseEstimator()
keypoints = est.predict_frame(frame) # → np.ndarray shape (33, 3)
Examples¶
import numpy as np frame = np.zeros((480, 640, 3), dtype=np.uint8)
Without mediapipe installed this raises MgDependencyError gracefully.¶
PoseEstimatorResult ¶
PoseEstimatorResult(keypoints, landmark_names, frame_index=0, timestamp=0.0)
Container for the output of a single-frame pose estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keypoints
|
ndarray
|
2-D array of shape |
required |
landmark_names
|
list[str]
|
List of keypoint names corresponding to each row. |
required |
frame_index
|
int
|
Frame index this result belongs to. |
0
|
timestamp
|
float
|
Timestamp in seconds. |
0.0
|
Source code in musicalgestures/_pose_estimator.py
157 158 159 160 161 162 163 164 165 166 167 | |
to_dict ¶
to_dict()
Return a plain dict representation.
Source code in musicalgestures/_pose_estimator.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
PoseEstimator ¶
PoseEstimator(model=PoseModel.MEDIAPIPE, device=PoseDevice.CPU)
Bases: ABC
Abstract base class for pose estimation backends.
All concrete subclasses must implement :meth:predict_frame and
:meth:landmark_names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PoseModel | str
|
Skeleton model variant. |
MEDIAPIPE
|
device
|
PoseDevice | str
|
Compute backend ( |
CPU
|
Source code in musicalgestures/_pose_estimator.py
209 210 211 212 213 214 215 | |
predict_frame
abstractmethod
¶
predict_frame(frame)
Run pose estimation on a single BGR frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
Input frame as a NumPy array of shape |
required |
Returns:
| Type | Description |
|---|---|
PoseEstimatorResult
|
|
Source code in musicalgestures/_pose_estimator.py
222 223 224 225 226 227 228 229 230 231 232 233 234 | |
predict_video ¶
predict_video(filename, start=0.0, end=None, skip=0)
Run pose estimation on every frame of a video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Path to the video file. |
required |
start
|
float
|
Start time in seconds. |
0.0
|
end
|
float | None
|
End time in seconds (None = full video). |
None
|
skip
|
int
|
Process every (1 + skip)-th frame. |
0
|
Returns:
| Type | Description |
|---|---|
list[PoseEstimatorResult]
|
|
Source code in musicalgestures/_pose_estimator.py
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 | |
MediaPipePoseEstimator ¶
MediaPipePoseEstimator(model_complexity=1, min_detection_confidence=0.5, min_tracking_confidence=0.5, device=PoseDevice.CPU)
Bases: PoseEstimator
Pose estimator backed by Google MediaPipe Pose (Tasks API).
Requires the optional mediapipe>=0.10 package::
pip install musicalgestures[pose]
The first time you use a given complexity level the corresponding
.task model file (~8–28 MB) is downloaded from Google's model
storage and cached in musicalgestures/models/.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_complexity
|
int
|
MediaPipe model complexity (0 = lite, 1 = full, 2 = heavy). Higher values are more accurate but slower. Default: 1. |
1
|
min_detection_confidence
|
float
|
Minimum confidence for initial body detection. Default: 0.5. |
0.5
|
min_tracking_confidence
|
float
|
Minimum confidence for landmark tracking. Default: 0.5. |
0.5
|
Examples:
>>> import numpy as np
>>> est = MediaPipePoseEstimator()
>>> frame = np.zeros((480, 640, 3), dtype=np.uint8)
>>> result = est.predict_frame(frame)
>>> result.keypoints.shape # (33, 3)
Source code in musicalgestures/_pose_estimator.py
314 315 316 317 318 319 320 321 322 323 324 325 | |
predict_frame ¶
predict_frame(frame)
Run MediaPipe Pose on a single BGR frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
BGR frame, shape |
required |
Returns:
| Type | Description |
|---|---|
PoseEstimatorResult
|
33 landmarks; |
Source code in musicalgestures/_pose_estimator.py
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 | |
close ¶
close()
Release MediaPipe resources.
Source code in musicalgestures/_pose_estimator.py
424 425 426 427 428 | |
OpenPosePoseEstimator ¶
OpenPosePoseEstimator(model=PoseModel.BODY_25, device=PoseDevice.GPU, threshold=0.1)
Bases: PoseEstimator
Thin wrapper around the legacy OpenPose / Caffe-model backend.
This class delegates to :func:musicalgestures._pose.pose and is
provided so that the old OpenPose workflow can be used through the
same :class:PoseEstimator interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
PoseModel | str
|
One of |
BODY_25
|
device
|
PoseDevice | str
|
|
GPU
|
threshold
|
float
|
Minimum confidence threshold. Default: 0.1. |
0.1
|
Source code in musicalgestures/_pose_estimator.py
454 455 456 457 458 459 460 461 462 | |
predict_frame ¶
predict_frame(frame)
Run OpenPose inference on a single BGR frame.
.. note::
Full video-level processing is better handled by calling
:meth:MgVideo.pose directly.
Source code in musicalgestures/_pose_estimator.py
469 470 471 472 473 474 475 476 477 478 479 | |
get_pose_model_path ¶
get_pose_model_path(model_complexity=1, models_dir=None)
Return the local path of the MediaPipe pose .task model file,
downloading and caching it on first use.
This is the shared model download/cache used by both
:class:MediaPipePoseEstimator (per-frame estimation, MgVideo.pose())
and :func:musicalgestures.extract_pose_landmarks (whole-video landmark
trajectories), so a given model file is only downloaded once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_complexity
|
int
|
MediaPipe model variant: 0 (lite), 1 (full) or 2 (heavy). Invalid values fall back to 1 with a warning. Defaults to 1. |
1
|
models_dir
|
Path or str
|
Directory to cache the model file
in. Defaults to None (the |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
Path to the cached model file (~8-28 MB, downloaded from |
Path
|
Google's model storage if not already present). |
Raises:
| Type | Description |
|---|---|
MgDependencyError
|
If the model file is missing and the download fails. |
Source code in musicalgestures/_pose_estimator.py
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 | |
get_pose_estimator ¶
get_pose_estimator(backend='mediapipe', **kwargs)
Factory function: return a :class:PoseEstimator for the requested backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
|
'mediapipe'
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to the estimator constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
PoseEstimator
|
|
Examples:
>>> est = get_pose_estimator("mediapipe", model_complexity=0)
Source code in musicalgestures/_pose_estimator.py
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 | |