> 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/zh/sdks/build-ontologies-in-python.md).

# 用 Python 构建本体

使用 Unitlab Python SDK 的 OntologyStructure 定义类别、标注形状、属性和整条目分类，以代码方式构建并版本化管理本体。

本体定义了标注员可以标注的内容：带形状的类别、这些类别上的属性，以及针对整个条目的分类。

```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)
```

## 形状 <a href="#shapes" id="shapes"></a>

| `Shape`               | 绘制内容             | 典型媒体          |
| --------------------- | ---------------- | ------------- |
| `BOUNDING_BOX`        | 边界框              | 图片、视频、文档、医学影像 |
| `POLYGON`             | 轮廓               | 图片、视频、地理空间、病理 |
| `BITMASK`             | 像素掩码             | 图片、视频、医学影像    |
| `POINT`               | 点                | 图片、视频         |
| `POLYLINE`            | 开放折线             | 图片、视频、地理空间    |
| `SKELETON`            | 相互连接的关键点         | 图片、视频         |
| `CUBOID`              | 在 2D 媒体上绘制的 3D 框 | 图片、视频、文档、医学影像 |
| `TEXT`                | 文本片段             | 文本、HTML、表格    |
| `TIME_RANGE`          | 时间段              | 音频、视频         |
| `INTERVAL`, `INSTANT` | x 轴上的区间或时刻       | 时间序列          |

## 属性 <a href="#properties" id="properties"></a>

| 类                    | 标注员填写  |
| -------------------- | ------ |
| `RadioAttribute`     | 单个选项   |
| `ChecklistAttribute` | 任意多个选项 |
| `NumericAttribute`   | 数字     |
| `TextAttribute`      | 自由文本   |

| 标志                                 | 效果                       |
| ---------------------------------- | ------------------------ |
| `required=True`                    | 没有值时无法提交对象。发布会排除缺少该值的标注。 |
| `dynamic=True`                     | 在视频中，该值可以在不同关键帧之间变化      |
| `option.add_nested_attribute(...)` | 仅在选中该选项时显示的属性            |

## 读取与写入结构 <a href="#read-and-write-the-structure" id="read-and-write-the-structure"></a>

`structure.to_dict()` 返回 API 存储的 JSON，`OntologyStructure.from_dict(data)` 可将其读回，因此可以将本体纳入版本控制。

## 相关内容 <a href="#related" id="related"></a>

[本体资源](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/ontology-resources.md) · [项目管理](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/projects.md) · [CLI：本体与云存储](https://docs.unitlab.ai/api-sdk-cli/zh/clis/cli-ontologies-and-cloud-storage.md)
