def bitmask2coco_with_ids( annotations: List[AnnType], mask_name: str, category_ids: List[int], instance_ids: List[int], ) -> List[AnnType]: """Convert bitmasks annotations of an image to RLEs or polygons.""" bitmask: NDArrayI32 = np.asarray(Image.open(mask_name), dtype=np.int32) category_map = bitmask[..., 0] instance_map = (bitmask[..., 2] << 2) + bitmask[..., 3] for annotation, category_id, instance_id in zip(annotations, category_ids, instance_ids): mask = np.logical_and(category_map == category_id, instance_map == instance_id) annotation = set_seg_object_geometry(annotation, mask) annotations = [ ann for ann in annotations if "bbox" in ann and "segmentation" in ann ] return annotations
def bitmask2coco_wo_ids(image_id: int, mask_name: str, mask_mode: str = "rle") -> List[AnnType]: """Convert bitmasks annotations of an image to RLEs or polygons.""" instances: List[InstanceType] = bitmasks_loader(mask_name) annotations: List[AnnType] = [] for instance in instances: annotation = AnnType( id=0, # set further image_id=image_id, category_id=instance["category_id"], instance_id=instance["instance_id"], iscrowd=instance["crowd"], ignore=instance["ignore"], ) annotation = set_seg_object_geometry(annotation, instance["mask"], mask_mode) annotations.append(annotation) return annotations
def bitmask2coco_wo_ids(image: ImgType, mask_base: str) -> List[AnnType]: """Convert bitmasks annotations of an image to RLEs or polygons.""" mask_name = os.path.join(mask_base, image["file_name"]) instances, img_shape = bitmasks_loader(mask_name) image["height"] = img_shape.height image["width"] = img_shape.width annotations: List[AnnType] = [] for instance in instances: annotation = AnnType( id=0, # set further image_id=image["id"], category_id=instance["category_id"], instance_id=instance["instance_id"], iscrowd=instance["crowd"], ) annotation = set_seg_object_geometry(annotation, instance["mask"]) annotations.append(annotation) return annotations