SAM2

Use Meta's SAM2 model through our Serverless Hosted API

We support Meta's Segment Anything Model 2 inferencing via our Serverless Hosted API. SAM2 is a promptable visual segmentation model that accepts points and bounding boxes as prompts. We offer two SAM2 endpoints:

Code sample

Call the /sam2/segment_image endpoint directly with curl:

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:

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 via the API_KEY env variable.

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)

Set api_url to match your deployment target:

  • https://serverless.roboflow.com for the Serverless Hosted API.

  • http://localhost:9001 for a local Inference server.

  • Your Dedicated Deployment URL for a private endpoint.

For additional usage details, including embedding caching and box prompts, see the Inference documentation.

Last updated

Was this helpful?