11from pathlib import Path
2+
23import sleap_io as sio
34from sleap_io .io import dlc
45
5-
66_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." ),
7+ "default" : (
8+ "No annotations could be extracted from the input file."
9+ "Please check that the input file contains labeled frames."
10+ ),
911 "dlc" : (
1012 "Ensure that the paths to the labelled frames are in the "
1113 "standard DLC project format: "
1214 "labeled-data / <video-name> / "
1315 "<filename-with-frame-number>.<extension>"
1416 "and that the frames files exist."
15- )
17+ ),
1618}
19+
20+
1721def annotations_to_coco (
1822 input_path : Path ,
1923 output_json_path : Path ,
@@ -22,45 +26,46 @@ def annotations_to_coco(
2226 coco_visibility_encoding : str = "ternary" ,
2327) -> Path :
2428 """Export annotations file to COCO format.
25-
29+
2630 Parameters
27- ----------
31+ ----------
2832 input_path : pathlib.Path
2933 Path to the input annotations file.
3034 output_json_path : pathlib.Path
3135 Path to save the output COCO JSON file.
3236 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),
37+ Optional image filenames to use in the COCO JSON. If provided,
38+ must be a single string (for single-frame videos) or a list of
39+ strings matching the number of labeled frames. If None (default),
3640 generates filenames from video filenames and frame indices.
3741 coco_visibility_encoding : str, optional
3842 Encoding scheme for keypoint visibility in the COCO JSON file.
3943 Options are "ternary" (0: not labeled, 1: labeled but not visible,
4044 2: labeled and visible) or "binary" (0: not visible, 1: visible).
4145 Default is "ternary".
42-
46+
4347 Returns
4448 -------
4549 pathlib.Path
4650 Path to the saved COCO JSON file.
47-
51+
4852 Notes
4953 -----
5054 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.
55+ on its extension. See :func:`sleap_io.io.main.load_file` for supported
56+ formats.
5257 """
5358 labels = sio .load_file (input_path )
5459 # Check if labels object is empty
5560 if len (labels .labeled_frames ) == 0 :
5661 error_msg = _EMPTY_LABELS_ERROR_MSG ["default" ]
5762 if dlc .is_dlc_file (input_path ):
5863 error_msg += _EMPTY_LABELS_ERROR_MSG ["dlc" ]
59- raise ValueError (error_msg )
64+ raise ValueError (error_msg )
6065 sio .save_coco (
6166 labels ,
6267 output_json_path ,
6368 image_filenames = coco_image_filenames ,
64- visibility_encoding = coco_visibility_encoding
69+ visibility_encoding = coco_visibility_encoding ,
6570 )
6671 return output_json_path
0 commit comments