def merge_coco_annotations(existing_coco_annotations, new_coco_annotations):
    """ Merges the two given coco annotation dicts into one.

    The "images" and "annotations" sections are concatenated and respective ids are adjusted.

    :param existing_coco_annotations: A dict describing the first coco annotations.
    :param new_coco_annotations: A dict describing the second coco annotations.
    :return: A dict containing the merged coco annotations.
    """

    # Concatenate category sections
    for cat_dict in new_coco_annotations["categories"]:
        if cat_dict not in existing_coco_annotations["categories"]:
            existing_coco_annotations["categories"].append(cat_dict)

    # Concatenate images sections
    image_id_offset = max([image["id"] for image in existing_coco_annotations["images"]]) + 1
    for image in new_coco_annotations["images"]:
        image["id"] += image_id_offset
    existing_coco_annotations["images"].extend(new_coco_annotations["images"])

    # Concatenate annotations sections
    if len(existing_coco_annotations["annotations"]) > 0:
        annotation_id_offset = max([annotation["id"] for annotation in existing_coco_annotations["annotations"]]) + 1
    else:
        annotation_id_offset = 0
    for annotation in new_coco_annotations["annotations"]:
        annotation["id"] += annotation_id_offset
        annotation["image_id"] += image_id_offset
    existing_coco_annotations["annotations"].extend(new_coco_annotations["annotations"])

    return existing_coco_annotations, image_id_offset