-
Notifications
You must be signed in to change notification settings - Fork 8
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
ivanvrlg
wants to merge
6
commits into
main
Choose a base branch
from
from_mmp_file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b00c586
Added from_mmp_file function
ivanvrlg f4983d7
changed _ds_from_valid_data call name
ivanvrlg c505047
Update movement/io/load_poses.py
ivanvrlg 3bc1c0c
Update movement/io/load_poses.py
ivanvrlg 4c42792
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a02f248
Update movement/io/load_poses.py
ivanvrlg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
}, | ||
... | ||
] | ||
}, | ||
... | ||
] | ||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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".