supervision icon indicating copy to clipboard operation
supervision copied to clipboard

[Bug]: as_pascal_voc adds 1 to xmax and ymax; the VOC devkit needs the +1 on xmin and ymin only (complements #2747)

Open t7spotter opened this issue 1 month ago • 0 comments

Search before asking

  • [x] I have searched the issues and discussions and found no similar bug report.

Bug

#2747 (with PR #2748) covers the int() truncation in the same function; this issue is about the remaining part, the +1 on xmax and ymax, which #2748 keeps.

dataset/formats/pascal_voc.py (line 72 in 0.30.9 and 0.31.0) adds 1 to all four coordinates before writing, citing #144 ("Pascal VOC coordinates are 1-indexed"). In the VOC devkit convention, pixel indices are 1-based and inclusive: a box covering 0-based pixels x0 .. x0+w-1 is written xmin = x0 + 1, xmax = x0 + w, so a box with xmin=1, xmax=W covers the whole image. Only xmin and ymin need the +1. Adding it to xmax and ymax makes every exported box one pixel wider and taller for readers that follow the devkit, such as Detectron2's pascal_voc.py loader, which subtracts 1 from xmin and ymin only.

supervision's reader subtracts 1 from all four coordinates (xyxy_arr -= 1), so a supervision -> supervision round trip looks exact, and the problem only appears when another tool reads the files. Reader and writer would need to change together.

This also affects #2747: with PR #2748, the 0-based box [0, 0, 37, 29] is written as 1 1 38 30, but its devkit encoding is 1 1 37 29. Integer boxes, which rounding does not change, are all one pixel too large on the right and bottom.

In our audit of COCO val2017 read with the devkit convention, 30% of the boxes exported by as_pascal_voc had IoU below 0.9 with their source box, most of them small objects; both the truncation and the +1 on xmax/ymax contribute, and #2748 removes only the first.

Output of the example below: <xmin>11</xmin> <ymin>21</ymin> <xmax>41</xmax> <ymax>61</ymax>. The devkit encoding of COCO box x=10, y=20, w=30, h=40 is 11, 21, 40, 60.

Expected: add 1 to xmin and ymin only when writing, and subtract 1 from xmin and ymin only when reading. Since #2748 is already changing this function, it may be simplest to fold the change into it. If the current behaviour is kept for compatibility with existing supervision files, a note in the docs would help.

Found while auditing COCO converters for a study of annotation-format conversion (preprint to follow). Happy to open a PR.

Environment

Reproduced on supervision 0.30.9 and 0.31.0 (latest), Python 3.13, Linux.

Minimal Reproducible Example

import json, tempfile
from pathlib import Path
import numpy as np, cv2
import supervision as sv

tmp = Path(tempfile.mkdtemp()); (tmp / "images").mkdir()
cv2.imwrite(str(tmp / "images" / "a.jpg"), np.zeros((100, 100, 3), np.uint8))
coco = {"images": [{"id": 1, "file_name": "a.jpg", "width": 100, "height": 100}],
        "categories": [{"id": 1, "name": "box"}],
        "annotations": [{"id": 1, "image_id": 1, "category_id": 1, "bbox": [10, 20, 30, 40], "area": 1200, "iscrowd": 0}]}
(tmp / "ann.json").write_text(json.dumps(coco))
ds = sv.DetectionDataset.from_coco(str(tmp / "images"), str(tmp / "ann.json"))
ds.as_pascal_voc(annotations_directory_path=str(tmp / "voc"))
print((tmp / "voc" / "a.xml").read_text())

Are you willing to submit a PR?

  • [ ] Yes I'd like to help by submitting a PR!

t7spotter avatar Oct 17 '26 15:10 t7spotter