> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/fr/sdks/build-ontologies-in-python.md).

# Créer des ontologies en Python

Définissez en Python avec OntologyStructure les classes, formes, propriétés et classifications d'élément d'une ontologie Unitlab via le SDK.

Une ontologie liste ce que les annotateurs peuvent annoter : des classes associées à une forme, des propriétés sur ces classes et des classifications portant sur l'élément entier.

```python
from unitlab import (
    ChecklistAttribute, NumericAttribute, OntologyStructure,
    RadioAttribute, Shape, TextAttribute,
)

structure = OntologyStructure()

# Objects: a class with a shape
gripper = structure.add_object("Gripper", Shape.BOUNDING_BOX, color="#2563EB")
state = gripper.add_attribute(RadioAttribute, "State", required=True, dynamic=True)
state.add_option("Open")
closed = state.add_option("Closed")
closed.add_nested_attribute(RadioAttribute, "Holding object").add_option("Yes")

part = structure.add_object("Part", Shape.POLYGON)
defects = part.add_attribute(ChecklistAttribute, "Defects")
for name in ("Scratch", "Dent", "Missing screw"):
    defects.add_option(name)
part.add_attribute(NumericAttribute, "Occlusion %")
part.add_attribute(TextAttribute, "Notes")

# Classification: answers about the whole item
scene = structure.add_classification()
lighting = scene.add_attribute(RadioAttribute, "Lighting", required=True)
lighting.add_option("Normal")
lighting.add_option("Low")

ontology = client.ontologies.create("Robot cell QA", structure=structure)
```

## Formes <a href="#shapes" id="shapes"></a>

| `Shape`               | Dessine                           | Médias typiques                      |
| --------------------- | --------------------------------- | ------------------------------------ |
| `BOUNDING_BOX`        | Boîte                             | Image, vidéo, document, médical      |
| `POLYGON`             | Contour                           | Image, vidéo, géospatial, pathologie |
| `BITMASK`             | Masque de pixels                  | Image, vidéo, médical                |
| `POINT`               | Point                             | Image, vidéo                         |
| `POLYLINE`            | Ligne ouverte                     | Image, vidéo, géospatial             |
| `SKELETON`            | Points clés reliés                | Image, vidéo                         |
| `CUBOID`              | Boîte 3D dessinée sur un média 2D | Image, vidéo, document, médical      |
| `TEXT`                | Segment de texte                  | Texte, HTML, tabulaire               |
| `TIME_RANGE`          | Segment temporel                  | Audio, vidéo                         |
| `INTERVAL`, `INSTANT` | Plage ou instant sur l'axe x      | Séries temporelles                   |

## Propriétés <a href="#properties" id="properties"></a>

| Classe               | L'annotateur fournit           |
| -------------------- | ------------------------------ |
| `RadioAttribute`     | Une option                     |
| `ChecklistAttribute` | Un nombre quelconque d'options |
| `NumericAttribute`   | Un nombre                      |
| `TextAttribute`      | Du texte libre                 |

| Option                             | Effet                                                                                               |
| ---------------------------------- | --------------------------------------------------------------------------------------------------- |
| `required=True`                    | L'objet ne peut pas être soumis sans valeur. Les releases excluent les labels auxquels elle manque. |
| `dynamic=True`                     | La valeur peut changer d'une keyframe à l'autre dans une vidéo                                      |
| `option.add_nested_attribute(...)` | Une propriété affichée uniquement lorsque cette option est choisie                                  |

## Lire et écrire la structure <a href="#read-and-write-the-structure" id="read-and-write-the-structure"></a>

`structure.to_dict()` renvoie le JSON stocké par l'API. `OntologyStructure.from_dict(data)` le relit, ce qui vous permet de conserver vos ontologies sous contrôle de version.

## Voir aussi <a href="#related" id="related"></a>

[Ressources d'ontologie](https://docs.unitlab.ai/api-sdk-cli/fr/sdks/ontology-resources.md) · [Projets](https://docs.unitlab.ai/api-sdk-cli/fr/sdks/projects.md) · [CLI : ontologies et stockage cloud](https://docs.unitlab.ai/api-sdk-cli/fr/clis/cli-ontologies-and-cloud-storage.md)
