|
13 | 13 | import shutil |
14 | 14 | from pathlib import Path |
15 | 15 |
|
16 | | -import sleap_io as sio |
17 | | - |
18 | 16 | from poseinterface.io import annotations_to_coco |
19 | 17 |
|
20 | 18 | # %% |
|
98 | 96 | # Save COCO annotations inside the Frames directory |
99 | 97 | target_annotations_path = target_frames_dir / f"{video_id}_framelabels.json" |
100 | 98 |
|
101 | | -# %% |
102 | | -# Build new filenames for COCO annotations |
103 | | -# ---------------------------------------- |
104 | | -# We need to determine the new filenames before calling annotations_to_coco. |
105 | | -# This ensures the JSON file references the renamed frame files, |
106 | | -# not the original source filenames. |
107 | | - |
108 | | -# Load annotations to get the order of labeled frames |
109 | | -labels = sio.load_file(source_annotations_path) |
110 | | - |
111 | | -# Build list of new filenames matching the order of labeled frames |
112 | | -coco_image_filenames = [] |
113 | | -for lf in labels.labeled_frames: |
114 | | - # Extract the original image path from the labeled frame |
115 | | - # For DLC, video.filename is a list of all image paths; |
116 | | - # use frame_idx to get the correct one for this labeled frame |
117 | | - filename = lf.video.filename |
118 | | - if isinstance(filename, list): |
119 | | - filename = filename[lf.frame_idx] |
120 | | - original_path = Path(filename) |
121 | | - # Extract frame number from filename, e.g. "img0042.png" -> "0042" |
122 | | - frame_number = original_path.stem.replace("img", "") |
123 | | - # Create new filename following the naming convention |
124 | | - new_filename = f"{video_id}_frame-{frame_number}.png" |
125 | | - coco_image_filenames.append(new_filename) |
126 | | - |
127 | 99 | # %% |
128 | 100 | # Convert DLC annotations to COCO format |
129 | 101 | # -------------------------------------- |
130 | 102 | # Here we use the :func:`annotations_to_coco` function from `poseinterface.io` |
131 | 103 | # which wraps around `sleap_io` functionality to perform the conversion. |
132 | | -# We pass the new filenames so the JSON references the renamed frames. |
133 | 104 |
|
134 | 105 | annotations_to_coco( |
135 | 106 | input_path=source_annotations_path, |
136 | 107 | output_json_path=target_annotations_path, |
137 | | - coco_image_filenames=coco_image_filenames, |
138 | | - coco_visibility_encoding="ternary", |
| 108 | + sub_id=subject_id, |
| 109 | + ses_id=session_id, |
| 110 | + cam_id=view_id, |
139 | 111 | ) |
140 | 112 | print(f"Saved COCO annotations to: {target_annotations_path}") |
141 | 113 |
|
|
0 commit comments