> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/ja/supported-models/roboflow-2.md).

# Roboflow 2.0

Roboflow 2.0 は DeepLabv3 ベースのセマンティックセグメンテーションモデルです。Roboflow 2.0 のモデルは Roboflow プラットフォームで学習し、当社の [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api).

セルフホスト型デプロイについては、 [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

## Roboflow 2.0 API

{% stepper %}
{% step %}

### APIキーを取得する

Roboflow アカウントを作成し、 [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) でキーを見つけ、シェルで利用できるようにします。

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### 依存関係をインストールする

以下をインストールします [Inference SDK](https://docs.roboflow.com/deployment/self-hosted/self-hosted) と [supervision](https://supervision.roboflow.com/):

```bash
pip install -U inference-sdk supervision
```

{% endstep %}

{% step %}

### モデルを実行する

学習済みの Roboflow 2.0 セマンティックセグメンテーションモデルに対して推論を実行し、ピクセルごとのクラスマップをデコードして、注釈付き PNG を書き出します。モデルはその `{workspace}/{model-slug}` IDに置き換えてください（ [バージョン、トレーニング、モデル](/models/ja/versions-trainings-and-models.md)).

応答には `segmentation_mask` （各ピクセル値がクラス ID である base64 エンコードされたグレースケール PNG で、 `0` は背景です）と `class_map` が含まれ、クラス ID をクラス名に対応付けます。スクリプトはそれを1つの `sv.Detections` クラスごとに1行に分割するので、 `sv.MaskAnnotator` がマスクを元の画像に重ねられます。

```python
import base64
import os
import cv2
import numpy as np
import supervision as sv
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image = sv.load_image_from_url("https://media.roboflow.com/quickstart/traffic.jpg")

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
).configure(InferenceConfiguration(api_key_transport="header"))
# 事前学習済みのエイリアスはありません: 独自のモデルを学習し、"your-project/1" をモデル ID に置き換えてください。
result = client.infer(image, model_id="your-project/1")
predictions = result["predictions"]

mask_bytes = base64.b64decode(predictions["segmentation_mask"])
class_map = predictions.get("class_map", {})
class_mask = cv2.imdecode(np.frombuffer(mask_bytes, np.uint8), cv2.IMREAD_GRAYSCALE)
class_mask = cv2.resize(class_mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)

class_ids = [cid for cid in np.unique(class_mask).tolist() if cid != 0]
if class_ids:
    masks, xyxy, names = [], [], []
    for cid in class_ids:
        binary = class_mask == cid
        rows = np.where(np.any(binary, axis=1))[0]
        cols = np.where(np.any(binary, axis=0))[0]
        xyxy.append([cols[0], rows[0], cols[-1], rows[-1]])
        masks.append(binary)
        names.append(class_map.get(str(cid), str(cid)))

    detections = sv.Detections(
        xyxy=np.array(xyxy, dtype=np.float64),
        mask=np.array(masks),
        class_id=np.array(class_ids),
        data={"class_name": np.array(names)},
    )
    annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
    annotated = sv.LabelAnnotator().annotate(annotated, detections)
else:
    annotated = image

cv2.imwrite("annotated.png", annotated)
"annotated.png" を保存しました
```

{% endstep %}
{% endstepper %}

{% hint style="info" %}
設定する `api_url` をデプロイ先に合わせます:

* `https://serverless.roboflow.com` サーバーレスクラウドAPI用。
* `http://localhost:9001` ローカルの [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバー用。
* あなたの [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用のURL。
  {% endhint %}
