Skip to content

Commit 6901cc7

Browse files
ppwwyyxxfacebook-github-bot
authored andcommitted
support RLE in convert_to_coco_dict
Summary: fix facebookresearch#200 Pull Request resolved: fairinternal/detectron2#391 Reviewed By: rbgirshick Differential Revision: D20260189 Pulled By: ppwwyyxx fbshipit-source-id: 690b497e45dda9a3dc9696dc991aac7bf482de0b
1 parent 5e2a1ec commit 6901cc7

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

detectron2/data/datasets/coco.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
2-
import io
3-
import logging
42
import contextlib
5-
import os
63
import datetime
4+
import io
75
import json
6+
import logging
87
import numpy as np
9-
10-
from PIL import Image
11-
12-
from fvcore.common.timer import Timer
13-
from detectron2.structures import BoxMode, PolygonMasks, Boxes
8+
import os
9+
import pycocotools.mask as mask_util
1410
from fvcore.common.file_io import PathManager, file_lock
11+
from fvcore.common.timer import Timer
12+
from PIL import Image
1513

14+
from detectron2.structures import Boxes, BoxMode, PolygonMasks
1615

17-
from .. import MetadataCatalog, DatasetCatalog
16+
from .. import DatasetCatalog, MetadataCatalog
1817

1918
"""
2019
This file contains functions to parse COCO-format annotations into dicts in "Detectron2 format".
@@ -333,8 +332,13 @@ def convert_to_coco_dict(dataset_name):
333332
# Computing areas for instances by counting the pixels
334333
segmentation = annotation["segmentation"]
335334
# TODO: check segmentation type: RLE, BinaryMask or Polygon
336-
polygons = PolygonMasks([segmentation])
337-
area = polygons.area()[0].item()
335+
if isinstance(segmentation, list):
336+
polygons = PolygonMasks([segmentation])
337+
area = polygons.area()[0].item()
338+
elif isinstance(segmentation, dict): # RLE
339+
area = mask_util.area(segmentation)
340+
else:
341+
raise TypeError(f"Unknown segmentation type {type(segmentation)}!")
338342
else:
339343
# Computing areas using bounding boxes
340344
bbox_xy = BoxMode.convert(bbox, BoxMode.XYWH_ABS, BoxMode.XYXY_ABS)

0 commit comments

Comments
 (0)