Example #1
0
    def _append_bulk(self,
                     entity_id,
                     figures_json,
                     figures_keys,
                     key_id_map: KeyIdMap,
                     field_name=ApiField.ENTITY_ID):
        if len(figures_json) == 0:
            return

        for (batch_keys,
             batch_jsons) in zip(batched(figures_keys, batch_size=100),
                                 batched(figures_json, batch_size=100)):
            resp = self._api.post('figures.bulk.add', {
                field_name: entity_id,
                ApiField.FIGURES: batch_jsons
            })
            for key, resp_obj in zip(batch_keys, resp.json()):
                figure_id = resp_obj[ApiField.ID]
                key_id_map.add_figure(key, figure_id)
Example #2
0
    def from_json(cls,
                  data,
                  objects: VideoObjectCollection,
                  frame_index,
                  key_id_map: KeyIdMap = None):
        '''
        The function from_json convert VideoFigure from json format to VideoFigure class object.
        :param data: input VideoFigure in json format
        :param objects: VideoObjectCollection
        :param frame_index: int
        :param key_id_map: KeyIdMap class object
        :return: VideoFigure class object
        '''
        object_id = data.get(OBJECT_ID, None)
        object_key = None
        if OBJECT_KEY in data:
            object_key = uuid.UUID(data[OBJECT_KEY])

        if object_id is None and object_key is None:
            raise RuntimeError(
                "Figure can not be deserialized from json: object_id or object_key are not found"
            )

        if object_key is None:
            if key_id_map is None:
                raise RuntimeError(
                    "Figure can not be deserialized: key_id_map is None")
            object_key = key_id_map.get_object_key(object_id)
            if object_key is None:
                raise RuntimeError(
                    "Object with id={!r} not found in key_id_map".format(
                        object_id))

        object = objects.get(object_key)
        if object is None:
            raise RuntimeError(
                "Figure can not be deserialized: corresponding object {!r} not found in ObjectsCollection"
                .format(object_key.hex))

        shape_str = data[ApiField.GEOMETRY_TYPE]
        geometry_json = data[ApiField.GEOMETRY]

        shape = GET_GEOMETRY_FROM_STR(shape_str)
        geometry = shape.from_json(geometry_json)

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

        if key_id_map is not None:
            key_id_map.add_figure(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(object,
                   geometry,
                   frame_index,
                   key,
                   class_id=class_id,
                   labeler_login=labeler_login,
                   updated_at=updated_at,
                   created_at=created_at)