Exemple #1
0
    def from_json(cls, data, project_meta: ProjectMeta):
        '''
        The function from_json convert Label from json format to Label class object. If there is no ObjClass from
        input json format in ProjectMeta, it generate RuntimeError error.
        :param data: input label in json format
        :param project_meta: ProjectMeta class object
        :return: Label class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a Label object from JSON: label class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        if obj_class.geometry_type is AnyGeometry:
            geometry_type_actual = GET_GEOMETRY_FROM_STR(
                data[GEOMETRY_TYPE] if GEOMETRY_TYPE in
                data else data[GEOMETRY_SHAPE])
            geometry = geometry_type_actual.from_json(data)
        else:
            geometry = obj_class.geometry_type.from_json(data)

        return cls(geometry=geometry,
                   obj_class=obj_class,
                   tags=TagCollection.from_json(data[LabelJsonFields.TAGS],
                                                project_meta.tag_metas),
                   description=data.get(LabelJsonFields.DESCRIPTION, ""))
    def from_json(cls,
                  data,
                  project_meta: ProjectMeta,
                  key_id_map: KeyIdMap = None):
        '''
        The function from_json convert PointcloudObject from json format to PointcloudObject class object. Raise error if object class name is not found in the given project meta
        :param data: input PointcloudObject in json format
        :param project_meta: ProjectMeta class object
        :param key_id_map: KeyIdMap class object
        :return: PointcloudObject class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a object from JSON: class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        object_id = data.get(ID, None)

        existing_key = None
        if object_id is not None and key_id_map is not None:
            existing_key = key_id_map.get_object_key(object_id)
        json_key = uuid.UUID(data[KEY]) if KEY in data else None
        if (existing_key
                is not None) and (json_key
                                  is not None) and (existing_key != json_key):
            raise RuntimeError(
                "Object id = {!r}: existing_key {!r} != json_key {!r}".format(
                    object_id, existing_key, json_key))

        if existing_key is not None:
            key = existing_key
        elif json_key is not None:
            key = json_key
        else:
            key = uuid.uuid4()

        if key_id_map is not None and existing_key is None:
            key_id_map.add_object(key, object_id)

        class_id = data.get(CLASS_ID, None)
        labeler_login = data.get(LABELER_LOGIN, None)
        updated_at = data.get(UPDATED_AT, None)
        created_at = data.get(CREATED_AT, None)

        return cls(obj_class=obj_class,
                   key=key,
                   tags=VideoTagCollection.from_json(
                       data[LabelJsonFields.TAGS], project_meta.tag_metas),
                   class_id=class_id,
                   labeler_login=labeler_login,
                   updated_at=updated_at,
                   created_at=created_at)
    def from_imgaug(cls,
                    img,
                    ia_boxes=None,
                    ia_masks=None,
                    index_to_class=None,
                    meta: ProjectMeta = None):
        if ((ia_boxes is not None) or (ia_masks is not None)) and meta is None:
            raise ValueError("Project meta has to be provided")

        labels = []
        if ia_boxes is not None:
            for ia_box in ia_boxes:
                obj_class = meta.get_obj_class(ia_box.label)
                if obj_class is None:
                    raise KeyError(
                        "Class {!r} not found in project meta".format(
                            ia_box.label))
                lbl = Label(
                    Rectangle(top=ia_box.y1,
                              left=ia_box.x1,
                              bottom=ia_box.y2,
                              right=ia_box.x2), obj_class)
                labels.append(lbl)

        if ia_masks is not None:
            if index_to_class is None:
                raise ValueError(
                    "mapping from index to class name is needed to transform masks to SLY format"
                )
            class_mask = ia_masks.get_arr()
            # mask = white_mask == 255
            (unique, counts) = np.unique(class_mask, return_counts=True)
            for index, count in zip(unique, counts):
                if index == 0:
                    continue
                mask = class_mask == index
                bitmap = Bitmap(data=mask[:, :, 0])
                restore_class = meta.get_obj_class(index_to_class[index])
                labels.append(Label(geometry=bitmap, obj_class=restore_class))

        return cls(img_size=img.shape[:2], labels=labels)
    def from_json(cls,
                  data,
                  project_meta: ProjectMeta,
                  key_id_map: KeyIdMap = None):
        '''
        The function from_json convert VideoObject from json format to VideoObject class object. Raise error if object class name is not found in the given project meta
        :param data: input VideoObject in json format
        :param project_meta: ProjectMeta class object
        :param key_id_map: KeyIdMap class object
        :return: VideoObject class object
        '''
        obj_class_name = data[LabelJsonFields.OBJ_CLASS_NAME]
        obj_class = project_meta.get_obj_class(obj_class_name)
        if obj_class is None:
            raise RuntimeError(
                f'Failed to deserialize a object from JSON: class name {obj_class_name!r} '
                f'was not found in the given project meta.')

        key = uuid.UUID(data[KEY]) if KEY in data else uuid.uuid4()

        if key_id_map is not None:
            key_id_map.add_object(key, data.get(ID, None))

        class_id = data.get(CLASS_ID, None)
        labeler_login = data.get(LABELER_LOGIN, None)
        updated_at = data.get(UPDATED_AT, None)
        created_at = data.get(CREATED_AT, None)

        return cls(obj_class=obj_class,
                   key=key,
                   tags=VideoTagCollection.from_json(
                       data[LabelJsonFields.TAGS], project_meta.tag_metas),
                   class_id=class_id,
                   labeler_login=labeler_login,
                   updated_at=updated_at,
                   created_at=created_at)