diff --git a/darwin/importer/importer.py b/darwin/importer/importer.py index d2f02f807..d535a6b8a 100644 --- a/darwin/importer/importer.py +++ b/darwin/importer/importer.py @@ -743,6 +743,8 @@ def _import_annotations( errors: dt.ErrorList = [] success: dt.Success = dt.Success.SUCCESS + rl: Optional[dt.Annotation] = None + rl_dense_rle_ids: Optional[Set[str]] = None serialized_annotations = [] for annotation in annotations: annotation_class = annotation.annotation_class @@ -771,6 +773,33 @@ def _import_annotations( data = _get_annotation_data(annotation, annotation_class_id, attributes) + # check if the mask is empty (i.e. masks that do not have a corresponding raster layer) if so, skip import of the mask + if annotation_type == "mask": + if rl is None: + rl = next( + ( + a + for a in annotations + if a.annotation_class.annotation_type == "raster_layer" + ), + None, + ) + if rl and rl_dense_rle_ids is None: + rl_dense_rle_ids = set(rl.data["dense_rle"][::2]) + if rl is not None and rl_dense_rle_ids is not None: + # check if the 'annotation_class_id' is in raster_layer's mask_annotation_ids_mapping dict + _annotation_id = annotation.id + if ( + rl.data["mask_annotation_ids_mapping"][_annotation_id] + not in rl_dense_rle_ids + ): + # skip import of the mask, and remove it from mask_annotation_ids_mapping + logger.warning( + f"Skipping import of mask annotation '{annotation_class.name}' as it does not have a corresponding raster layer" + ) + del rl.data["mask_annotation_ids_mapping"][_annotation_id] + continue + actors: List[dt.DictFreeForm] = [] actors.extend(_handle_annotators(annotation, import_annotators)) actors.extend(_handle_reviewers(annotation, import_reviewers))