> 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 クライアントを使って、Web カメラ、カメラフィード、動画ファイルを Workflow にストリーミングします。

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

セルフホストのワーカー制限とサーバー側メディア参照については、 [Video Configuration](https://docs.roboflow.com/deployment/self-hosted/inference-server/configuration/video-configuration)を参照してください。プロセス上限はクライアント全体に適用され、アイドル状態のワーカーも含まれます。これは Serverless の制限とは別です。

## 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 reference](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 video sources](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc#video-sources) でソース設定を、 [consuming results](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 reference](https://docs.roboflow.com/reference/inference/inference-sdk/webrtc).
* 開始して設定する [セルフホストの 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).
