|
1 | 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
|
2 |
| -import io |
3 |
| -import logging |
4 | 2 | import contextlib
|
5 |
| -import os |
6 | 3 | import datetime
|
| 4 | +import io |
7 | 5 | import json
|
| 6 | +import logging |
8 | 7 | 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 |
14 | 10 | from fvcore.common.file_io import PathManager, file_lock
|
| 11 | +from fvcore.common.timer import Timer |
| 12 | +from PIL import Image |
15 | 13 |
|
| 14 | +from detectron2.structures import Boxes, BoxMode, PolygonMasks |
16 | 15 |
|
17 |
| -from .. import MetadataCatalog, DatasetCatalog |
| 16 | +from .. import DatasetCatalog, MetadataCatalog |
18 | 17 |
|
19 | 18 | """
|
20 | 19 | 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):
|
333 | 332 | # Computing areas for instances by counting the pixels
|
334 | 333 | segmentation = annotation["segmentation"]
|
335 | 334 | # 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)}!") |
338 | 342 | else:
|
339 | 343 | # Computing areas using bounding boxes
|
340 | 344 | bbox_xy = BoxMode.convert(bbox, BoxMode.XYWH_ABS, BoxMode.XYXY_ABS)
|
|
0 commit comments