Skip to content

🩹 [Fix] Labels in YOLO detection format #175

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion yolo/tools/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from yolo.tools.data_augmentation import AugmentationComposer
from yolo.tools.dataset_preparation import prepare_dataset
from yolo.utils.dataset_utils import (
convert_bboxes,
create_image_metadata,
locate_label_paths,
scale_segmentation,
Expand Down Expand Up @@ -104,7 +105,8 @@ def filter_data(self, dataset_path: Path, phase_name: str, sort_image: bool = Fa
if not label_path.is_file():
continue
with open(label_path, "r") as file:
image_seg_annotations = [list(map(float, line.strip().split())) for line in file]
annotations = [list(map(float, line.strip().split())) for line in file]
image_seg_annotations = convert_bboxes(annotations)
else:
image_seg_annotations = []

Expand Down
29 changes: 29 additions & 0 deletions yolo/utils/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ def scale_segmentation(
return seg_array_with_cat


def convert_bboxes(
annotations: list[list[float]],
) -> list[list[float]]:
"""
Converts annotations in YOLO detection format (class_id, cx, cy, w, h) or YOLO segmentation format \
(class_id, x1, y1, x2, y2, ..., xn, yn) to YOLO segmentation format.

Args:
annotations (list[list[float]]): List of annotations in any YOLO format.

Returns:
list[list[float]]: List of annotations in any YOLO segmentation format.
"""
segmentation_data = []

for anno in annotations:
# YOLO segmentation format
if len(anno) > 5:
segmentation_data.append(anno)
continue

# YOLO detection format
category_id, cx, cy, w, h = anno
x1, y1, x2, y2 = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2
segmentation_data.append([category_id, x1, y1, x2, y1, x2, y2, x1, y2])

return segmentation_data


def tensorlize(data):
try:
img_paths, bboxes, img_ratios = zip(*data)
Expand Down