> 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/get-started/python-sdk-quickstart.md).

# Python SDK クイックスタート

Unitlab Python SDK のクイックスタートです。オントロジー付きのプロジェクトを作成し、画像フォルダーをアップロードして、処理結果とデータを確認するまでを約 5 分で体験できます。

約 5 分で、バウンディングボックスのオントロジーを持つプロジェクトを作成し、画像フォルダーをアップロードして、Unitlab が作成したアイテムを一覧表示します。

## 始める前に <a href="#before-you-begin" id="before-you-begin"></a>

* [SDK をインストール](https://docs.unitlab.ai/api-sdk-cli/ja/get-started/install-the-python-sdk-and-cli.md)し、[`UNITLAB_API_KEY` を設定](https://docs.unitlab.ai/api-sdk-cli/ja/get-started/authentication-and-api-keys.md)します。キーには **Manage projects**、**Manage ontologies**、**Manage data** が必要です。
* スクリプトと同じ場所にある `images` フォルダーに、JPG または PNG 画像を数枚入れます。

```
warehouse-picks/
├── quickstart.py
└── images/
    ├── bin-001.jpg
    └── bin-002.jpg
```

## スクリプトを実行する <a href="#run-the-script" id="run-the-script"></a>

次の内容を `quickstart.py` として保存し、Python で実行します。

```python
from pathlib import Path

from unitlab import OntologyStructure, RadioAttribute, Shape, UnitlabClient

client = UnitlabClient()  # reads UNITLAB_API_KEY
try:
    # 1. Describe what annotators will label.
    structure = OntologyStructure()
    item = structure.add_object("Item", Shape.BOUNDING_BOX)
    grasp = item.add_attribute(RadioAttribute, "Graspable", required=True)
    grasp.add_option("Yes")
    grasp.add_option("No")
    ontology = client.ontologies.create("Bin picking", structure=structure)

    # 2. Create a project that uses it.
    project = client.projects.create("Warehouse picks", ontology_hash=ontology.id)
    print("Project:", project.id)

    # 3. Upload the folder. One call creates one Batch Queue.
    batch = project.upload(Path(__file__).parent / "images")
    print(f"Uploaded {batch.uploaded} of {batch.total} files")
    for failure in batch.failed:
        print("Not uploaded:", failure.path, failure.error)

    # 4. Wait until the server has processed every file.
    status = batch.wait(timeout=1800)
    print(f"Processed: {status.completed}, failed: {status.failed}")

    # 5. List the items in this upload.
    for unit in project.data_units(batch_queue=batch.batch_queue_id):
        print(unit.name, unit.data_type, unit.status)
finally:
    client.close()
```

次のような出力が表示されます。ターミナルでは `batch.wait()` が進捗バーも表示します。

```
Project: 7c9e6679-7425-40de-944b-e07fc1f90ae7
Uploaded 2 of 2 files
Processed: 2, failed: 0
bin-001.jpg image annotate
bin-002.jpg image annotate
```

{% hint style="success" %}
アプリでプロジェクトを開くと、2 枚の画像が **Data** に表示され、**Start Annotating** で **Item** クラスを使って開けます。
{% endhint %}

## 何が起きたか <a href="#what-happened" id="what-happened"></a>

| ステップ | 呼び出し                         | 結果                                                             |
| ---- | ---------------------------- | -------------------------------------------------------------- |
| 1    | `client.ontologies.create()` | クラス 1 つと必須プロパティ 1 つを持つワークスペースのオントロジー                           |
| 2    | `client.projects.create()`   | そのオントロジーで開始するプロジェクト                                            |
| 3    | `project.upload()`           | ファイルを並列送信し、Batch Queue ID とローカルでの失敗を返す                         |
| 4    | `batch.wait()`               | 処理中のものがなくなるまでポーリングし、処理済み数と失敗数を返す                               |
| 5    | `project.data_units()`       | ファイルごとに 1 つの Data Unit とそのワークフローステータス（最初のステージで待機中は `annotate`） |

実行するたびに新しいオントロジーとプロジェクトが作成されます。プロジェクトを再利用する場合は、代わりに `client.projects.get("PROJECT_ID")` を呼び出します。

## うまくいかない場合 <a href="#if-something-goes-wrong" id="if-something-goes-wrong"></a>

| 症状                                         | 対処                                                                                |
| ------------------------------------------ | --------------------------------------------------------------------------------- |
| `AuthenticationError: No API key provided` | `UNITLAB_API_KEY` を設定します。                                                         |
| `PermissionDeniedError`                    | **始める前に**に記載した権限をキーでオンにします。                                                       |
| ファイルが `batch.failed` に含まれる                 | `failure.error` を読み、ファイルを修正して、そのファイルだけを再アップロードします。                                |
| `status.failed` が 0 より大きい                  | アプリで Batch Queue を開くか、`project.batch_queue(batch.batch_queue_id).data()` を呼び出します。 |
| `ProcessingTimeoutError`                   | 処理はまだ実行中です。後で `project.batch_queue(id).wait()` を再度呼び出します。                         |

## 次のステップ <a href="#next-steps" id="next-steps"></a>

* [アップロード、ラベル付け、エクスポート](https://docs.unitlab.ai/api-sdk-cli/ja/get-started/upload-label-and-export.md): モデルのラベルをインポートし、レビューしてリリースをエクスポートします。
* [プロジェクトへのアップロード](https://docs.unitlab.ai/api-sdk-cli/ja/sdks/project-uploads.md): 動画のフレームレート、CSV、医用画像、タイル化ラスター、制限事項。
* [Python でオントロジーを構築](https://docs.unitlab.ai/api-sdk-cli/ja/sdks/build-ontologies-in-python.md): すべての形状とプロパティタイプ。

## 関連情報 <a href="#related" id="related"></a>

[CLI クイックスタート](https://docs.unitlab.ai/api-sdk-cli/ja/get-started/cli-quickstart.md) · [UnitlabClient とリソース名前空間](https://docs.unitlab.ai/api-sdk-cli/ja/sdks/unitlabclient-and-resource-namespaces.md) · [例外、タイムアウト、リトライ](https://docs.unitlab.ai/api-sdk-cli/ja/sdks/exceptions-timeouts-and-retries.md)
