> 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/tui-lun/inference-sdk.md).

# Inference SDK

この `inference-sdk` Pythonパッケージは提供します `InferenceHTTPClient`、ある [Inference Server](https://docs.roboflow.com/deployment/self-hosted/inference-server) へのHTTP経由での通信を行うクライアントです。同じクライアントはRoboflow [サーバーレスホスト型 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プロセス内でモデルを読み込んで実行するには、次を使用してください。 [`推論` パッケージ](/reference/ja/tui-lun/inference-python.md).
{% endhint %}

## クイックスタート

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

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

```python
from inference_sdk import InferenceHTTPClient
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"],
)

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

{% endtab %}

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

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

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

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
from PIL import Image
import os

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

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

{% endtab %}
{% endtabs %}

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

{% hint style="info" %}
モデルIDは次の文字列で構成されます `<project_id>/<version_id>`。参照 [ワークスペースIDとプロジェクトID](/reference/ja/ren-zheng/authentication/workspace-and-project-ids.md) ご自身のものを見つけるには。
{% endhint %}

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

Inference Server をセルフホストすることもできます（次を参照： [Inference CLI](/reference/ja/tui-lun/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回の呼び出しで複数の画像に対して予測したい場合があります。の2つのパラメータが [`InferenceConfiguration`](/reference/ja/tui-lun/inference-sdk/configuration.md) バッチ処理と並列処理を制御します：

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

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

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

```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/tui-lun/inference-sdk/core-models.md) （CLIP、DocTR）、 [Workflows の実行](/reference/ja/tui-lun/inference-sdk/workflows.md) 複数ステップのパイプライン向けで、 [WebRTCストリーミング](/reference/ja/tui-lun/inference-sdk/webrtc.md) リアルタイム動画推論向けです。WebRTC を使うと、モデルまたは Workflow のいずれかで、ウェブカメラ、カメラストリーム、動画ファイルを処理できます。

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

`InferenceHTTPClient` モデル配信 API からのレスポンスである通常の Python 辞書を返します。変更は、 `可視化` キーのコンテキスト内でのみ行われます。このキーは、サーバー生成の予測可視化を保持し、任意の形式に変換できます。クライアント側のリスケーリングは入力サイズを調整するだけです。

## 次のステップ

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