-
Notifications
You must be signed in to change notification settings - Fork 9
Video support: Add youtubevis input format #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JonasWurst
merged 7 commits into
main
from
jonas-lig-8150-load-video-annotations-from-youtube-vis-labelformat-youtube-vis-format
Jan 15, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
473bb9c
Adding models: video and object detection track
JonasWurst 6bd5ed3
Add youtubevis input format
JonasWurst 6c7582c
Merge commit 'd8f8f84a2007ff89a9e6202d7eaadbc326937404' into jonas-li…
JonasWurst 1b4afd5
Video support: Add youtubevis input format
JonasWurst 1982968
revert python version
JonasWurst 230d907
Review comments
JonasWurst 59d6441
format
JonasWurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from argparse import ArgumentParser | ||
| from pathlib import Path | ||
| from typing import Dict, Iterable, List | ||
|
|
||
| from labelformat.model.bounding_box import BoundingBox, BoundingBoxFormat | ||
| from labelformat.model.category import Category | ||
| from labelformat.model.object_detection_track import ( | ||
| ObjectDetectionTrackInput, | ||
| SingleObjectDetectionTrack, | ||
| VideoObjectDetectionTrack, | ||
| ) | ||
| from labelformat.model.video import Video | ||
| from labelformat.types import JsonDict | ||
|
|
||
|
|
||
| class YouTubeVISObjectDetectionTrackInput(ObjectDetectionTrackInput): | ||
| @staticmethod | ||
| def add_cli_arguments(parser: ArgumentParser) -> None: | ||
| parser.add_argument( | ||
| "--input-file", | ||
| type=Path, | ||
| required=True, | ||
| help="Path to input YouTube-VIS JSON file", | ||
| ) | ||
|
|
||
| def __init__(self, input_file: Path) -> None: | ||
| with input_file.open() as file: | ||
| self._data = json.load(file) | ||
|
|
||
| def get_categories(self) -> Iterable[Category]: | ||
| for category in self._data["categories"]: | ||
| yield Category( | ||
| id=category["id"], | ||
| name=category["name"], | ||
| ) | ||
|
|
||
| def get_videos(self) -> Iterable[Video]: | ||
| for video in self._data["videos"]: | ||
| yield Video( | ||
| id=video["id"], | ||
| # TODO (Jonas, 1/2026): The file_names do not hold the video file extension. Solution required. | ||
| filename=Path(video["file_names"][0]).parent.name, | ||
| width=int(video["width"]), | ||
| height=int(video["height"]), | ||
| number_of_frames=int(video["length"]), | ||
| ) | ||
|
|
||
| def get_labels(self) -> Iterable[VideoObjectDetectionTrack]: | ||
| video_id_to_video = {video.id: video for video in self.get_videos()} | ||
| category_id_to_category = { | ||
| category.id: category for category in self.get_categories() | ||
| } | ||
| video_id_to_tracks: Dict[int, List[JsonDict]] = { | ||
| video_id: [] for video_id in video_id_to_video.keys() | ||
| } | ||
| for ann in self._data["annotations"]: | ||
| video_id_to_tracks[ann["video_id"]].append(ann) | ||
|
|
||
| for video_id, tracks in video_id_to_tracks.items(): | ||
| video = video_id_to_video[video_id] | ||
| objects = [] | ||
| for track in tracks: | ||
| boxes = _get_object_track_boxes(ann=track) | ||
| objects.append( | ||
| SingleObjectDetectionTrack( | ||
| category=category_id_to_category[ann["category_id"]], | ||
| boxes=boxes, | ||
| ) | ||
| ) | ||
| yield VideoObjectDetectionTrack( | ||
| video=video, | ||
| objects=objects, | ||
| ) | ||
|
|
||
|
|
||
| def _get_object_track_boxes( | ||
| ann: JsonDict, | ||
| ) -> list[BoundingBox | None]: | ||
| boxes: list[BoundingBox | None] = [] | ||
| for bbox in ann["bboxes"]: | ||
| if bbox is None or len(bbox) == 0: | ||
| boxes.append(None) | ||
| else: | ||
| boxes.append( | ||
| BoundingBox.from_format( | ||
| bbox=[float(x) for x in bbox], | ||
| format=BoundingBoxFormat.XYWH, | ||
| ) | ||
| ) | ||
| return boxes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from labelformat.formats.youtubevis import YouTubeVISObjectDetectionTrackInput | ||
| from labelformat.model.bounding_box import BoundingBox | ||
| from labelformat.model.category import Category | ||
| from labelformat.model.object_detection_track import ( | ||
| SingleObjectDetectionTrack, | ||
| VideoObjectDetectionTrack, | ||
| ) | ||
| from labelformat.model.video import Video | ||
|
|
||
|
|
||
| class TestYouTubeVISObjectDetectionTrackInput: | ||
| def test_get_categories(self, tmp_path: Path) -> None: | ||
| input_file = _write_youtube_vis_json(tmp_path / "instances.json") | ||
| label_input = YouTubeVISObjectDetectionTrackInput(input_file=input_file) | ||
|
|
||
| assert list(label_input.get_categories()) == [Category(id=1, name="cat")] | ||
|
|
||
| def test_get_videos(self, tmp_path: Path) -> None: | ||
| input_file = _write_youtube_vis_json(tmp_path / "instances.json") | ||
| label_input = YouTubeVISObjectDetectionTrackInput(input_file=input_file) | ||
|
|
||
| assert list(label_input.get_videos()) == [ | ||
| Video( | ||
| id=5, | ||
| filename="video1", | ||
| width=640, | ||
| height=480, | ||
| number_of_frames=2, | ||
| ) | ||
| ] | ||
|
|
||
| def test_get_labels(self, tmp_path: Path) -> None: | ||
| input_file = _write_youtube_vis_json(tmp_path / "instances.json") | ||
| label_input = YouTubeVISObjectDetectionTrackInput(input_file=input_file) | ||
|
|
||
| assert list(label_input.get_labels()) == [ | ||
| VideoObjectDetectionTrack( | ||
| video=Video( | ||
| id=5, | ||
| filename="video1", | ||
| width=640, | ||
| height=480, | ||
| number_of_frames=2, | ||
| ), | ||
| objects=[ | ||
| SingleObjectDetectionTrack( | ||
| category=Category(id=1, name="cat"), | ||
| boxes=[ | ||
| BoundingBox( | ||
| xmin=10.0, | ||
| ymin=20.0, | ||
| xmax=40.0, | ||
| ymax=60.0, | ||
| ), | ||
| None, | ||
| ], | ||
| ) | ||
| ], | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def _write_youtube_vis_json(input_file: Path) -> Path: | ||
| data = { | ||
| "categories": [ | ||
| {"id": 1, "name": "cat"}, | ||
| ], | ||
| "videos": [ | ||
| { | ||
| "id": 5, | ||
| "file_names": ["video1/00000.jpg", "video1/00001.jpg"], | ||
| "width": 640, | ||
| "height": 480, | ||
| "length": 2, | ||
| } | ||
| ], | ||
| "annotations": [ | ||
| { | ||
| "video_id": 5, | ||
| "category_id": 1, | ||
| "bboxes": [ | ||
| [10.0, 20.0, 30.0, 40.0], | ||
| None, | ||
| ], | ||
| } | ||
| ], | ||
| } | ||
| input_file.write_text(json.dumps(data)) | ||
| return input_file |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.