Skip to content

leap

sleap_io.io.leap

This module handles direct I/O operations for working with LEAP .mat files.

Functions:

Name Description
read_labels

Read LEAP pose data from a .mat file and return a Labels object.

read_labels(labels_path, skeleton=None)

Read LEAP pose data from a .mat file and return a Labels object.

Parameters:

Name Type Description Default
labels_path str

Path to the LEAP .mat pose file.

required
skeleton Optional[Skeleton]

An optional Skeleton object. If not provided, will be constructed from the data in the file.

None

Returns:

Type Description
Labels

Parsed labels as a Labels instance.

Source code in sleap_io/io/leap.py
def read_labels(labels_path: str, skeleton: Optional[Skeleton] = None) -> Labels:
    """Read LEAP pose data from a .mat file and return a `Labels` object.

    Args:
        labels_path: Path to the LEAP .mat pose file.
        skeleton: An optional `Skeleton` object. If not provided, will be constructed
            from the data in the file.

    Returns:
        Parsed labels as a `Labels` instance.
    """
    try:
        from pymatreader import read_mat
    except ImportError:
        raise ImportError(
            "pymatreader is required to read LEAP .mat files. "
            "Install it with: pip install sleap-io[mat]"
        )

    # Load the MATLAB data
    mat_data = read_mat(labels_path)

    # Extract video path
    video_path = mat_data.get("boxPath", None)
    if video_path is None:
        # Try to infer video path from labels path
        video_path = Path(labels_path).with_suffix(".mp4")

    # Create Video object
    video = Video.from_filename(str(video_path))

    # Parse skeleton if not provided
    if skeleton is None:
        skeleton = _parse_skeleton(mat_data)

    # Parse pose data
    labeled_frames = _parse_pose_data(mat_data, video, skeleton)

    # Create Labels object
    labels = Labels(
        labeled_frames=labeled_frames,
        videos=[video],
        skeletons=[skeleton],
    )

    return labels