Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_mmp_file function #198

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions movement/io/load_poses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Functions for loading pose tracking data from various frameworks."""

import json
import logging
from pathlib import Path
from typing import Literal, Optional, Union
Expand Down Expand Up @@ -678,3 +679,103 @@ def _ds_from_valid_data(data: ValidPosesDataset) -> xr.Dataset:
"source_file": None,
},
)


def from_mmp_file(
file_path: Union[Path, str], fps: Optional[float] = None
) -> xr.Dataset:
"""Create a ``movement`` dataset from an MMPose .json file

Path to the .json file containing the pose tracking data.
[
{
"frame_id": int,
"instances": [
{
"keypoints": list[list[float]],
"keypoint_scores": list[float],
"bbox": list[float],
"bbox_score": float
},
...
]
},
...
]
Comment on lines +690 to +704
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This infor is useful, but better move it to the "Notes" section of the docstring, or better still add a reference to the corresponding MMPose documentation page where the format is describe. See from_sleap_file() for the syntax for "Notes" and "References".


Parameters
----------
file_path : pathlib.Path or str
Path to the JSON file containing the pose tracking data.
fps : float, optional
The number of frames per second in the video. If None (default),
the 'time' coordinates will be in frame numbers.

Returns
-------
xarray.Dataset
``movement`` dataset containing the pose tracks, confidence scores,
and associated metadata.

"""
file = ValidFile(
file_path,
expected_permission="r",
expected_suffix=[".json"],
)

# file = ValidMMPoseJSON(file_path)

with open(file.path) as f:
data = json.load(f)

all_tracks = []
all_scores = []
n_keypoints = len(
data[0]["instances"][0]["keypoints"]
) # Keypoints in the first instance of the first frame

for _, frame in enumerate(data):
# Initialize arrays for this frame's tracks and scores
n_individuals = len(frame["instances"])
frame_tracks = np.full(
(n_individuals, n_keypoints, 2), np.nan, dtype=np.float32
)
frame_scores = np.full(
(n_individuals, n_keypoints), np.nan, dtype=np.float32
)

for i, instance in enumerate(frame["instances"]):
frame_tracks[i] = np.array(instance["keypoints"])[:, :2] # (x, y)
frame_scores[i] = instance["keypoint_scores"]

all_tracks.append(frame_tracks)
all_scores.append(frame_scores)

# Stack the frames to get a 3D array
tracks_array = np.stack(all_tracks, axis=0)
scores_array = np.stack(all_scores, axis=0)

keypoint_names = [
f"keypoint_{i}" for i in range(n_keypoints)
] # Use pre-calculated keypoint names

# Create ValidPosesDataset and convert to xarray.Dataset
valid_data = ValidPosesDataset(
position_array=tracks_array,
confidence_array=scores_array,
individual_names=[
f"individual_{i}" for i in range(tracks_array.shape[1])
],
keypoint_names=keypoint_names,
fps=fps,
)
ds = _ds_from_valid_data(valid_data)

# Metadata
ds.attrs["source_software"] = "JSON"
Copy link
Member

@niksirbi niksirbi Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this "MMPose", as that is the software. json is just the file format and may be used by other software too.

ds.attrs["source_file"] = file.path.as_posix()

logger.info(f"Loaded pose tracks from {file.path}:")
logger.info(ds)
return ds
Loading