Skip to content

Commit 0a9e9cf

Browse files
committed
Use generic loader
1 parent e5a5565 commit 0a9e9cf

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

poseinterface/io.py

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,66 @@
11
from pathlib import Path
2+
import sleap_io as sio
3+
from sleap_io.io import dlc
24

3-
from sleap_io.io import coco, dlc
45

5-
6-
def format_dlc_annotations_file(
6+
_EMPTY_LABELS_ERROR_MSG = {
7+
"default": ("No annotations could be extracted from the input file."
8+
"Please check that the input file contains labeled frames."),
9+
"dlc": (
10+
"Ensure that the paths to the labelled frames are in the "
11+
"standard DLC project format: "
12+
"labeled-data / <video-name> / "
13+
"<filename-with-frame-number>.<extension>"
14+
"and that the frames files exist."
15+
)
16+
}
17+
def annotations_to_coco(
718
input_path: Path,
819
output_json_path: Path,
20+
*,
921
coco_image_filenames: str | list[str] | None = None,
1022
coco_visibility_encoding: str = "ternary",
11-
) -> dict:
12-
"""Export input DLC annotations file to COCO format."""
13-
# Read annotations as Labels object
14-
labels = dlc.load_dlc(input_path, video_search_paths=None)
15-
23+
) -> Path:
24+
"""Export annotations file to COCO format.
25+
26+
Parameters
27+
----------
28+
input_path : pathlib.Path
29+
Path to the input annotations file.
30+
output_json_path : pathlib.Path
31+
Path to save the output COCO JSON file.
32+
coco_image_filenames : str | list[str] | None, optional
33+
Optional image filenames to use in the COCO JSON. If provided,
34+
must be a single string (for single-frame videos) or a list of
35+
strings matching the number of labeled frames. If None (default),
36+
generates filenames from video filenames and frame indices.
37+
coco_visibility_encoding : str, optional
38+
Encoding scheme for keypoint visibility in the COCO JSON file.
39+
Options are "ternary" (0: not labeled, 1: labeled but not visible,
40+
2: labeled and visible) or "binary" (0: not visible, 1: visible).
41+
Default is "ternary".
42+
43+
Returns
44+
-------
45+
pathlib.Path
46+
Path to the saved COCO JSON file.
47+
48+
Notes
49+
-----
50+
The format of the input annotations file is automatically inferred based
51+
on its extension. See :func:`sleap_io.io.main.load_file` for supported formats.
52+
"""
53+
labels = sio.load_file(input_path)
1654
# Check if labels object is empty
1755
if len(labels.labeled_frames) == 0:
18-
raise ValueError(
19-
"No annotations could be extracted from the input file."
20-
"Please check the paths to the labelled frames are in the "
21-
"standard DLC project format: "
22-
"labeled-data / <video-name> / "
23-
"<filename-with-frame-number>.<extension>"
24-
"and that the frames files exist."
25-
)
26-
27-
# Export Labels object to COCO
28-
coco.write_labels(
56+
error_msg = _EMPTY_LABELS_ERROR_MSG["default"]
57+
if dlc.is_dlc_file(input_path):
58+
error_msg += _EMPTY_LABELS_ERROR_MSG["dlc"]
59+
raise ValueError(error_msg)
60+
sio.save_coco(
2961
labels,
3062
output_json_path,
31-
visibility_encoding=coco_visibility_encoding,
3263
image_filenames=coco_image_filenames,
64+
visibility_encoding=coco_visibility_encoding
3365
)
34-
3566
return output_json_path

0 commit comments

Comments
 (0)