from_coco_file#
- movement.io.load_poses.from_coco_file(file, fps=None, *, annotations_file=None, category_as_track=False, use_frame_numbers_from_file=False)[source]#
Create a
movementposes dataset from a COCO keypoint results file.- Parameters:
file (
str|Path) – Path to the COCO keypoint detection results JSON file.fps (
float|None) – Frames per second. If None (default),timecoordinates are frame numbers.annotations_file (
str|Path|None) – Optional path to a COCO annotations JSON file, used for keypoint and individual names (see Notes).category_as_track (
bool) – If True, treat eachcategory_idas one individual tracked across frames. If False (default), assign individuals by order of detection within each frame (see Notes).use_frame_numbers_from_file (
bool) – Frame numbers are taken from theimage_idof each detection. If False (default), frame numbers are offset so that the lowest image ID becomes frame 0. If True, frame numbers are kept as the image IDs in the file. This may be useful if only a subset of the video’s frames were processed, but you want to keep the start of the video as the time origin. In both cases, the spacing between images is preserved, so any gaps show up in thetimecoordinates. For example, image IDs[5, 6, 9]in the file become[0, 1, 4]by default and remain[5, 6, 9]if True.
- Returns:
movementpose tracks and confidence scores.- Return type:
Notes
COCO keypoint results do not contain track identities. By default, the
i-th detection of each image (in file order) is assigned to individuali(namedid_i), so an individual does not necessarily correspond to the same animal across frames. How to handle this depends on your data:One animal per frame: no action needed, all detections are assigned to
id_0.Multiple animals, one category per animal: set
category_as_track=True. Each category present in the results then becomes one individual, named after the category ifannotations_fileis provided, or after itscategory_id(e.g."3") otherwise. Multiple detections of the same category in the same image raise aValueError.Multiple animals sharing a category: identities are not reliable across frames. Assign them with a tracking tool before analyses that follow individuals over time (e.g. kinematics), and load the tracked output instead of the raw COCO results.
In
movement, pose data can currently only be loaded if all individuals share the same set of keypoints. All detections in the results must therefore have the same number of keypoints. Ifannotations_fileis provided, all its categories must also have the samekeypointslist (same names, in the same order), with one name per keypoint in each detection. Otherwise, aValueErroris raised. Without an annotations file, keypoints are assigned default nameskeypoint_0,keypoint_1, etc. in file order.Each keypoint in COCO results is stored as
(x, y, v), wherevis a visibility flag. As COCO recommends settingv=1for all predicted keypoints,vcarries no information in results files and is discarded. Onlyxandyare loaded asposition. The per-detectionscoreis loaded as individual-wiseconfidence, with dimensions(time, individual).Examples
Create a dataset from the COCO results file at “path/to/results.json”, with the time coordinates in seconds, and the keypoints named after the categories in the annotations file at “path/to/annotations.json”.
>>> from movement.io import load_poses >>> ds = load_poses.from_coco_file( ... "path/to/results.json", ... fps=30, ... annotations_file="path/to/annotations.json", ... )
Create a dataset where each category identifies one individual tracked across frames, with individuals named after the categories in the annotations file.
>>> ds = load_poses.from_coco_file( ... "path/to/results.json", ... annotations_file="path/to/annotations.json", ... category_as_track=True, ... )
Create a dataset using the image IDs as frame numbers, so that t = 0 seconds corresponds to image ID 0 (e.g. the first frame of the full video).
>>> ds = load_poses.from_coco_file( ... "path/to/results.json", ... fps=30, ... use_frame_numbers_from_file=True, ... )