def _parse_labelmap_proto(labelmap):
    """Converts a labelmap proto into a category index.

    Adapted from `tensorflow/models/research/object_detection/utils/label_map_util.py`,
    which was necessary to properly include `id == 0` in the index, if present.

    Args:
        a StringIntLabelMapProto

    Returns:
        a dictionary mapping class IDs to class names
    """
    category_index = {}
    for item in labelmap.item:
        if item.HasField("display_name"):
            name = item.display_name
        else:
            name = item.name

        category_index[item.id] = name

    return category_index