> 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 Cloud APIでは利用できません。次の環境で実行してください [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## Moondream2 API

{% 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` 専用デプロイURLまたはローカル推論サーバーに対して。

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

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"],
).configure(InferenceConfiguration(api_key_transport="header"))
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 %}

## Moondream2の推論速度

次で測定したレイテンシー [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1基の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) サーバー用。
* あなたの [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用のURL。
  {% endhint %}

## セルフホストのInferenceでMoondream2を実行する

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, "この画像には犬が何匹いますか？")

print(result)
```

{% endstep %}
{% endstepper %}

### Workflowsの実行モード

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

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