def _convert_segmentation(segmentation, mask_targets, mask_types, converter):
    """Convert segmentation to a collection of detections, polylines, etc.

    `converter(label_mask, label, label_type, offset, frame_size)` is
    a function that returns a list of detections, polylines, etc. It
    gets called for each value in `mask_targets`, or for all values in
    the mask if `mask_targets` is `None`.
    """
    if isinstance(mask_types, dict):
        default = None
    else:
        default = mask_types
        mask_types = {}

    mask = segmentation.get_mask()
    is_rgb = mask.ndim == 3

    if is_rgb:
        # convert to int, like in transform_mask
        mask = _rgb_array_to_int(mask)
        if mask_targets is not None:
            mask_targets = {_hex_to_int(k): v for k, v in mask_targets.items()}

    mask = mask.squeeze()
    if mask.ndim != 2:
        raise ValueError(f"Unsupported mask dimensions: {mask.ndim}")

    objects = _find_slices(mask)
    results = []
    for target, slices in objects.items():
        if mask_targets is not None:
            label = mask_targets.get(target, None)

            if label is None:
                continue  # skip unknown target
        else:
            label = str(target)

        label_type = mask_types.get(target, None)

        if label_type is None:
            if default is None:
                continue  # skip unknown type

            label_type = default

        label_mask = mask[slices] == target
        offset = list(s.start for s in slices)[::-1]
        frame_size = mask.shape[:2][::-1]

        new_results = converter(
            label_mask, label, label_type, offset, frame_size
        )
        results.extend(new_results)

    return results
Convert segmentation to a collection of detections, polylines, etc.

    `converter(label_mask, label, label_type, offset, frame_size)` is
    a function that returns a list of detections, polylines, etc. It
    gets called for each value in `mask_targets`, or for all values in
    the mask if `mask_targets` is `None`.
    
fiftyone/core/labels.py