Skip to content

Commit

Permalink
Support alternative yolov8 'names' loading
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorSusmelj committed Dec 21, 2024
1 parent d2f038a commit 8c5c247
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/labelformat/formats/yolov8.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,45 @@ def __init__(self, input_file: Path, input_split: str) -> None:
)

def get_categories(self) -> Iterable[Category]:
for category_id, category_name in self._config_data["names"].items():
yield Category(
id=int(category_id),
name=category_name,
"""Get categories from YOLOv8 config file.
Supports two formats for the 'names' field:
- dictionary mapping id to name
Example:
names:
0: person
1: dog
2: cat
- list of names where index is the id
Example:
names:
- person
- dog
- cat
or
names: [person, dog, cat]
Returns:
Iterator of Category objects.
"""
names = self._config_data["names"]
if isinstance(names, dict):
# Original YOLOv8 format: dictionary mapping id to name
for category_id, category_name in names.items():
yield Category(
id=int(category_id),
name=category_name,
)
elif isinstance(names, list):
# Alternative YOLO format: list of names where index is the id
for category_id, category_name in enumerate(names):
yield Category(
id=category_id,
name=category_name,
)
else:
raise ValueError(
f"Invalid 'names' format in config file '{self._config_file}'. "
"Expected dictionary or list, got {type(names)}"
)

def get_images(self) -> Iterable[Image]:
Expand Down

0 comments on commit 8c5c247

Please sign in to comment.