Ontology SDK Basics

An Ontology, or labeling protocol, defines the concepts, relationships, and representations in your data. Ontologies are essential for creating object and frame labels by defining what is being labeled.

Before you can apply Classifications to a Collection in Encord Active, the Classifications have to exist in an Ontology in Encord.

Encord supports Classifications with nested attributes up to 7 levels deep using Radio buttons (RadioAttribute). Checklists and text fields stop the nesting of attributes.


 - Classification
    - Radio button
      - Radio button
        - Radio button
          - Radio button
           - Radio button
             - Radio button
               - Radio button or Checklist or Text box

Annotation Ontologies

Ontology with Annotation: Creates an Ontology (title=“My test Ontology”) with a single Bounding Box Object (“Cute cat”) in the structure.

Workflow Project with Ontology and Annotations: Creates a Workflow Project (“Cats annotation project”) with a dataset (“MY_CAT_DATASET”) and an Ontology.


import logging
import time
from tqdm import tqdm
from encord import EncordUserClient
from encord.objects import Shape, Object, Option, OntologyStructure
from encord.objects.coordinates import BoundingBoxCoordinates
from encord.objects.attributes import RadioAttribute, ChecklistAttribute


user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path='/Users/encord/.ssh/id_ed25519'
    )


ontology_structure = OntologyStructure()
ontology_structure.add_object("Cute Cat", shape=Shape.BOUNDING_BOX)

cat_ontology = user_client.create_ontology(title="My test Ontology", structure=ontology_structure)

Classification Ontologies

Use the following example as a guide to creating your Ontology with Classifications using the SDK.

**Radio button: ** Creates an Ontology with 2 radio button lists.

Checklist: Creates an Ontology with 3 options in a checklist.

Text field: Creates an Ontology with a single text field.

Nested attributes: Creates an Ontology with 2 levels of nesting.


from encord import EncordUserClient
from encord.objects import Shape, Object, Option, OntologyStructure
from encord.objects.attributes import RadioAttribute, ChecklistAttribute


user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path='/Users/encord/.ssh/id_ed25519'
    )

# Create a new ontology with Classifications
ontology_structure = OntologyStructure()

# Adding a Classification of type radio button
classification = ontology_structure.add_classification()
cat_colour = classification.add_attribute(RadioAttribute, "colour", required=True)
cat_colour.add_option("white")
cat_colour.add_option("black")

# Adding a Classification of type check box
classification = ontology_structure.add_classification()
cat_features = classification.add_attribute(ChecklistAttribute, "features")
cat_features.add_option("soft")
cat_features.add_option("fluffy")
cat_features.add_option("friendly")

# Create a new ontology with Classifications
ontology = user_client.create_ontology("Radio Button Ontology", structure=ontology_structure)

Ontologies with Annotations and Classifications

This example explains how to create an Ontology with annotations and classifications.

Ensure you replace:

  • <file-path-to-ssh-private-key> with the pull path to your private key.
  • My test Ontology with the name of your Ontology.
  • Cute Cat with the name of your Ontology class.
  • As well as any other options relevant to your Ontology.
from encord import EncordUserClient
from encord.objects import Shape, Object, Option, OntologyStructure
from encord.objects.coordinates import BoundingBoxCoordinates
from encord.objects.attributes import RadioAttribute, ChecklistAttribute


user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path='<file-path-to-ssh-private-key>'
    )


ontology_structure = OntologyStructure()

# Adding bounding box annotation
ontology_structure.add_object("Cute Cat", shape=Shape.BOUNDING_BOX)


# Adding a Classification of type radio button
classification = ontology_structure.add_classification()
cat_colour = classification.add_attribute(RadioAttribute, "colour", required=True)
cat_colour.add_option("white")
cat_colour.add_option("black")

# Adding a Classification of type check box
classification = ontology_structure.add_classification()
cat_features = classification.add_attribute(ChecklistAttribute, "features")
cat_features.add_option("soft")
cat_features.add_option("fluffy")
cat_features.add_option("friendly")

cat_ontology = user_client.create_ontology(title="My test Ontology", structure=ontology_structure)