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

# Python でオントロジーを構築する

Unitlab Python SDK の OntologyStructure で、形状付きのクラス、プロパティ、アイテム全体の分類（classification）を定義してオントロジーを構築する方法と、対応する形状とプロパティの種類を解説します。

オントロジーは、アノテーターがラベル付けできる内容を定義します。形状を持つクラス、それらのクラスのプロパティ、アイテム全体に対する分類で構成されます。

```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`     | 1 つの選択肢       |
| `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/ja/sdks/ontology-resources.md) · [プロジェクト](https://docs.unitlab.ai/api-sdk-cli/ja/sdks/projects.md) · [CLI：オントロジーとクラウドストレージ](https://docs.unitlab.ai/api-sdk-cli/ja/clis/cli-ontologies-and-cloud-storage.md)
