# SAM2

We support Meta's [Segment Anything Model 2](https://github.com/facebookresearch/sam2) inferencing via our [Serverless Hosted API](/deploy/serverless-hosted-api-v2.md). SAM2 is a promptable visual segmentation model that accepts points and bounding boxes as prompts. We offer two SAM2 endpoints:

* [/sam2/embed\_image](#post-sam2-embed_image), which generates and caches an image embedding
* [/sam2/segment\_image](#post-sam2-segment_image), which returns instance segmentation masks for the given prompts

## Code sample

Call the `/sam2/segment_image` endpoint directly with `curl`:

```bash
curl --location 'https://serverless.roboflow.com/sam2/segment_image' \
  --header 'Content-Type: application/json' \
  --data '{
    "api_key": "YOUR_API_KEY",
    "image": {"type": "url", "value": "https://storage.googleapis.com/com-roboflow-marketing/notebooks/examples/bicycle.png"},
    "prompts": {"prompts": [{"points": [{"x": 300, "y": 250, "positive": true}]}]},
    "sam2_version_id": "hiera_tiny"
  }'
```

The same call through the SDK. Install the dependencies:

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

Below is a code sample that calls the segmentation endpoint with a single positive point prompt, decodes the returned polygons into a binary mask, and saves an annotated PNG with the mask drawn over the input image. Pass [Roboflow's API Key](https://app.roboflow.com/settings/api) via the `API_KEY` env variable.

```python
import os
import urllib.request

import cv2
import numpy as np
import supervision as sv
from inference_sdk import InferenceHTTPClient

IMAGE_URL = "https://storage.googleapis.com/com-roboflow-marketing/notebooks/examples/bicycle.png"
IMAGE_PATH = "bicycle.png"
OUTPUT_PATH = "bicycle_annotated.png"

urllib.request.urlretrieve(IMAGE_URL, IMAGE_PATH)
image = cv2.imread(IMAGE_PATH)
height, width = image.shape[:2]

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.getenv("API_KEY"),
)

result = client.sam2_segment_image(
    inference_input=IMAGE_PATH,
    prompts=[
        {"points": [{"x": 300, "y": 250, "positive": True}]}
    ],
    sam2_version_id="hiera_tiny",
)

masks, xyxys, confidences = [], [], []
for pred in result["predictions"]:
    contours = [np.array(poly, dtype=np.int32) for poly in pred.get("masks", []) if poly]
    if not contours:
        continue
    mask = np.zeros((height, width), dtype=np.uint8)
    cv2.fillPoly(mask, contours, 1)
    ys, xs = np.where(mask > 0)
    if xs.size == 0:
        continue
    masks.append(mask.astype(bool))
    xyxys.append([xs.min(), ys.min(), xs.max(), ys.max()])
    confidences.append(pred.get("confidence", 1.0))

detections = sv.Detections(
    xyxy=np.array(xyxys, dtype=float),
    mask=np.stack(masks, axis=0),
    class_id=np.zeros(len(masks), dtype=int),
    confidence=np.array(confidences, dtype=float),
)

annotated = sv.MaskAnnotator().annotate(scene=image.copy(), detections=detections)
cv2.imwrite(OUTPUT_PATH, annotated)
```

<figure><img src="/files/e8FqE2jVLGb1WOvVJQQJ" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Set `api_url` to match your deployment target:

* `https://serverless.roboflow.com` for the Serverless Hosted API.
* `http://localhost:9001` for a local [Inference](https://inference.roboflow.com/) server.
* Your [Dedicated Deployment](/deploy/dedicated-deployments.md) URL for a private endpoint.
  {% endhint %}

For additional usage details, including embedding caching and box prompts, see the [Inference documentation](https://inference.roboflow.com/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/deploy/supported-models/sam2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
