Skip to content

Wrong COCO format while exporting a sv.DetectionDataset using its as_coco method #285

@rizavelioglu

Description

@rizavelioglu

The Issue:

I export some annotations using:

sv.DetectionDataset(
   classes=CLASSES,
   images=images,
   annotations=annotations
).as_coco(
   annotations_path=ANNOTATIONS_PATH,
   min_image_area_percentage=0,
   max_image_area_percentage=1.,
   approximation_percentage=0
)

The resulting .json is created successfully.
Then, loading the annotations using pycocotools and plotting annotations of an image:

from pycocotools.coco import COCO
coco=COCO(ANNOTATIONS_PATH)

imgId = 42  # an arbitrary image id
file_name = coco.loadImgs(imgId)[0]["file_name"]

img = plt.imread(file_name)
plt.imshow(img)
plt.axis("off")

annIds = coco.getAnnIds(imgIds=imgId, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns, draw_bbox=True)

which plots wrong annotations for the given imgId: the annotations (box & mask) is seen bottom-right and it seems that there is a mismatch between the image and its annotations.
image

Actually, for any imgId, we have the same issue: the image is shown correctly but the annotations are not. Interestingly, the annotations (annIds) are always the same for different imgId's. In my case, there is only 1 box & mask per image and for various values of imgId, the annIds are always equal to 1.

The Cause:

The annIds are not unique among all the annotations (the "id" in "annotations" in COCO format).
I presume they have to be unique among all annotation id's (see the following issue).

However, the current implementation resets the annotation id to 1 for each image:

image_id, annotation_id = 1, 1

The (possible) Fix:

Simply changing label_id to annotation_id in the following line will assign a unique id for each annotation:

coco_annotation, label_id = detections_to_coco_annotations(

I would be glad to open a PR.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingversion 0.14.0Feature to be added in `0.14.0` release

Type

No type

Projects

Status

Current Release: Done

Relationships

None yet

Development

No branches or pull requests

Issue actions