@@ -21,6 +21,24 @@ def _cliplabels_suffix_error(name: str) -> str:
2121 return f"Input file must end with '_cliplabels.json', got { name } "
2222
2323
24+ def _extract_start_labels (clip_labels : dict ) -> dict :
25+ start_images = [img for img in clip_labels ["images" ] if img ["id" ] == 0 ]
26+ if len (start_images ) != 1 :
27+ raise ValueError (
28+ "Clip labels must contain exactly one image with id 0"
29+ )
30+
31+ return {
32+ "images" : start_images ,
33+ "annotations" : [
34+ annot
35+ for annot in clip_labels ["annotations" ]
36+ if annot ["image_id" ] == 0
37+ ],
38+ "categories" : clip_labels ["categories" ],
39+ }
40+
41+
2442def extract_clip (
2543 video_path : str | Path ,
2644 start_frame : int ,
@@ -228,19 +246,7 @@ def extract_start_frames_label(cliplabels_path: str | Path) -> Path:
228246 with open (cliplabels_path ) as f :
229247 clip_labels = json .load (f )
230248
231- # Extract only the first frame's data (frame with id=0)
232- start_labels = {}
233- start_labels ["images" ] = [
234- img for img in clip_labels ["images" ] if img ["id" ] == 0
235- ]
236-
237- # Extract only annotations for the first frame (image_id=0)
238- start_labels ["annotations" ] = [
239- annot for annot in clip_labels ["annotations" ] if annot ["image_id" ] == 0
240- ]
241-
242- # Keep categories unchanged
243- start_labels ["categories" ] = clip_labels ["categories" ]
249+ start_labels = _extract_start_labels (clip_labels )
244250
245251 # Generate output path by replacing _cliplabels.json with _startlabels.json
246252 output_path = cliplabels_path .parent / cliplabels_path .name .replace (
@@ -329,21 +335,7 @@ def extract_start_frames_label_s3(
329335 response = s3_client .get_object (Bucket = bucket_name , Key = key )
330336 clip_labels = json .loads (response ["Body" ].read ().decode ("utf-8" ))
331337
332- # Extract only the first frame's data (frame with id=0)
333- start_labels = {}
334- start_labels ["images" ] = [
335- img for img in clip_labels ["images" ] if img ["id" ] == 0
336- ]
337-
338- # Extract only annotations for the first frame (image_id=0)
339- start_labels ["annotations" ] = [
340- annot
341- for annot in clip_labels ["annotations" ]
342- if annot ["image_id" ] == 0
343- ]
344-
345- # Keep categories unchanged
346- start_labels ["categories" ] = clip_labels ["categories" ]
338+ start_labels = _extract_start_labels (clip_labels )
347339
348340 # Generate output key by replacing _cliplabels.json with
349341 # _startlabels.json
0 commit comments