Esempio n. 1
0
def set_instance_color(label: Label, category_id: int,
                       ann_id: int) -> NDArrayU8:
    """Set the color for an instance given its attributes and ID."""
    attributes = label.attributes
    if attributes is None:
        truncated, occluded, crowd, ignored = 0, 0, 0, 0
    else:
        truncated = int(attributes.get("truncated", False))
        occluded = int(attributes.get("occluded", False))
        crowd = int(check_crowd(label))
        ignored = int(check_ignored(label))
    color: NDArrayU8 = np.array(
        [
            category_id & 255,
            (truncated << 3) + (occluded << 2) + (crowd << 1) + ignored,
            ann_id >> 8,
            ann_id & 255,
        ],
        dtype=np.uint8,
    )
    return color
Esempio n. 2
0
def parse_seg_objects(
    objects: List[Label],
    classes: List[Category],
    ignore_unknown_cats: bool = False,
    image_size: Optional[ImageSize] = None,
) -> Tuple[List[RLEDict], NDArrayI32, NDArrayI32, List[RLEDict]]:
    """Parse segmentation objects under Scalabel formats."""
    rles, labels, ids, ignore_rles = [], [], [], []
    class_names = [category.name for category in classes]
    for obj in objects:
        if obj.rle is not None:
            rle = obj.rle
        elif obj.poly2d is not None:
            assert (
                image_size
                is not None), "Requires ImageSize for Poly2D conversion to RLE"
            rle = mask_to_rle(poly2ds_to_mask(image_size, obj.poly2d))
        else:
            continue
        category = obj.category
        if category in class_names:
            if check_crowd(obj) or check_ignored(obj):
                ignore_rles.append(rle)
            else:
                rles.append(rle)
                labels.append(class_names.index(category))
                ids.append(int(obj.id))
        else:
            if not ignore_unknown_cats:
                raise KeyError(f"Unknown category: {category}")
    if any(category.isThing is not None and not category.isThing
           for category in classes):
        rles, labels, ids = combine_stuff_masks(rles, labels, ids, classes)
    rles_dict = [rle.dict() for rle in rles]
    ignore_rles_dict = [rle.dict() for rle in ignore_rles]
    labels_arr: NDArrayI32 = np.array(labels, dtype=np.int32)
    ids_arr: NDArrayI32 = np.array(ids, dtype=np.int32)
    return (rles_dict, labels_arr, ids_arr, ignore_rles_dict)
Esempio n. 3
0
def bdd100k2coco_seg_track(mask_base: str,
                           frames: List[Frame],
                           config: Config,
                           nproc: int = NPROC) -> GtType:
    """Converting BDD100K Segmentation Tracking Set to COCO format."""
    video_id, image_id, ann_id = 0, 0, 0
    img_shape = config.imageSize
    frames_list = group_and_sort(frames)
    videos: List[VidType] = []
    images: List[ImgType] = []

    mask_names: List[str] = []
    category_ids_list: List[List[int]] = []
    instance_ids_list: List[List[int]] = []
    annotations_list: List[List[AnnType]] = []

    categories = get_leaf_categories(config.categories)
    cat_name2id = {cat.name: i + 1 for i, cat in enumerate(categories)}

    logger.info("Collecting annotations...")

    for video_anns in tqdm(frames_list):
        global_instance_id: int = 1
        instance_id_maps: Dict[str, int] = {}

        video_name = video_anns[0].videoName
        video_id += 1
        video = VidType(id=video_id, name=video_name)
        videos.append(video)

        for image_anns in video_anns:
            image_id += 1
            if img_shape is None:
                if image_anns.size is not None:
                    img_shape = image_anns.size
                else:
                    raise ValueError("Image shape not defined!")

            image = ImgType(
                video_id=video_id,
                frame_id=image_anns.frameIndex,
                id=image_id,
                file_name=os.path.join(video_name, image_anns.name),
                height=img_shape.height,
                width=img_shape.width,
            )
            if image_anns.url is not None:
                image["coco_url"] = image_anns.url
            images.append(image)

            mask_name = os.path.join(
                mask_base,
                video_name,
                # Bitmask in .png format, image in .jpg format
                image_anns.name.replace(".jpg", ".png"),
            )
            mask_names.append(mask_name)

            category_ids: List[int] = []
            instance_ids: List[int] = []
            annotations: List[AnnType] = []

            for label in image_anns.labels:
                if label.poly2d is None:
                    continue
                if label.category not in cat_name2id:
                    continue

                ann_id += 1
                instance_id, global_instance_id = get_bdd100k_instance_id(
                    instance_id_maps, global_instance_id, label.id)
                category_id = cat_name2id[label.category]
                annotation = AnnType(
                    id=ann_id,
                    image_id=image_id,
                    instance_id=instance_id,
                    category_id=category_id,
                    scalabel_id=label.id,
                    iscrowd=int(check_crowd(label) or check_ignored(label)),
                    ignore=0,
                )

                category_ids.append(category_id)
                instance_ids.append(instance_id)
                annotations.append(annotation)

            category_ids_list.append(category_ids)
            instance_ids_list.append(instance_ids)
            annotations_list.append(annotations)

    annotations = bitmask2coco_with_ids_parallel(
        annotations_list,
        mask_names,
        category_ids_list,
        instance_ids_list,
        nproc,
    )

    return GtType(
        type="instances",
        categories=get_coco_categories(config),
        videos=videos,
        images=images,
        annotations=annotations,
    )
Esempio n. 4
0
def check_bdd100k_crowd(label: Label) -> bool:
    """Check crowd attribute for BDD100K."""
    if label.id == "-1":
        return True
    return check_crowd(label)