def _obj_id_to_idx(inference_state, obj_id):
    """Map client-side object id to model-side object index."""
    obj_idx = inference_state["obj_id_to_idx"].get(obj_id, None)
    if obj_idx is not None:
        return obj_idx

    # This is a new object id not sent to the server before. We only allow adding
    # new objects *before* the tracking starts.
    allow_new_object = not inference_state["tracking_has_started"]
    if allow_new_object:
        # get the next object slot
        obj_idx = len(inference_state["obj_id_to_idx"])
        inference_state["obj_id_to_idx"][obj_id] = obj_idx
        inference_state["obj_idx_to_id"][obj_idx] = obj_id
        inference_state["obj_ids"] = list(inference_state["obj_id_to_idx"])
        # set up input and output structures for this object
        inference_state["point_inputs_per_obj"][obj_idx] = {}
        inference_state["mask_inputs_per_obj"][obj_idx] = {}
        inference_state["output_dict_per_obj"][obj_idx] = {
            "cond_frame_outputs": {},  # dict containing {frame_idx: <out>}
            "non_cond_frame_outputs": {},  # dict containing {frame_idx: <out>}
        }
        inference_state["temp_output_dict_per_obj"][obj_idx] = {
            "cond_frame_outputs": {},  # dict containing {frame_idx: <out>}
            "non_cond_frame_outputs": {},  # dict containing {frame_idx: <out>}
        }
        return obj_idx
    else:
        raise RuntimeError(
            f"Cannot add new object id {obj_id} after tracking starts. "
            f"All existing object ids: {inference_state['obj_ids']}. "
            f"Please call 'reset_state' to restart from scratch."
        )
Map client-side object id to model-side object index.
projects/llava_sam2/hf/models/sam2.py