Pose¶
Auto-generated documentation for musicalgestures._pose module.
- Mgt-python / Modules / Musicalgestures / Pose
Attributes¶
MEDIAPIPE_POSE_CONNECTIONS- MediaPipe Pose skeleton connections (pairs of landmark indices):[(0, 1), (1, 2), (2, 3), (3, 7), (0, 4), (4, 5)...OPENPOSE_NAMES- OpenPose marker names per model (order matches the network keypoint indices):{'coco': ['Nose', 'Neck', 'Right Shoulder', 'Ri...
download_model¶
Download the OpenPose Caffe weights (.caffemodel) for the given model type into the package's
pose/ folder.
Uses Python's urllib directly (cross-platform, no external wget / shell scripts /
bundled binary). The .prototxt configs ship with the package; only the large weights file
is fetched.
mg_pose_center¶
def mg_pose_center(
self,
save_data: bool = True,
dpi: int = 200,
target_name: str | None = None,
overwrite: bool = True,
**pose_kwargs,
) -> 'MgFigure':
Centre the pose data on its global centroid — a 2D port of the MoCap Toolbox mccenter.
A single offset per coordinate (the mean of the per-marker temporal means, missing detections
ignored) is subtracted from every marker so the overall spatiotemporal centroid sits at the
origin (0, 0). This removes the performer's absolute position in the frame, leaving relative
posture/movement — useful before comparing or further analysing trajectories. Plots the centred
marker trajectories and (by default) saves a CSV of the centred coordinates. Uses cached pose
keypoints when available, otherwise runs pose() first (**pose_kwargs are forwarded).
Arguments¶
save_databool, optional - Save a CSV of the centred coordinates. Defaults to True.dpiint, optional - Output DPI. Defaults to 200.target_namestr, optional - Output name. Defaults to None ("_pose_centered.png").overwritebool, optional - Overwrite or auto-increment the filename. Defaults to True.**pose_kwargs- Forwarded topose()if keypoints have to be computed.
Returns¶
MgFigure- the centred-trajectories figure;.data['coords']holds the (T, n, 2) centred coordinates and.data['offset']the removed centroid. None if there are too few frames.
mg_pose_distance¶
def mg_pose_distance(
self,
dpi: int = 200,
target_name: str | None = None,
overwrite: bool = True,
**pose_kwargs,
) -> 'MgFigure':
Per-marker distance travelled and the average across markers — a 2D port of the MoCap Toolbox
mccumdist.
Sums each marker's frame-to-frame Euclidean displacement (in pixels) and accumulates it over
time. The figure shows the per-marker cumulative-distance curves and a ranked bar chart of the
total distance per marker with the across-marker average marked; a CSV of the totals (plus the
average) is saved. Uses cached pose keypoints when available, otherwise runs pose() first
(**pose_kwargs are forwarded).
Arguments¶
dpiint, optional - Output DPI. Defaults to 200.target_namestr, optional - Output name. Defaults to None ("_pose_distance.png").overwritebool, optional - Overwrite or auto-increment the filename. Defaults to True.**pose_kwargs- Forwarded topose()if keypoints have to be computed.
Returns¶
MgFigure-.data['total'](per-marker totals),.data['average'](mean total), and.data['cumulative'](per-marker cumulative curves). None if there are too few frames.
mg_pose_segments¶
def mg_pose_segments(
self,
segments: list | None = None,
n_bins: int = 36,
cmap: str = 'viridis',
dpi: int = 200,
ncols: int = 6,
target_name: str | None = None,
overwrite: bool = True,
**pose_kwargs,
) -> 'MgFigure':
Circular (polar) motion plots and statistics for each body segment.
A segment is the bone between two connected joints (e.g. shoulder–elbow). For every segment
this computes its per-frame orientation angle and draws a polar rose histogram of the angle
distribution with the mean-direction resultant vector, annotated with circular statistics
(mean angle, resultant length R, and range of motion). A CSV of the per-segment statistics —
mean angle, R, circular std, range of motion, and mean angular speed — is saved alongside the
image. Uses cached pose keypoints from a previous pose() call when available; otherwise it
runs pose estimation first (model/device/… are forwarded to pose()).
Arguments¶
segmentslist, optional - Subset of connections as(a, b)joint-index tuples. Defaults to all skeleton connections.n_binsint, optional - Number of angular bins per rose. Defaults to 36 (10° bins).cmapstr, optional - Matplotlib colormap for the bars. Defaults to 'viridis'.dpiint, optional - Output DPI. Defaults to 200.ncolsint, optional - Columns in the subplot grid. Defaults to 6.target_namestr, optional - Output name. Defaults to None ("_pose_segments.png").overwritebool, optional - Overwrite or auto-increment the filename. Defaults to True.**pose_kwargs- Forwarded topose()if keypoints have to be computed.
Returns¶
MgFigure- the grid of circular plots (per-segment stats in.data['stats']), or None.
mg_pose_waterfall¶
def mg_pose_waterfall(
self,
style: str = 'trajectories',
n_samples: int = 40,
markers: list | None = None,
color_by: str | None = None,
cmap: str = 'hsv',
dpi: int = 200,
elev: float = 20,
azim: float = -60,
lw: float = 1.0,
axes: bool = True,
crop: bool = False,
target_name: str | None = None,
overwrite: bool = True,
**pose_kwargs,
) -> 'MgFigure':
Render a 3D spatio-temporal waterfall of the pose, cascading along the time (depth) axis —
a pose-based counterpart to silhouette_waterfall(). Uses cached pose keypoints from a
previous pose() call when available; otherwise it runs pose estimation first (extra
keyword arguments such as model/device/downsampling_factor are forwarded to
pose()).
Arguments¶
stylestr, optional - What to draw.'trajectories'(default) draws each marker's continuous path through (x, time, y);'markers'scatters the markers atn_samplestime slices;'skeleton'draws the skeleton joint lines at each time slice;'both'draws markers + skeleton.n_samplesint, optional - Number of time slices for the marker/skeleton styles. Defaults to 40 (ignored for'trajectories').markerslist, optional - Subset of marker names or indices to draw. Defaults to all.color_bystr, optional -'marker'or'time'. Defaults to None ("auto"): 'marker' for trajectories, 'time' for the slice styles.cmapstr, optional - Matplotlib colormap. Defaults to 'hsv'.dpiint, optional - Output DPI. Defaults to 200.elevfloat, optional - 3D elevation angle. Defaults to 20.azimfloat, optional - 3D azimuth angle. Defaults to -60.lwfloat, optional - Line width. Defaults to 1.0.axesbool, optional - Draw the axes and tick labels. Set to False for a clean render with all axes and text removed. Defaults to True.cropbool, optional - Tighten the spatial limits to the marker extent and trim the surrounding whitespace, so the figure shows mostly the data. Defaults to False.target_namestr, optional - Output name. Defaults to None ("_pose_waterfall.png").overwritebool, optional - Overwrite or auto-increment the filename. Defaults to True.**pose_kwargs- Forwarded topose()if keypoints have to be computed.
Returns¶
MgFigure- the 3D waterfall figure, or None if there are too few frames.
pose¶
def pose(
self,
model: str = 'mediapipe',
device: str = 'gpu',
threshold: float = 0.1,
downsampling_factor: int = 2,
use_cache: bool = True,
save_data: bool = True,
data_format: str | list = 'csv',
save_video: bool = True,
style: str = 'both',
overlay: bool = True,
background: str = 'black',
convert: bool | None = None,
quiet: bool = True,
marker_history: int = 0,
save_average_pose: bool = True,
save_trajectories: bool = True,
transparent_trajectories: bool | None = None,
trajectory_background: str | None = None,
trajectory_labels: bool = False,
target_name_video: str | None = None,
target_name_data: str | None = None,
target_name_average: str | None = None,
target_name_trajectories: str | None = None,
overwrite: bool = True,
) -> 'musicalgestures.MgVideo':
Renders a video with the pose estimation (aka. "keypoint detection" or "skeleton tracking") overlaid on it. Outputs the predictions in a text file containing the normalized x and y coordinates of each keypoint (default format is csv).
Supports two backends:
- MediaPipe (
model='mediapipe'): Uses Google's MediaPipe Pose which detects 33 landmarks. Runs on CPU, or on GPU via MediaPipe's GPU delegate whendevice='gpu'(with automatic CPU fallback if the delegate is unavailable). Requires the optionalmediapipepackage (pip install musicalgestures[pose]). On first use, the model file (~8–28 MB) is downloaded automatically and cached inmusicalgestures/models/. - OpenPose (
model='body_25','coco', or'mpi'): Uses Caffe-based OpenPose models. Model weights (~200 MB) are downloaded on first use. GPU here requires an OpenCV built with CUDA; if unavailable whiledevice='gpu',pose()automatically switches to the MediaPipe backend (when installed) for GPU acceleration.
Arguments¶
modelstr, optional - Pose model to use.'mediapipe'(default) uses MediaPipe Pose (33 landmarks with depth + visibility, model auto-downloaded on first use); it is fast on plain CPU, needs no CUDA build, and is best for single-person analysis.'body_25'loads the OpenPose BODY_25 model (25 keypoints),'mpi'loads the MPII model (15 keypoints),'coco'loads the COCO model (18 keypoints). The OpenPose models support multi-person scenes but are slow without a CUDA-enabled OpenCV build. Defaults to 'mediapipe'.devicestr, optional - Compute backend ('cpu' or 'gpu'). For OpenPose models this selects the OpenCV DNN backend (GPU needs a CUDA-enabled OpenCV). For MediaPipe it selects the inference delegate (GPU delegate with CPU fallback). Defaults to 'gpu'.thresholdfloat, optional - The normalized confidence threshold that decides whether we keep or discard a predicted point. Discarded points get substituted with (0, 0) in the output data. Defaults to 0.1.downsampling_factorint, optional - Decides how much we downsample the video before we pass it to the neural network. Ignored whenmodel='mediapipe'. Defaults to 2.use_cachebool, optional - If True (default), reuse keypoints from a previous pose() run on this object (same model/threshold) to re-render a differentstyle/overlay/backgroundwithout re-running the network — e.g. runstyle='markers'thenstyle='skeleton'fast. Defaults to True.save_databool, optional - Whether we save the predicted pose data to a file. Defaults to True.data_formatstr, optional - Specifies format of pose-data. Accepted values are 'csv', 'tsv', 'txt' and 'c3d' (motion-capture format; requires the optionalc3dpackage). For multiple output formats, use a list, e.g. ['csv', 'c3d']. Defaults to 'csv'.save_videobool, optional - Whether we save the video with the estimated pose overlaid on it. Defaults to True.stylestr, optional - How to draw the pose.'both'draws markers (keypoints) connected by joint lines (the skeleton);'markers'draws only the keypoints;'skeleton'draws only the connecting joint lines. Defaults to 'both'.overlaybool, optional - If True, draw the pose on top of the original video frames. If False, draw it on a plain background instead (a "markers only" video with no video underneath). Defaults to True.backgroundstr, optional - Background colour used whenoverlay=False:'black'(default) or'white'. With'white'the skeleton and markers are drawn in black (an inverted, print-friendly look). With'black'they are drawn in bright colours. Ignored whenoverlay=True.marker_historyint, optional - If greater than 0, draw a motion trail for every marker by joining its positions over the lastmarker_historyframes. Defaults to 0 (no trails). Works in all rendering paths (OpenPose, MediaPipe, and cached re-render).convertbool, optional - Whether non-AVI input is first converted to an all-intra MJPEG.avi(cached asself.as_avi) for frame-accurate decoding. Defaults to None ("auto"): the MediaPipe backend reads the source file directly (it decodes sequentially through an FFmpeg pipe and needs no intra-frame AVI), while the OpenPose backend converts. Pass True/False to force the behaviour.quietbool, optional - MediaPipe only. If True (default), suppress MediaPipe's native C++/GL console logs (EGL init, absl INFO/WARNING, GPU-delegate messages) during inference. Set to False to see them for debugging.target_name_videostr, optional - Target output name for the video. Defaults to None (which assumes that the input filename with the suffix "_pose" should be used).save_average_posebool, optional - Whether to also render an image of the average pose over the whole video, with each marker coloured/labelled by its average quantity of motion (px/frame) and labelled with its dominant movement frequency (Hz). A CSV of the per-marker statistics is saved alongside it. Defaults to True.save_trajectoriesbool, optional - Whether to also render an image of every marker's spatial trajectory across the whole video. Defaults to True.trajectory_labelsbool, optional - Whether to annotate the trajectories image with each marker's name. Defaults to False (cleaner image).trajectory_backgroundstr, optional - Background of the trajectories PNG:'black','white', or'transparent'(for overlaying on the video). Defaults to None("auto")- transparent when the trajectories image is the only one exported, else black. Takes precedence over the legacytransparent_trajectoriesflag.
target_name_datastr, optional - Target output name for the data. Defaults to None (which assumes that the input filename with the suffix "_pose" should be used).target_name_averagestr, optional - Target output name for the average-pose image. Defaults to None (input filename with the suffix "_pose_average.png").target_name_trajectoriesstr, optional - Target output name for the trajectories image. Defaults to None (input filename with the suffix "_pose_trajectories.png").overwritebool, optional - Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True.
Returns¶
MgVideo- An MgVideo pointing to the output video. The average-pose and trajectories images (when rendered) are attached as.average_poseand.trajectories(MgImage), and the collected keypoints are available on the parent object asself.pose_average/self.pose_trajectories.