Exemplo n.º 1
0
 def json_to_value(mcs, json: collections.Mapping):
     key = json.get('__type__', None)
     value = json.get('value', None)
     if key is None:
         raise ValueError(f"Can not convert with missing type key!")
     if value is None:
         raise ValueError(f"Can not convert with missing value!")
     converter_cls = mcs.__key_to_converter__.get(key, None)
     if converter_cls is None:
         raise ValueError(f"Can not convert object of type key {key}!")
     return converter_cls.json_to_value(value)
Exemplo n.º 2
0
def extract_labels_from_mask(mask: np.ndarray, color_id_to_obj_class: collections.Mapping) -> list:
    """
    Extract multiclass instances from grayscale mask and save it to labels list.
    Args:
        mask: multiclass grayscale mask
        color_id_to_obj_class: dict of objects classes assigned to color id (e.g. {1: ObjClass('cat), ...})
    Returns:
        list of labels with bitmap geometry
    """
    zero_offset = 1 if 0 in color_id_to_obj_class else 0
    if zero_offset > 0:
        mask = mask + zero_offset

    labeled, labels_count = measure.label(mask, connectivity=1, return_num=True)
    objects_slices = ndimage.find_objects(labeled)
    labels = []

    for object_index, slices in enumerate(objects_slices, start=1):
        crop = mask[slices]
        sub_mask = crop * (labeled[slices] == object_index).astype(np.int)

        class_index = np.max(sub_mask) - zero_offset

        if class_index in color_id_to_obj_class:
            bitmap = Bitmap(data=sub_mask.astype(np.bool), origin=PointLocation(slices[0].start, slices[1].start))
            label = Label(geometry=bitmap, obj_class=color_id_to_obj_class.get(class_index))
            labels.append(label)
    return labels
Exemplo n.º 3
0
def _get_nested(value: collections.Mapping, keys: deque, default=None):
    if len(keys) > 0:
        attr = keys.popleft()
        value = value.get(attr)
        if len(keys) > 0 and value is not None:
            if isinstance(value, collections.Mapping):
                return _get_nested(value, keys, default)
            if isinstance(value, object):
                return _get_nested(vars(value), keys, default)
    return value or default
Exemplo n.º 4
0
 def prepare_field(self, field_name: str, values: collections.Mapping):
     return values.get(field_name)