Encord’s SDK supports importing labels/annotations for your data in the COCO format.

You can also import your COCO labels/annotations as predictions on your Active Projects.

This guide assumes your files were already imported into Encord and a Project containing the data has been created.
  1. Save import.py, replacing the following variables:
    • <private_key_path> with the full path to your private key.
    • <project_id> with the unique ID of the Project containing the data units you want to import labels for.
    • COCOimportfile.json with the full path of the COCO file containing the labels you want to import.
The COCO file MUST include the info field. If it is missing, add it as: "info": {},.
  1. If necessary, modify the matching logic that maps COCO image IDs to Encord frame indices. This is particularly important in cases where filenames in the COCO file do not directly match those in the Encord Project or when multiple files have the same name.

The following import.py script is configured to import labels into single images with unique names and assumes that the category names in the COCO file match the names of your Ontology objects.

In practice, you must implement your own matching logic. An example where filenames in the COCO file do not directly match those in the Encord Project is provided below.

import json
from pathlib import Path
from encord.utilities.coco.datastructure import FrameIndex
from encord import EncordUserClient
from encord.exceptions import OntologyError

# Replace with the path to your SSH private key
keyfile = "<private_key_path>"

# Authenticate with Encord using your SSH private key
user_client = EncordUserClient.create_with_ssh_private_key(ssh_private_key_path=keyfile)

# Replace with your Project ID
project = user_client.get_project("<project_id>")

# Load the COCO annotations JSON file
# Replace 'COCOimportfile.json' with the full path to your COCO file
coco_file = Path('COCOimportfile.json')
labels_dict = json.loads(coco_file.read_text())

# Build a mapping from COCO category IDs to the feature hashes in your Encord Ontology. 
category_id_to_feature_hash = {}
ont_struct = project.ontology_structure
for coco_category in labels_dict['categories']:
    try:
        ont_obj = ont_struct.get_child_by_title(coco_category['name'])
        category_id_to_feature_hash[coco_category['id']] = ont_obj.feature_node_hash
    except OntologyError:
        print(f'Could not match {coco_category["name"]} in the Ontology. Import will crash if these are present.')

# Build a mapping from COCO image IDs to Encord frame indices
# This is only applicable for images, image groups, image sequences, and DICOM series
image_id_to_frame_index = {}
data_title_to_label_row = {lr.data_title: lr for lr in project.list_label_rows_v2()}
for img in labels_dict['images']:
    if "video_title" in img.keys():
        lr = data_title_to_label_row[img["video_title"]]
        frame_num = int(img["file_name"].split('/')[-1].split(".")[0])
    else:
        lr = data_title_to_label_row[img['image_title']]
        frame_num = 0

    # Creates a mapping between the COCO image IDs and the corresponding frame indices in Encord
    # In this example, the target frame is 0 because the files in the sample project are single images
    image_id_to_frame_index[img['id']] = FrameIndex(lr.data_hash, frame=frame_num)

# Import the COCO labels into Encord
project.import_coco_labels(
    labels_dict,
    category_id_to_feature_hash,
    image_id_to_frame_index
)