supervision
supervision copied to clipboard
[Bug]: DetectionDataset.as_yolo / as_pascal_voc / as_labelme write a mask with a hole as two instances
Search before asking
- [x] I have searched the issues and discussions and found no similar bug report.
Bug
What's the bug? A mask that contains a hole (for example a ring, or any SAM mask with an interior gap) is exported as two instances of the same class: one for the outer contour and one for the hole. sv.mask_to_polygons uses cv2 RETR_TREE and discards the hierarchy, so hole contours come back as ordinary polygons, and approximate_mask_with_polygons passes them straight to the writers.
Expected behavior: One instance per mask. as_coco already handles this (it uses RLE for masks with holes).
Actual behavior: as_yolo writes two label lines, as_pascal_voc two
Error/Traceback (if any):
# No error: the export succeeds but writes the wrong labels.
Environment
- Supervision: 0.31.0.dev0 (develop @ 69cb843)
- Python: 3.11
- OS: macOS 15 (arm64)
Minimal Reproducible Example
import tempfile
from pathlib import Path
import numpy as np
from PIL import Image
import supervision as sv
m = np.zeros((100, 150), bool)
m[10:60, 10:60] = True
m[25:45, 25:45] = False # a hole; mask area is 2100
root = Path(tempfile.mkdtemp())
(root / "images").mkdir()
p = str(root / "images" / "a.png")
Image.fromarray(np.zeros((100, 150, 3), np.uint8)).save(p)
det = sv.Detections(xyxy=np.array([[10, 10, 59, 59]], float), mask=m[None], class_id=np.array([0]))
ds = sv.DetectionDataset(classes=["ring"], images=[p], annotations={p: det})
ds.as_yolo(str(root / "y/images"), str(root / "y/labels"), str(root / "y/data.yaml"))
print(len((root / "y/labels/a.txt").read_text().splitlines())) # 2, expected 1
r = sv.DetectionDataset.from_yolo(
str(root / "y/images"), str(root / "y/labels"), str(root / "y/data.yaml"), force_masks=True
)
d = next(iter(r.annotations.values()))
print(len(d), [int(x.sum()) for x in d.mask]) # 2 [2500, 480], expected 1 [2100]
Are you willing to submit a PR?
- [x] Yes I'd like to help by submitting a PR!