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

# Model APIエンドポイントを使用する

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

このガイドでは、何もない状態から5分でモデルを実行できるようになります。画像内の物体を名前で指定するだけで見つけ、既製モデルで日常的な物体を検出し、自分のモデルを実行できます。すべて同じサーバーレスAPIエンドポイントを使います。

## 名前を付けて物体を見つける

モデルに次のような語句をいくつか与えます `"タクシー"`, `"青いバス"`、または `"茂み"` すると、画像内の一致するすべての物体を輪郭で囲みます。最初に設定や学習をする必要はありません。これは [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 supervision
```

{% endstep %}

{% step %}

### モデルを実行する

次にPOSTリクエストを送信します: `/sam3/concept_segment` これでモデルを実行します。 `sv.Detections.from_sam3` それらの結果を次のような形式に変換します [`supervision`](https://supervision.roboflow.com) 描画できます:

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

content = requests.get("https://media.roboflow.com/quickstart/traffic.jpg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)
image_b64 = base64.b64encode(content).decode("utf-8")
h, w = image.shape[:2]

response = requests.post(
    "https://serverless.roboflow.com/sam3/concept_segment",
    params={"api_key": os.environ["ROBOFLOW_API_KEY"]},
    json={
        "image": {"type": "base64", "value": image_b64},
        "prompts": [
            {"type": "text", "text": "タクシー"},
            {"type": "text", "text": "青いバス"},
            {"type": "text", "text": "茂み"},
        ],
    },
)

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 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 numpy as np
import requests
import supervision as sv
from inference_sdk import InferenceHTTPClient

content = requests.get("https://media.roboflow.com/quickstart/traffic.jpg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)
h, w = image.shape[:2]

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

data = client.sam3_concept_segment(
    image,
    prompts=[
        {"type": "text", "text": "タクシー"},
        {"type": "text", "text": "青いバス"},
        {"type": "text", "text": "茂み"},
    ],
)

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="/files/86db0a5230fef4246e56b13cff137935387fe1b0" 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}` モデルIDを使います。代わりに `POST /sam3/concept_segment`。この例では既製の [RF-DETR](https://docs.roboflow.com/models/supported-models/rf-detr) モデルを使います。これは車両、人、ペットなどの一般的な物体を検出できます:

```python
result = requests.post(
    "https://serverless.roboflow.com/coco/40",  # coco/40 は rfdetr-medium の別名
    params={"api_key": os.environ["ROBOFLOW_API_KEY"]},
    data=image_b64,  # 本文には生のbase64を入れる。JSONではない
    headers={"Content-Type": "application/x-www-form-urlencoded"},
).json()
detections = sv.Detections.from_inference(result)

print(f"{len(detections)} 個のオブジェクトを検出しました")

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()` モデルIDを使います。代わりに `client.sam3_concept_segment()`。この例では既製の [RF-DETR](https://docs.roboflow.com/models/supported-models/rf-detr) モデルを使います。これは車両、人、ペットなどの一般的な物体を検出できます:

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

print(f"{len(detections)} 個のオブジェクトを検出しました")

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

{% endtab %}
{% endtabs %}

<figure><img src="/files/f893ac8ee103dba7bf6bee7d9486692cd70be913" 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` （Inference SDKのみ）

## 次に進むには

クラウド上でモデルを実行しています。ここから次のことができます:

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