> 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/reference/ja/inference/inference-sdk.md).

# Inference SDK

この `inference-sdk` Python パッケージでは次を提供します `InferenceHTTPClient`、への通信を行うクライアントです [Inference Server](https://docs.roboflow.com/deployment/self-hosted/inference-server) をHTTP経由で利用します。同じクライアントはRoboflowの [Serverless Hosted API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api)、 [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments)、セルフホスト型サーバー、またはエッジデバイス上で動作するサーバーでも利用できます。変更するのは `api_url` だけです。

```bash
pip install inference-sdk
```

{% hint style="info" %}
`inference-sdk` は軽量なHTTPクライアントであり、モデル自体は実行しません。独自のPythonプロセス内でモデルを読み込んで実行するには、 [`inference` パッケージ](/reference/ja/inference/inference-python.md).
{% endhint %}

## クイックスタート

URL、ファイルパス、PIL画像、NumPy配列から画像に対して推論を実行できます。

{% tabs %}
{% tab title="URL" %}

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
import os

image_url = "https://media.roboflow.com/inference/soccer.jpg"

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

results = client.infer(image_url, model_id="soccer-players-5fuqs/1")
print(results)
```

{% endtab %}

{% tab title="NumPy 配列" %}

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
import cv2
import os

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

numpy_image = cv2.imread("path/to/local/image.jpg")
results = client.infer(numpy_image, model_id="soccer-players-5fuqs/1")
print(results)
```

{% endtab %}

{% tab title="PIL画像" %}

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
from PIL import Image
import os

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

pil_image = Image.open("path/to/local/image.jpg")
results = client.infer(pil_image, model_id="soccer-players-5fuqs/1")
print(results)
```

{% endtab %}
{% endtabs %}

この `api_key_transport="header"` 設定では、APIキーを次の形式でのみ送信します `Authorization: Bearer` ヘッダーとして送信し、URLやログには含めません。これにはリリース1.5.0以降の inference server が必要です。古いサーバーを引き続き呼び出す場合は `"both"` を使用してください。次を参照してください [APIキーの送信方法](/reference/ja/inference/inference-sdk/configuration.md#api-key-transport) で3つのモードを確認できます。

セルフホスト型サーバーに対する最初のリクエストでは、モデルの重みがダウンロードされ、セットアップされます。このリクエストは、ネットワーク接続やモデルのサイズによって時間がかかる場合があります。モデルのダウンロードが完了すると、以降のリクエストは大幅に速くなります。さらに、 [モデルを事前に読み込み、ロード済みの重みを管理する](/reference/ja/inference/inference-sdk/model-management.md) ことで、この処理を制御することもできます。

{% hint style="info" %}
モデルIDは次の文字列で構成されます `<project_id>/<version_id>`。次を参照してください [Workspace ID と Project ID](/reference/ja/ren-zheng/authentication/workspace-and-project-ids.md) で確認できます。
{% endhint %}

### セルフホスト型サーバー

Inference Server をセルフホストすることもできます（ [Inference CLI](/reference/ja/inference/inference-cli.md)を参照）。その後、 `api_url` 内の `InferenceHTTPClient`:

```python
client = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key=os.environ["API_KEY"],
)
```

### AsyncIO クライアント

```python
import asyncio
from inference_sdk import InferenceHTTPClient

CLIENT = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key="ROBOFLOW_API_KEY"
)

image_url = "https://source.roboflow.com/pwYAXv9BTpqLyFfgQoPZ/u48G0UpWfk8giSw7wrU8/original.jpg"
loop = asyncio.get_event_loop()
result = loop.run_until_complete(
  CLIENT.infer_async(image_url, model_id="soccer-players-5fuqs/1")
)
```

## 並列推論とバッチ推論

1回の呼び出しで複数の画像に対して予測したい場合があります。 [`InferenceConfiguration`](/reference/ja/inference/inference-sdk/configuration.md) の2つのパラメータがバッチ処理と並列処理を制御します:

* `max_concurrent_requests` - 開始できる同時リクエストの最大数
* `max_batch_size` - 1つのリクエストに投入できる要素の最大数

これにより、次の改善が可能になります:

* 高性能なオンプレミスGPUマシン上で inference コンテナを実行する場合、 `max_batch_size` を適切に設定するとスループットの向上が期待できます
* ホストされた Roboflow API に対して inference を実行する場合、 `max_concurrent_requests` を設定すると複数の画像が同時に処理され、スループットの向上につながります
* 両方のオプションを組み合わせると、マシンクラスター上で inference コンテナを実行するクライアントに有益です。単一ノードの負荷を最適化し、異なるノードへの並列リクエストを同時に実行できます

```python
from inference_sdk import InferenceHTTPClient

image_url = "https://source.roboflow.com/pwYAXv9BTpqLyFfgQoPZ/u48G0UpWfk8giSw7wrU8/original.jpg"

# ROBOFLOW_API_KEY をあなたの Roboflow API キーに置き換えてください
CLIENT = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key="ROBOFLOW_API_KEY"
)
predictions = CLIENT.infer([image_url] * 5, model_id="soccer-players-5fuqs/1")

print(predictions)
```

バッチ処理と並列処理に対応するメソッド:

* `infer(...)` と `infer_async(...)`
* `ocr_image(...)` と `ocr_image_async(...)` (強制的に `max_batch_size=1`)
* `detect_gazes(...)` と `detect_gazes_async(...)` - **非推奨**、常に `inference_sdk.http.errors.FeatureDeprecatedError`
* `get_clip_image_embeddings(...)` と `get_clip_image_embeddings_async(...)`

このクライアントは以下もサポートします [コア基盤モデル](/reference/ja/inference/inference-sdk/core-models.md) (CLIP, DocTR)、 [Workflow の実行](/reference/ja/inference/inference-sdk/workflows.md) によるマルチステップのパイプライン、そして [WebRTC ストリーミング](/reference/ja/inference/inference-sdk/webrtc.md) によるリアルタイム動画推論。WebRTC を使うと、Webカメラ、カメラストリーム、動画ファイルをモデルまたは Workflow のいずれかで処理できます。

## 実際には予測として何が返されるのですか？

`InferenceHTTPClient` モデル配信 API の応答である通常の Python 辞書を返します。変更は以下の文脈でのみ行われます `可視化` キー。これによりサーバー生成の予測可視化が保持され、任意の形式へ変換できます。クライアント側のリスケーリングは入力サイズを調整するだけです。

## 次のステップ

* [設定](/reference/ja/inference/inference-sdk/configuration.md) - クライアントとモデルのパラメータ、コンテキストマネージャー、既定値。
* [モデル管理](/reference/ja/inference/inference-sdk/model-management.md) - サーバー上のモデルを事前読み込み、一覧表示、アンロードします。
* [コアモデル](/reference/ja/inference/inference-sdk/core-models.md) - CLIP と DocTR のエンドポイント。
* [Workflow](/reference/ja/inference/inference-sdk/workflows.md) - クライアント経由で Workflow を実行します。
* [WebRTC ストリーミング](/reference/ja/inference/inference-sdk/webrtc.md) - モデルまたは Workflow を通じて動画をストリーミングします。
