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

# Inference Python パッケージ

この `inference` Python パッケージは、Roboflow のコンピュータビジョンデプロイメントスタックを支える中核のオープンソースライブラリです。モデルの読み込み、前処理/後処理、GPU/CPU 最適化、そして [Workflow](https://docs.roboflow.com/workflows) 実行を行い、Python から直接呼び出せます。

この [Inference Server](https://docs.roboflow.com/deployment/self-hosted/inference-server) このパッケージをラップし、HTTP 経由で公開します（依存関係をすべてインストールした Docker イメージとして配布されます）が、 `inference` 自分のスクリプトやアプリケーション内で直接使うこともできます。

## 各要素のつながり

Inference には、コンピュータビジョンモデルを提供するために連携するいくつかのコンポーネントがあります。

* [**inference**](/reference/ja/inference/inference-python.md) - モデルの読み込み、推論、Workflows の実行のための中核 Python パッケージ。
* [**inference-sdk**](/reference/ja/inference/inference-sdk.md) - HTTP 経由で Inference Server と通信するための軽量 Python クライアント。
* [**inference-cli**](/reference/ja/inference/inference-cli.md) - Inference Server の管理や一般的なタスクの実行を行うコマンドラインツール。
* [**Inference Server**](https://docs.roboflow.com/deployment/self-hosted/inference-server) -  `inference` パッケージを REST API としてラップする HTTP サーバー（Docker）。

実行時にこれらの要素がどのように動作するか（リクエストのルーティング、並列化、マイクロサービスとアプライアンスのパターン）については、 [Inference アーキテクチャ](https://docs.roboflow.com/deployment/self-hosted/inference-server/architecture).

## マルチバックエンド対応

Inference 1.0 は、ONNX、TensorRT、Hugging Face、PyTorch など複数の推論実行バックエンドをサポートしています。ハードウェアで利用可能な最速のバックエンドを自動的に選択します。たとえば、NVIDIA GPU を搭載している場合、または Jetson デバイス上で実行していて、プラットフォーム上でモデルに対応する TensorRT エンジンが利用可能な場合、Inference はデフォルトで TensorRT を使用します。

## インストール

私たちは、 [Python 仮想環境（venv）](https://docs.python.org/3/tutorial/venv.html) を使って Inference の依存関係を分離することを推奨します。

pip で Inference をインストールするには:

```bash
pip install inference
```

NVIDIA GPU がある場合は、以下で推論を高速化できます:

```bash
pip install --extra-index-url https://download.pytorch.org/whl/cu124 inference-gpu
# --extra-index-url は、OS にインストールされている CUDA のバージョンに合わせて調整してください
# https://download.pytorch.org/whl/cu<major><minor>、たとえば CUDA 13.0 なら https://download.pytorch.org/whl/cu130
```

## クイック例

HTTP 経由で Inference Server に対して（ `inference-sdk`を使って）、またはプロセス内で直接（ネイティブの `inference` パッケージを使って）推論を実行できます。ネイティブの `get_model()` 呼び出しはモデルをスクリプトに読み込み、 `.infer()` を呼び出せるオブジェクトを返します。一方、HTTP クライアントは画像を同じ処理を行うサーバーに送信します。

{% tabs %}
{% tab title="推論（ネイティブ）" %}

```python
from inference import get_model

model = get_model(model_id="rfdetr-small")
results = model.infer("https://media.roboflow.com/inference/people-walking.jpg")
```

{% endtab %}

{% tab title="inference-sdk（HTTP クライアント）" %}

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

client = InferenceHTTPClient(
    # api_url="http://localhost:9001",  # self-hosted の場合
    api_url="https://serverless.roboflow.com",
    api_key="ROBOFLOW_API_KEY",
).configure(InferenceConfiguration(api_key_transport="header"))
results = client.infer(
    "https://media.roboflow.com/inference/people-walking.jpg",
    model_id="rfdetr-small",
)
```

{% endtab %}
{% endtabs %}

API キーが必要なモデルを使用するには、 `ROBOFLOW_API_KEY` 環境変数を設定するか、直接渡してください:

```python
model = get_model(model_id="my-project/1", api_key="ROBOFLOW_API_KEY")
```

次を参照してください [ネイティブ Python API](/reference/ja/inference/inference-python/native-python-api.md) ページでは、可視化付きのより詳しい手順と、 [Run a Model](https://docs.roboflow.com/deployment/self-hosted/self-hosted#run-a-model) サーバーベースの経路について説明しています。

## Inference パイプライン

`InferencePipeline` は、ビデオを  `inference` パッケージと同じ Python プロセス内で実行します。直接 Inference Library を実行することを選び、独自のロジックや sink インターフェースが必要な場合にのみ使用してください。Inference Server または Serverless を使うアプリケーションでは、 [WebRTC ストリーミング](/reference/ja/inference/inference-sdk/webrtc.md).

```python
from inference import InferencePipeline
from inference.core.interfaces.stream.sinks import render_boxes

pipeline = InferencePipeline.init(
    model_id="rfdetr-large",
    video_reference="https://storage.googleapis.com/com-roboflow-marketing/inference/people-walking.mp4",
    on_prediction=render_boxes,
    api_key="ROBOFLOW_API_KEY",
)

pipeline.start()
pipeline.join()
```

上記のコードは、（ `render_boxes` sink を通じて）オブジェクト検出の注釈を直接行います。詳細は、 [Inference パイプライン](/reference/ja/inference/inference-python/inference-pipeline.md) ページを参照してください。

## 貢献

Inference はオープンソースです。ソースコード、issue トラッカー、コントリビューションガイドは [roboflow/inference](https://github.com/roboflow/inference) リポジトリにあります。開始方法については、 [CONTRIBUTING.md](https://github.com/roboflow/inference/blob/main/CONTRIBUTING.md) を参照してください。

## 次のステップ

* [ネイティブ Python API](/reference/ja/inference/inference-python/native-python-api.md) - モデルを読み込み、自分のプロセス内で推論を実行します。
* [Inference パイプライン](/reference/ja/inference/inference-python/inference-pipeline.md) - 動画ストリーム上でモデルを実行します。
* [モデル重みのダウンロード](/reference/ja/inference/inference-python/offline-weights.md) - 重みのキャッシュと永続ストレージ。
* [ベンチマーク](/reference/ja/inference/inference-python/benchmarks.md) - 一般的なハードウェアで測定したスループット。
