diff --git a/src/labelformat/formats/yolov8.py b/src/labelformat/formats/yolov8.py index da371f1..d1918e1 100644 --- a/src/labelformat/formats/yolov8.py +++ b/src/labelformat/formats/yolov8.py @@ -221,7 +221,7 @@ def get_labels(self) -> Iterable[ImageInstanceSegmentation]: ) continue with label_path.open() as file: - label_data = [line.split() for line in file.readlines()] + label_data = [line.split() for line in file if line.strip()] objects = [] for row in label_data: diff --git a/tests/unit/formats/test_yolov8.py b/tests/unit/formats/test_yolov8.py index 82d32dc..042f56e 100644 --- a/tests/unit/formats/test_yolov8.py +++ b/tests/unit/formats/test_yolov8.py @@ -377,6 +377,22 @@ def test_instance_segmentation__image_without_label_file( ] == expected +def test_yolov8_instance_segmentation_input__blank_lines(tmp_path: Path) -> None: + config_file = _make_dataset_with_background_image( + tmp_path=tmp_path, label_line="0 0.1 0.1 0.5 0.1 0.5 0.5\n\n \n" + ) + + labels = list( + YOLOv8InstanceSegmentationInput( + input_file=config_file, input_split="train" + ).get_labels() + ) + + assert [(label.image.filename, len(label.objects)) for label in labels] == [ + ("labeled.jpg", 1) + ] + + def _make_dataset_with_background_image(tmp_path: Path, label_line: str) -> Path: """Create a YOLOv8 dataset where only labeled.jpg has a label file.""" images_dir = tmp_path / "images"