> 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/get-started/ja/gaido/run-model-serverless-api.md).

# モデルAPIエンドポイントを使用する

ハードウェアの準備は不要で、5分でRoboflowのクラウドGPU上でモデルを実行できます。

Roboflowの [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api) は、Roboflowのクラウド上のGPUでモデルとワークフローを実行します。画像を送信すると、結果が返ってきます。ハードウェアのセットアップも、稼働し続けるサーバーの管理も不要です。

このガイドでは、何もない状態から5分でモデルを実行するまでを説明します。オブジェクトの名前を指定して画像内から見つけたり、すぐに使えるモデルで日常的な物体を検出したり、独自のモデルを実行したりできます。いずれも同じServerless Cloud APIエンドポイントを使用します。

## 名前を指定してオブジェクトを見つける

モデルに次のような単語をいくつか与えます。 `"taxi"`, `"blue bus"`、または `"bush"` を指定すると、画像内で一致するすべてのオブジェクトを輪郭で囲みます。事前のセットアップや学習は必要ありません。これは [SAM3](https://docs.roboflow.com/models/supported-models/sam3).

{% tabs %}
{% tab title="HTTP（Python）" icon="webhook" %}
{% stepper %}
{% step %}

### APIキーを取得する

Roboflowアカウントを作成し、 [Roboflow API設定ページ](https://app.roboflow.com/settings/api) でキーを確認して、シェルから使えるようにします。

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

{% endstep %}

{% step %}

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

`supervision` をインポートすると、 `cv2` と `numpy`:

```bash
pip install -U supervision
```

{% endstep %}

{% step %}

### モデルを実行する

モデルを実行するには、 `/sam3/concept_segment` にPOSTリクエストを送信します。 `sv.Detections.from_sam3` は、結果を [`supervision`](https://supervision.roboflow.com) で描画できる形式に変換します。

```python
import os
import base64
import cv2
import requests
import supervision as sv

image = sv.load_image_from_url("https://media.roboflow.com/quickstart/traffic.jpg")
image_b64 = base64.b64encode(content).decode("utf-8")
h, w = image.shape[:2]

response = requests.post(
    "https://serverless.roboflow.com/sam3/concept_segment",
    headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
    json={
        "image": {"type": "base64", "value": image_b64},
        "prompts": [
            {"type": "text", "text": "taxi"},
            {"type": "text", "text": "blue bus"},
            {"type": "text", "text": "bush"},
        ],
    },
)

detections = sv.Detections.from_sam3(response.json(), (w, h))
annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
cv2.imwrite("sam3.jpg", annotated)
```

{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="SDK（Python）" icon="python" %}
{% stepper %}
{% step %}

### APIキーを取得する

Roboflowアカウントを作成し、 [Roboflow API設定ページ](https://app.roboflow.com/settings/api) でキーを確認して、シェルから使えるようにします。

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

{% endstep %}

{% step %}

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

この2つのパッケージで、モデルを呼び出して結果を描画できます。

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

{% endstep %}

{% step %}

### モデルを実行する

「 [Inference SDK](https://docs.roboflow.com/reference/inference/inference-sdk) 」は画像をクラウドに送信し、結果を返します。 `sv.Detections.from_sam3` は、結果を次の形式に変換します [`supervision`](https://supervision.roboflow.com) は次を描画できます:

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

image = sv.load_image_from_url("https://media.roboflow.com/quickstart/traffic.jpg")
h, w = image.shape[:2]

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
).configure(InferenceConfiguration(api_key_transport="header"))

data = client.sam3_concept_segment(
    image,
    prompts=[
        {"type": "text", "text": "taxi"},
        {"type": "text", "text": "blue bus"},
        {"type": "text", "text": "bush"},
    ],
)

detections = sv.Detections.from_sam3(data, (w, h))
annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
annotated = sv.BoxAnnotator().annotate(annotated, detections)
cv2.imwrite("sam3.jpg", annotated)
```

{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

レスポンスには、各プロンプトで見つかった輪郭と、それぞれの信頼度スコアが含まれます。 `sv.Detections.from_sam3` がこれらすべてを読み取ります。

<figure><img src="https://2537277549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1YMZNIiZMBdlCHUDeZMP%2Fuploads%2Fgit-blob-fc88d211337fc1990e06fab3fc499075fdedd7c1%2Fserverless-sam3-out.jpg?alt=media" alt="Outlines around taxis, a blue bus, and bushes from text prompts"><figcaption><p>テキストプロンプトで輪郭を描いたオブジェクト</p></figcaption></figure>

ビジュアルプロンプト、例示ボックス、RLE出力を含むSAM3 APIの詳細については、 [SAM3のドキュメント](https://docs.roboflow.com/models/supported-models/sam3).

## 既製モデルで日常的な物体を検出する

{% tabs %}
{% tab title="HTTP（Python）" icon="webhook" %}
名前を指定した物体の輪郭を描く代わりに、一般的な物体を囲むボックスを描くには、 `POST /{project}/{version}` を、 `POST /sam3/concept_segment`の代わりにモデルIDを指定して呼び出します。この例では既製の [RF-DETR](https://docs.roboflow.com/models/supported-models/rf-detr) モデルを使用します。車両、人、ペットなど一般的な物体を検出できます。RF-DETRはApache 2.0ライセンスのため、商用利用も無料です:

```python
result = requests.post(
    "https://serverless.roboflow.com/coco/40",  # coco/40 is the rfdetr-medium alias
    data=image_b64,  # raw base64 in the body, not JSON
    headers={
        "Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}",
        "Content-Type": "application/x-www-form-urlencoded",
    },
).json()
detections = sv.Detections.from_inference(result)

print(f"Found {len(detections)} objects")

annotated = sv.BoxAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections)
cv2.imwrite("rf-detr.jpg", annotated)
```

{% endtab %}

{% tab title="SDK（Python）" icon="python" %}
名前を指定した物体の輪郭を描く代わりに、一般的な物体を囲むボックスを描くには、 `client.infer()` を、 `client.sam3_concept_segment()`の代わりにモデルIDを指定して呼び出します。この例では既製の [RF-DETR](https://docs.roboflow.com/models/supported-models/rf-detr) モデルを使用します。車両、人、ペットなど一般的な物体を検出できます。RF-DETRはApache 2.0ライセンスのため、商用利用も無料です:

```python
result = client.infer(image, model_id="rfdetr-medium")
detections = sv.Detections.from_inference(result)

print(f"Found {len(detections)} objects")

annotated = sv.BoxAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections)
cv2.imwrite("rf-detr.jpg", annotated)
```

{% endtab %}
{% endtabs %}

<figure><img src="https://2537277549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1YMZNIiZMBdlCHUDeZMP%2Fuploads%2Fgit-blob-516089a73418852ced8eddbeeb1a94db1bc11421%2Fserverless-rfdetr-out.jpg?alt=media" alt="RF-DETR bounding boxes for cars, buses, people, and motorcycles"><figcaption><p>RF-DETRが車、バス、人、バイクを検出します</p></figcaption></figure>

## 独自のモデルや別のモデルを実行する

同じ `client.infer()` または `POST /{project}/{version}` 呼び出しで、任意のモデルをその `model_id`:

* Roboflowでトレーニングしたモデル。モデルID（次の形式）をコピーします: `{project}/{version}`）をプロジェクトから [Roboflowアプリ](https://app.roboflow.com)
* 次から利用できる公開モデル： [Roboflow Universe](https://docs.roboflow.com/datasets/universe/universe/what-is-roboflow-universe)
* すぐに使える [事前学習済みモデル](https://docs.roboflow.com/models/pretrained-aliases) 次のようなエイリアスで指定します： `rfdetr-nano`, `rfdetr-seg-medium`または `yolo26l-640` （推論SDKでのみ利用可能）。RF-DETRはApache 2.0、YOLO26はAGPL-3.0ライセンスです。Roboflowにデプロイする場合は、商用ライセンスが含まれます。詳しくは [モデルのライセンス](https://docs.roboflow.com/models/model-licenses)

## 次に進む

モデルをクラウドで実行しています。ここでは次のことができます：

* 同じモデルを独自のハードウェアで実行できます。詳しくは [モデルをローカルで実行する](https://docs.roboflow.com/get-started/ja/gaido/run-a-model-locally.md)
* 大規模モデルや安定したトラフィック向けに、専用のシングルテナントエンドポイントを利用できます： [専用デプロイ](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments)
* モデルとロジックを連携させてアプリケーションを構築できます： [ワークフロー](https://docs.roboflow.com/workflows)
* 各リクエストの料金を確認できます： [サーバーレス料金ページ](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api/model-pricing)
