> 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/workflows/ja/depuroi/video-processing.md).

# Workflows による動画処理

Inference SDK の WebRTC クライアントを使用して、Workflow 経由で動画をストリーミングします。同じクライアントは、セルフホスト型の Inference Server と Serverless Video Streaming API の両方で動作します。

WebRTC は、Workflow がフレームを処理している間、1つのセッションを開いたままにします。これにより、トラッカー、カウンター、バッファーなどのステートフルなブロックをサポートできます。また、各フレームを個別の HTTP リクエストとして送信する必要もありません。

## ランタイムを選択

<table data-search="false"><thead><tr><th>ランタイム</th><th>API URL</th><th>こんな場合に使用</th></tr></thead><tbody><tr><td>セルフホスト型 Inference Server</td><td><code>http://localhost:9001</code></td><td>動画を自分のハードウェアまたはネットワーク上に留めたい場合。</td></tr><tr><td>Serverless Video Streaming API</td><td><code>https://serverless.roboflow.com</code></td><td>計算処理を Roboflow に管理してほしい場合。</td></tr></tbody></table>

セルフホストする場合は、 [Inference Server](https://docs.roboflow.com/deployment/self-hosted/inference-server) をクライアントを実行する前に起動してください。ホスト型の制限、リージョン、GPU プランについては、次を参照してください: [Serverless Video Streaming](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api/serverless-video-streaming-api).

## SDK をインストール

```bash
pip install "inference-sdk[webrtc]"
```

## Workflow をストリーミングする

この例ではウェブカメラから動画をキャプチャし、 `予測` を保存済み Workflow の出力として表示します:

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
from inference_sdk.webrtc import StreamConfig, WebcamSource

client = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key="ROBOFLOW_API_KEY",
).configure(InferenceConfiguration(api_key_transport="header"))

session = client.webrtc.stream(
    source=WebcamSource(),
    workflow="workflow-id",
    workspace="workspace-name",
    config=StreamConfig(data_output=["predictions"]),
)

@session.on_data("predictions")
def handle_predictions(predictions, metadata):
    print(f"Frame {metadata.frame_id}: {predictions}")

session.run()
```

置き換えてください `workflow-id`, `workspace-name`と `ROBOFLOW_API_KEY` を自分の値に。Serverless で実行するには、 `api_url` を `https://serverless.roboflow.com`.

次の `data_output` にある名前は、Workflow で定義された出力と一致している必要があります。画像出力を動画として受け取るには、 `stream_output` に追加し、 `on_frame` ハンドラーを登録してください。 [WebRTC Streaming リファレンス](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc#stream-a-workflow) を参照すると、完全な例があります。

## 動画ソースを選択

次を変更 `source` を `client.webrtc.stream()` に渡し、動画の取得元に応じて次のようにします:

<table data-search="false"><thead><tr><th>ソース</th><th>こんな場合に使用</th></tr></thead><tbody><tr><td><code>WebcamSource()</code></td><td>カメラはクライアントマシンに接続されています。</td></tr><tr><td><code>RTSPSource("rtsp://...")</code></td><td>Inference Server が RTSP カメラに接続できます。</td></tr><tr><td><code>LocalStreamSource("rtsp://...")</code></td><td>RTSP または RTMP ストリームにアクセスできるのはクライアントのみです。</td></tr><tr><td><code>VideoFileSource("video.mp4")</code></td><td>保存済みの動画ファイルを処理したい場合。</td></tr><tr><td><code>ManualSource()</code></td><td>アプリケーションが個々のフレームを提供します。</td></tr></tbody></table>

を使用する場合、 `RTSPSource` Serverless では、カメラの URL はパブリックインターネットから到達可能である必要があります。カメラがクライアントのネットワーク内でのみ利用可能な場合は、 `LocalStreamSource` を使用してください。

参照: [WebRTC の動画ソース](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc#video-sources) でソース設定を、 [結果の消費](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc#consuming-results) でコールバック、イテレーター、セッションのクリーンアップを確認してください。

## 1つのモデルをストリーミング

1つのモデルだけが必要な場合は、Workflow を作成する必要はありません。 `model_id` の代わりに `workflow` と `workspace`:

```python
session = client.webrtc.stream(
    source=WebcamSource(),
    model_id="rfdetr-nano",
)
```

参照: [モデルをストリーミングする](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc#stream-a-model) タスクタイプごとの予測処理用に。

## 次のステップ

* フレームとデータの出力を設定する [WebRTC Streaming リファレンス](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc).
* セルフホスト型 Inference Server を起動して設定する [セルフホスト型 Inference Server](https://docs.roboflow.com/deployment/self-hosted/inference-server).
* でホスト型リージョン、プラン、制限を確認する [Serverless Video Streaming](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api/serverless-video-streaming-api).
