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

# Moondream2

Moondream2는 컴팩트한 비전-언어 모델입니다. Roboflow Inference에서는 오픈 보캐뷸러리 객체 탐지기로 노출됩니다. 클래스 이름을 프롬프트로 전달하면 일치하는 영역의 바운딩 박스를 반환합니다.

{% hint style="info" %}
Moondream2는 Serverless Hosted API에서 사용할 수 없습니다. 다음에서 실행하세요: [전용 배포](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` 전용 배포 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="개",
)

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/50e2c5f01e94b34eab4bd5d602f4810b41e77750" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## 추론 속도

다음을 사용해 측정한 지연 시간 [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1x NVIDIA L4에서, 배치 크기 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, "이 이미지에는 개가 몇 마리 있나요?")

print(result)
```

{% endstep %}
{% endstepper %}

### Workflows의 실행 모드

다음에서 사용될 때 [Workflow](https://docs.roboflow.com/workflows), Moondream2는 다음 두 가지 모드 중 하나로 실행됩니다:

* **로컬 실행** : 모델이 사용자 Inference 서버에서 실행됩니다(GPU 권장).
* **원격 실행** : 모델은 `infer_lmm()` 클라이언트 메서드를 통해 원격 Inference 서버에서 HTTP로 호출됩니다.
