View Ontology guide page for Ontology schema definitions and details.

Create an ontology

Create from new feature objects

This is the recommended method.

from labelbox import OntologyBuilder, Tool, Classification, Option, Client

client = Client()

object_features = [
    Tool(
        tool=Tool.Type.BBOX,
        name="regulatory-sign",
        color="#ff0000",
        classifications=[
            Classification(
                class_type=Classification.Type.RADIO,
                instructions="Sign type",
                options=[
                    Option(value="pedestrian", label="Pedestrian"),
                    Option(value="one_way", label="One way")
                ]
            )
        ]
    )
]

classification_features = [
    Classification(
        class_type=Classification.Type.CHECKLIST,
        instructions="Quality Issues",
        options=[
            Option(value="blurry", label="Blurry"),
            Option(value="distorted", label="Distorted")
        ]
    )

]

ontology_builder = OntologyBuilder(
    tools=object_features,
    classifications=classification_features
)

ontology = client.create_ontology("Ontology from new features", ontology_builder.asdict())

Create from normalized json format

  • Users can create ontologies from a json definition of the ontology.
  • Each tool type requires a specific value be passed:
ToolValue
Bounding boxrectangle
Polygonpolygon
Polylineline
Pointpoint
Segmentation masksuperpixel
Entitynamed-entity
# This will automatically create new feature schema
ontology_name = "sdk-ontology"
feature_schema_cat_normalized = {
    'tool': 'polygon',
    'name': 'cat',
    'color': 'black'
}

ontology_normalized_json = {
    "tools": [feature_schema_cat_normalized],
    "classifications": []
}
ontology = client.create_ontology(name=ontology_name,
                                  normalized=ontology_normalized_json)

Create from existing features

If you already have features created, you can query them by name or by schema id.
Re-using an existing feature is highly recommended.

## Search feature by name in your org
regulatory_sign_feature_schema = next(client.get_feature_schemas("regulatory-sign"))
classification_feature = next(client.get_feature_schemas("Quality Issues"))

## Get feature by feature schema ID. You can get this from the UI
regulatory_sign_feature_schema = client.get_feature_schema("FEATURE_SCHEMA_ID")

## Or, create a new 
ontology = client.create_ontology_from_feature_schemas("Ontology from existing features", [regulatory_sign_feature_schema.uid, classification_feature.uid])

Create a nested ontology

You can create a child feature nested under a parent feature. Only classification features can be children of a nested ontology. For instance, if you want to create an ontology where a bounding box has a radio sub-classification, you will add a classificationsfield that contains a list of classification features as children.

ontology_builder = OntologyBuilder(tools=[
    Tool(tool=Tool.Type.BBOX, name="cat", 
         classifications=[
            Classification(class_type=Classification.Type.RADIO, name="radio", options=[
                Option(value="long-fur"),
                Option(value="short-fur")
            ])
         ])
])

ontology = client.create_ontology("Simple nested ontology", ontology_builder.asdict())

Get an ontology

You can query an ontology by name or id.

## Search ontology by name in your org
ontology = next(client.get_ontologies(ontology_name))

## Get ontology by ID. You can get this from the UI
ontology = client.get_ontology("ONTOLOGY_ID")

print(ontology)

Connect an ontology to a project

project = client.create_project(name="my_new_project")
project.setup_editor(ontology)
print(project.ontology())