> 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/roboflow/roboflow-hi/deploy/supported-models/sam2.md).

# SAM2

हम Meta के [Segment Anything Model 2](https://github.com/facebookresearch/sam2) हमारे माध्यम से इन्फरेंसिंग [Serverless Hosted API](/roboflow/roboflow-hi/deploy/serverless-hosted-api-v2.md). SAM2 एक promptable visual segmentation model है जो prompts के रूप में points और bounding boxes स्वीकार करता है। हम दो SAM2 endpoints प्रदान करते हैं:

* [/sam2/embed\_image](#post-sam2-embed_image), जो एक image embedding उत्पन्न करता है और cache करता है
* [/sam2/segment\_image](#post-sam2-segment_image), जो दिए गए prompts के लिए instance segmentation masks लौटाता है

## कोड नमूना

SAM2 को HTTP endpoint के माध्यम से सीधे चलाएँ: `curl`, या [`inference-sdk`](https://inference.roboflow.com/inference_helpers/inference_sdk/) रैपर के साथ।

{% tabs %}
{% tab title="HTTP (curl)" icon="webhook" %}
{% stepper %}
{% step %}

### अपनी API Key प्राप्त करें

एक Roboflow खाता बनाएं, अपनी key यहाँ पर ढूँढें [Roboflow API settings page](https://app.roboflow.com/settings/api) और इसे अपने shell में उपलब्ध कराएँ:

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

{% endstep %}

{% step %}

### मॉडल चलाएँ

को कॉल करें `/sam2/segment_image` एंडपॉइंट को `curl`:

```bash
curl --location 'https://serverless.roboflow.com/sam2/segment_image' \
  --header 'Content-Type: application/json' \
  --data '{
    \"api_key\": \"'\"$ROBOFLOW_API_KEY\"'\",
    "image": {"type": "url", "value": "https://media.roboflow.com/quickstart/traffic.jpg"},
    "prompts": {"prompts": [{"points": [{"x": 520, "y": 470, "positive": true}]}]},
    "sam2_version_id": "hiera_tiny"
  }'
```

{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="SDK (Python)" icon="python" %}
{% stepper %}
{% step %}

### अपनी API Key प्राप्त करें

एक Roboflow खाता बनाएं, अपनी key यहाँ पर ढूँढें [Roboflow API settings page](https://app.roboflow.com/settings/api) और इसे अपने shell में उपलब्ध कराएँ:

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

{% endstep %}

{% step %}

### निर्भरताएँ इंस्टॉल करें

ये packages मॉडल को कॉल करते हैं और उसके परिणामों को draw करते हैं:

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

{% endstep %}

{% step %}

### मॉडल चलाएँ

एक single positive point prompt के साथ segmentation endpoint को कॉल करें, लौटे हुए polygons को supervision के साथ detections में convert करें, और input image के ऊपर mask के साथ एक annotated PNG सहेजें:

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

content = requests.get("https://media.roboflow.com/quickstart/traffic.jpg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)
height, width = image.shape[:2]

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.sam2_segment_image(
    inference_input=image,
    prompts=[
        {"points": [{"x": 520, "y": 470, "positive": True}]}
    ],
    sam2_version_id="hiera_tiny",
)

detections = sv.Detections.from_sam3(sam3_result=result, resolution_wh=(width, height))

annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
cv2.imwrite("traffic_annotated.png", annotated)
```

`sv.Detections.from_sam3` SAM2 और SAM3 दोनों द्वारा लौटाई गई polygon predictions को पढ़ता है, इसलिए वही call दोनों में से किसी भी model के output को decode कर देती है।

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

## Inference speed

Latency मापी गई [Roboflow Inference](https://inference.roboflow.com/) 1x NVIDIA L4 पर, batch size 1, warmup के बाद का औसत।

<table data-search="false"><thead><tr><th>मॉडल</th><th>विलंबता (ms)</th></tr></thead><tbody><tr><td><code>sam2</code></td><td>177.7</td></tr></tbody></table>

के साथ मापा गया `segment_image` पर `hiera_large` checkpoint. SAM2 image embeddings को cache करता है, इसलिए यह figure हर call में एक fresh image का उपयोग करती है और पूर्ण encode plus decode cost को दर्शाती है। पहले से encoded image को फिर से prompt करना काफ़ी तेज़ है।

{% hint style="info" %}
सेट करें `api_url` को अपने deployment target से मिलाएँ:

* `https://serverless.roboflow.com` Serverless Hosted API के लिए।
* `http://localhost:9001` एक local [Inference](https://inference.roboflow.com/) server.
* आपका [Dedicated Deployment](/roboflow/roboflow-hi/deploy/dedicated-deployments.md) एक private endpoint के लिए URL.
  {% endhint %}

embedding caching और box prompts सहित अतिरिक्त usage details के लिए, देखें [Inference documentation](https://inference.roboflow.com/).
