> 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/models/ja/supported-models/moondream2.md).

# Moondream2

Moondream2 はコンパクトな視覚言語モデルです。Roboflow Inference では、オープンボキャブラリの物体検出器として公開されています。プロンプトとしてクラス名を渡すと、該当領域のバウンディングボックスを受け取れます。

{% hint style="info" %}
Moondream2 は Serverless Hosted API では利用できません。a 上で実行してください。 [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [セルフホスト型 Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## コード例

{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### 依存関係をインストールする

以下をインストールします： [Inference SDK](https://docs.roboflow.com/deployment/self-hosted/self-hosted) と [supervision](https://supervision.roboflow.com/):

```bash
pip install -U inference-sdk supervision opencv-python
```

{% endstep %}

{% step %}

### モデルを実行する

設定する `api_url` Dedicated Deployment URL またはローカルの推論サーバーに接続してください。

```python
import os
import cv2
import numpy as np
import supervision as sv
from inference_sdk import InferenceHTTPClient

image = sv.load_image_from_url("https://media.roboflow.com/notebooks/examples/dog.jpeg")
client = InferenceHTTPClient(
    api_url="https://your-deployment.roboflow.cloud",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.infer_lmm(
    image,
    model_id="moondream2",
    prompt="dog",
)

preds = result["predictions"]
xyxys = [
    [p["x"] - p["width"] / 2, p["y"] - p["height"] / 2,
     p["x"] + p["width"] / 2, p["y"] + p["height"] / 2]
    for p in preds
]
detections = sv.Detections(
    xyxy=np.array(xyxys, dtype=float),
    class_id=np.array([p.get("class_id", 0) for p in preds]),
    confidence=np.array([p.get("confidence", 1.0) for p in preds], dtype=float),
    data={"class_name": np.array([p["class"] for p in preds])},
)
labels = [f"{p['class']} {p.get('confidence', 1.0):.2f}" for p in preds]
annotated = sv.BoxAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections, labels=labels)
cv2.imwrite("dog_annotated.png", annotated)
```

<figure><img src="/files/c4c6d57a1db567261afdcff2f31ef1409f6c321e" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## 推論速度

レイテンシの測定条件： [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1x NVIDIA L4、バッチサイズ 1 で、1 枚の画像にキャプションを付ける場合。Moondream2 は出力長を固定できないため、レイテンシは応答によって変動します。

<table data-search="false"><thead><tr><th>別名</th><th>レイテンシ（ms）</th></tr></thead><tbody><tr><td><code>moondream2</code></td><td>1669</td></tr></tbody></table>

{% hint style="info" %}
設定する `api_url` をデプロイ先に合わせます：

* `http://localhost:9001` ローカルの [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバー用。
* あなたの [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用の URL。
  {% endhint %}

## Inference（セルフホスト型）で使用する

Moondream2 は次のものを使って直接読み込むこともできます。 [`inference`](https://docs.roboflow.com/deployment/self-hosted/self-hosted) パッケージ。検出以外にも、このモデルは画像キャプション生成、ポイントプロンプト検出、視覚質問応答をサポートします。

{% stepper %}
{% step %}

### パッケージをインストールする

```bash
pip install "inference[transformers]"
```

以下を使用してください： `inference-gpu[transformers]` GPU マシン上で。
{% endstep %}

{% step %}

### モデルを実行する

```python
from PIL import Image

from inference.models.moondream2.moondream2 import Moondream2

model = Moondream2(api_key="YOUR_API_KEY")

image = Image.open("dog.jpeg")
result = model.query(image, "How many dogs are in this image?")

print(result)
```

{% endstep %}
{% endstepper %}

### Workflows の実行モード

以下で使用する場合： [Workflow](https://docs.roboflow.com/workflows)、Moondream2 は次の 2 つのモードのいずれかで動作します：

* **ローカル実行**：モデルは Inference サーバー上で実行されます（GPU 推奨）。
* **リモート実行**：モデルは、以下を介してリモート Inference サーバー上で HTTP 経由で呼び出されます： `infer_lmm()` クライアントメソッド。
