|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from argparse import ArgumentParser |
| 5 | +from pathlib import Path |
| 6 | +from typing import Dict, Iterable, List |
| 7 | + |
| 8 | +from labelformat.model.bounding_box import BoundingBox, BoundingBoxFormat |
| 9 | +from labelformat.model.category import Category |
| 10 | +from labelformat.model.object_detection_track import ( |
| 11 | + ObjectDetectionTrackInput, |
| 12 | + SingleObjectDetectionTrack, |
| 13 | + VideoObjectDetectionTrack, |
| 14 | +) |
| 15 | +from labelformat.model.video import Video |
| 16 | +from labelformat.types import JsonDict |
| 17 | + |
| 18 | + |
| 19 | +class YouTubeVISObjectDetectionTrackInput(ObjectDetectionTrackInput): |
| 20 | + @staticmethod |
| 21 | + def add_cli_arguments(parser: ArgumentParser) -> None: |
| 22 | + parser.add_argument( |
| 23 | + "--input-file", |
| 24 | + type=Path, |
| 25 | + required=True, |
| 26 | + help="Path to input YouTube-VIS JSON file", |
| 27 | + ) |
| 28 | + |
| 29 | + def __init__(self, input_file: Path) -> None: |
| 30 | + with input_file.open() as file: |
| 31 | + self._data = json.load(file) |
| 32 | + |
| 33 | + def get_categories(self) -> Iterable[Category]: |
| 34 | + for category in self._data["categories"]: |
| 35 | + yield Category( |
| 36 | + id=category["id"], |
| 37 | + name=category["name"], |
| 38 | + ) |
| 39 | + |
| 40 | + def get_videos(self) -> Iterable[Video]: |
| 41 | + for video in self._data["videos"]: |
| 42 | + yield Video( |
| 43 | + id=video["id"], |
| 44 | + # TODO (Jonas, 1/2026): The file_names do not hold the video file extension. Solution required. |
| 45 | + filename=Path(video["file_names"][0]).parent.name, |
| 46 | + width=int(video["width"]), |
| 47 | + height=int(video["height"]), |
| 48 | + number_of_frames=int(video["length"]), |
| 49 | + ) |
| 50 | + |
| 51 | + def get_labels(self) -> Iterable[VideoObjectDetectionTrack]: |
| 52 | + video_id_to_video = {video.id: video for video in self.get_videos()} |
| 53 | + category_id_to_category = { |
| 54 | + category.id: category for category in self.get_categories() |
| 55 | + } |
| 56 | + video_id_to_tracks: Dict[int, List[JsonDict]] = { |
| 57 | + video_id: [] for video_id in video_id_to_video.keys() |
| 58 | + } |
| 59 | + for ann in self._data["annotations"]: |
| 60 | + video_id_to_tracks[ann["video_id"]].append(ann) |
| 61 | + |
| 62 | + for video_id, tracks in video_id_to_tracks.items(): |
| 63 | + video = video_id_to_video[video_id] |
| 64 | + objects = [] |
| 65 | + for track in tracks: |
| 66 | + boxes = _get_object_track_boxes(ann=track) |
| 67 | + objects.append( |
| 68 | + SingleObjectDetectionTrack( |
| 69 | + category=category_id_to_category[ann["category_id"]], |
| 70 | + boxes=boxes, |
| 71 | + ) |
| 72 | + ) |
| 73 | + yield VideoObjectDetectionTrack( |
| 74 | + video=video, |
| 75 | + objects=objects, |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def _get_object_track_boxes( |
| 80 | + ann: JsonDict, |
| 81 | +) -> list[BoundingBox | None]: |
| 82 | + boxes: list[BoundingBox | None] = [] |
| 83 | + for bbox in ann["bboxes"]: |
| 84 | + if bbox is None or len(bbox) == 0: |
| 85 | + boxes.append(None) |
| 86 | + else: |
| 87 | + boxes.append( |
| 88 | + BoundingBox.from_format( |
| 89 | + bbox=[float(x) for x in bbox], |
| 90 | + format=BoundingBoxFormat.XYWH, |
| 91 | + ) |
| 92 | + ) |
| 93 | + return boxes |
0 commit comments