Create an Ontology

ℹ️

Note

We recommend using the Encord platform to create Ontologies. The platform allows you to visualize how different elements of the Ontology are related to each other.

Ontologies are used to label your data and define how concepts are related to each other.

The following script creates an Ontology called "My test ontology" containing an object called Cute Cat, a radio button attribute for colour, and a checklist attribute for features. Examples for bounding boxes, rotatable bounding boxes, polygons, and bitmasks are provided.

The script outputs the hash of the Ontology. The hash is used for the creation of a Project in step 3.


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

from encord.http.constants import RequestsSettings

requests_settings = RequestsSettings(
    max_retries=10,
    read_timeout=1800,
    write_timeout=1800,
)

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

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("My test ontology", structure=ontology_structure)

# Prints the ontology hash
print (cat_ontology.ontology_hash)


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

from encord.http.constants import RequestsSettings

requests_settings = RequestsSettings(
    max_retries=10,
    read_timeout=1800,
    write_timeout=1800,
)

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

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("My test ontology", structure=ontology_structure)

# Prints the ontology hash
print (cat_ontology.ontology_hash)


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

from encord.http.constants import RequestsSettings

requests_settings = RequestsSettings(
    max_retries=10,
    read_timeout=1800,
    write_timeout=1800,
)

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

ontology_structure = OntologyStructure()

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


# 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("My test ontology", structure=ontology_structure)

# Prints the ontology hash
print (cat_ontology.ontology_hash)


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

from encord.http.constants import RequestsSettings

requests_settings = RequestsSettings(
    max_retries=10,
    read_timeout=1800,
    write_timeout=1800,
)

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

ontology_structure = OntologyStructure()

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


# 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("My test ontology", structure=ontology_structure)

# Prints the ontology hash
print (cat_ontology.ontology_hash)