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

# ネイティブ Python API

ネイティブ Python API は、Inference を使用する最も簡単な方法で、基盤となるパッケージ API に直接アクセスします。この方法では、Inference モジュールを Python コードに直接インポートします。モデルの読み込み、推論の実行、結果の処理をすべて独自のロジック内で行います。また、Python 環境内で依存関係も管理します。シンプルなアプリを作成している場合や試しているだけの場合は、ネイティブ Python API から始めるのが最適です。

ネイティブ Python API の中心は、モデルを読み込み、その後それらの `infer(...)` メソッドを使って推論結果を取得します。

## クイックスタート

この例では、モデルを読み込み、推論を実行し、結果を表示する方法を示します。

次のものを使用することを推奨します： [Python 仮想環境 (venv)](https://docs.python.org/3/tutorial/venv.html) 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 のバージョンに合わせて調整してください
```

次に、モデルをインポートします：

```python
from inference import get_model

model = get_model(model_id="rfdetr-large")
```

この `get_model` メソッドは、Roboflow からコンピュータビジョンモデルを読み込むためのユーティリティ関数です。モデルは、その `model_id`。Roboflow のモデルでは、モデル ID はプロジェクト名とバージョン番号の組み合わせです： `f"{project_name}/{version_number}"`.

{% hint style="success" %}
モデルのプロジェクト名とバージョン番号は、次の場所で確認できます： [Roboflow アプリ](/reference/ja/ren-zheng/authentication/workspace-and-project-ids.md)。また、次の場所で、すぐに使える公開モデルを閲覧できます： [Roboflow Universe](https://universe.roboflow.com/)。この例では、COCO の事前学習済みモデルのエイリアスである特別なモデル ID を使用しています。 [事前学習済みモデルのエイリアス](https://docs.roboflow.com/models/pretrained-aliases) でエイリアスの一覧をご覧ください。
{% endhint %}

次に、入力画像を指定してこのモデルで推論を実行できます：

```python
from inference import get_model

model = get_model(model_id="rfdetr-large")

results = model.infer("people-walking.jpg") # 画像のパスに置き換えてください
```

results オブジェクトは推論レスポンスオブジェクトです（たとえば `ObjectDetectionInferenceResponse`、次の場所で定義されています： [`inference/core/entities/responses/inference.py`](https://github.com/roboflow/inference/blob/main/inference/core/entities/responses/inference.py)）。これには、処理時間などのメタデータと、予測の配列が含まれます。レスポンスの種類とその属性は、モデルの種類によって異なります。

では、次を使って結果を可視化してみましょう： [Supervision](https://supervision.roboflow.com):

```python
from inference import get_model
import supervision as sv
import cv2

# モデルを読み込み
model = get_model(model_id="rfdetr-large")

# cv2 で画像を読み込み
image = cv2.imread("people-walking.jpg")

# 推論を実行
results = model.infer(image)[0]

# 結果を Supervision Detection API に読み込み
detections = sv.Detections.from_inference(results)

# Supervision のアノテータを作成
bounding_box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()

# 推論結果からラベル配列を抽出
labels = [p.class_name for p in results.predictions]

# Supervision のアノテータを使って画像に結果を適用
annotated_image = bounding_box_annotator.annotate(scene=image, detections=detections)
annotated_image = label_annotator.annotate(
    scene=annotated_image, detections=detections, labels=labels
)

# 注釈付き画像をファイルに書き込むか表示する
sv.plot_image(annotated_image)
```

<img src="https://storage.googleapis.com/com-roboflow-marketing/inference/people-walking-annotated.jpg" alt="歩行中の人に注釈を付けた画像" width="100%">

## さまざまな画像タイプ

この `infer(...)` メソッドは、PIL 画像、OpenCV 画像（NumPy 配列）、ローカル画像へのパス、画像 URL など、さまざまな形式の画像を受け付けます。内部では、モデルは `load_image(...)` メソッドを [`image_utils` モジュール内の](https://github.com/roboflow/inference/blob/main/inference/core/utils/image_utils.py).

```python
from inference import get_model

import cv2
from PIL import Image

model = get_model(model_id="rfdetr-large")

image_url = "https://media.roboflow.com/inference/people-walking.jpg"
local_image_file = "people-walking.jpg"
pil_image = Image.open(local_image_file)
numpy_image = cv2.imread(local_image_file)

results = model.infer(image_url)
#または     = model.infer(local_image_file)
#または     = model.infer(pil_image)
#または     = model.infer(numpy_image)
```

## 推論パラメータ

この `infer(...)` メソッドは、推論パラメータを設定するためのキーワード引数を受け付けます。以下の例では、信頼度しきい値と IoU しきい値を設定しています。

```python
from inference import get_model

model = get_model(model_id="rfdetr-large")

results = model.infer("people-walking.jpg", confidence=0.75, iou_threshold=0.5)
```

## 次のステップ

* [Inference パイプライン](/reference/ja/tui-lun/inference-python/inference-pipeline.md) - 同じモデルをビデオストリームで実行します。
* [モデル重みのダウンロード](/reference/ja/tui-lun/inference-python/offline-weights.md) - 重みをキャッシュする場所を制御します。
