Skip to content

Commit bbad548

Browse files
https://github.com/MultimediaTechLab/YOLO/pull/175
1 parent e58d789 commit bbad548

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

yolo/tools/data_loader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from yolo.tools.data_augmentation import AugmentationComposer
1717
from yolo.tools.dataset_preparation import prepare_dataset
1818
from yolo.utils.dataset_utils import (
19+
convert_bboxes,
1920
create_image_metadata,
2021
locate_label_paths,
2122
scale_segmentation,
@@ -116,7 +117,8 @@ def filter_data(self, dataset_path: Path, phase_name: str, sort_image: bool = Fa
116117
image_seg_annotations = []
117118
else:
118119
with open(label_path, "r") as file:
119-
image_seg_annotations = [list(map(float, line.strip().split())) for line in file]
120+
annotations = [list(map(float, line.strip().split())) for line in file]
121+
image_seg_annotations = convert_bboxes(annotations)
120122
else:
121123
image_seg_annotations = []
122124

yolo/utils/dataset_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ def scale_segmentation(
116116
return seg_array_with_cat
117117

118118

119+
def convert_bboxes(
120+
annotations: list[list[float]],
121+
) -> list[list[float]]:
122+
"""
123+
Converts annotations in YOLO detection format (class_id, cx, cy, w, h) or YOLO segmentation format \
124+
(class_id, x1, y1, x2, y2, ..., xn, yn) to YOLO segmentation format.
125+
126+
Args:
127+
annotations (list[list[float]]): List of annotations in any YOLO format.
128+
129+
Returns:
130+
list[list[float]]: List of annotations in any YOLO segmentation format.
131+
"""
132+
segmentation_data = []
133+
134+
for anno in annotations:
135+
# YOLO segmentation format
136+
if len(anno) > 5:
137+
segmentation_data.append(anno)
138+
continue
139+
140+
# YOLO detection format
141+
category_id, cx, cy, w, h = anno
142+
x1, y1, x2, y2 = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2
143+
segmentation_data.append([category_id, x1, y1, x2, y1, x2, y2, x1, y2])
144+
145+
return segmentation_data
146+
147+
119148
def tensorlize(data):
120149
try:
121150
img_paths, bboxes, img_ratios = zip(*data)

0 commit comments

Comments
 (0)