> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encord.com/llms.txt
> Use this file to discover all available pages before exploring further.

# COCO Labels/Annotations

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](/sdk-documentation/api-active/active-api-import-model-predictions-cloud).

<Note>This guide assumes your files were already [imported into Encord](/sdk-documentation/getting-started-sdk/sdk-register-data-aws) and a [Project containing the data has been created](/sdk-documentation/getting-started-sdk/sdk-get-started-ontology).</Note>

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.

<Info>The COCO file MUST include the `info` field. If it is missing, add it as: `"info": {},`.</Info>

2. 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.

<CodeGroup>
  ```python import.py theme={"dark"}
  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
  )
  ```

  ```json COCO example Bounding Box theme={"dark"}
  {
    "info": {},
    "images": [
      {
        "id": 1,
        "file_name": "cutekitty.jpg",
        "height": 480,
        "width": 640
      },
      {
        "id": 2,
        "file_name": "cutepuppy.jpg",
        "height": 480,
        "width": 640
      }
    ],
    "annotations": [
      {
        "id": 1,
        "image_id": 1,
        "category_id": 1,
        "bbox": [246, 251, 153, 159],
        "area": 1234.56,
        "iscrowd": false
      },
      {
        "id": 2,
        "image_id": 2,
        "category_id": 2,
        "bbox": [220, 236, 76, 64],
        "area": 5678.90,
        "iscrowd": true
      }
    ],
    "categories": [
      {
        "id": 1,
        "name": "Cat",
        "supercategory": "bounding_box"
      },
      {
        "id": 2,
        "name": "Dog",
        "supercategory": "bounding_box"
      }
    ]
  }
  ```

  ```json COCO example Polygon theme={"dark"}
  {
    "info": {},
    "images": [
      {
        "id": 1,
        "file_name": "cutekitty.jpg",
        "height": 480,
        "width": 640
      },
      {
        "id": 2,
        "file_name": "cutepuppy.jpg",
        "height": 480,
        "width": 640
      }
    ],
    "annotations": [
      {
        "id": 1,
        "image_id": 1,
        "category_id": 1,
        "segmentation": [508, 559, 512, 555, 517, 554, 520, 548, 527, 542, 531, 540, 538, 540, 553, 562],
        "area": 1234.56,
        "iscrowd": false
      },
      {
        "id": 2,
        "image_id": 2,
        "category_id": 2,
        "segmentation": [508, 559, 512, 555, 517, 554, 520, 548, 527, 542, 531, 540, 538, 540, 553, 562],
        "area": 5678.90,
        "iscrowd": true
      }
    ],
    "categories": [
      {
        "id": 1,
        "name": "Cat",
        "supercategory": "polygon"
      },
      {
        "id": 2,
        "name": "Dog",
        "supercategory": "polygon"
      }
    ]
  }
  ```

  ```python Filename mismatch theme={"dark"}
  data_title_to_label_row = {lr.data_title: lr for lr in project.list_label_rows_v2()}
  for img in labels_dict['images']:
      fname = img['file_name']
      stem = fname.split('/')[0]  # Adjust based on your filename structure
      assert stem in data_title_to_label_row, f'Problem matching {stem}'
      lr = data_title_to_label_row[stem]
      image_id_to_frame_index[img['id']] = FrameIndex(lr.data_hash, frame=0)
  ```
</CodeGroup>
