Segment Anything (SAM)

Run Meta's original Segment Anything (SAM) API on a Dedicated Deployment or self-hosted Inference

Segment Anything is Meta's original promptable image segmentation model. You give it a point (or a box) inside an object, and it returns a mask marking that object's precise boundary.

SAM works in two steps:

  1. Create an embedding for the image.
  2. Prompt the model with the coordinates of the object you want to segment.

Embeddings are cached by image_id, so once an image is embedded you can send many prompts against it without re-encoding.

For new projects, prefer SAM2 (faster, better masks, video support) or SAM3 (segments every instance of a concept from a text prompt). SAM v1 is documented here for existing integrations.

SAM is not available on the Serverless Cloud API. Run it on a Dedicated Deployment or self-hosted Inference.

SAM API

1

Get your API Key

Create a Roboflow account, find your key on the Roboflow API settings page and make it available to your shell:

export ROBOFLOW_API_KEY="your-key-here"
2

Install the dependencies

pip install requests
3

Embed an image

An embedding is a numeric representation of the image. SAM uses it to compute object locations. Set base_url to your Dedicated Deployment URL or a local Inference server.

import os
import requests

base_url = "http://localhost:9001"
api_key = os.environ["ROBOFLOW_API_KEY"]

payload = {
    "image": {"type": "url", "value": "https://media.roboflow.com/quickstart/traffic.jpg"},
    "image_id": "example_image_id",
}

response = requests.post(
    f"{base_url}/sam/embed_image?api_key={api_key}",
    json=payload,
)
embeddings = response.json()["embeddings"]

The image_id caches the embedding, so later segmentation requests for the same image do not have to send it again.

4

Segment an object

Prompt the model with at least one point that lies on the object. point_labels marks each point as positive (1, include) or negative (0, exclude).

payload = {
    "image": {"type": "url", "value": "https://media.roboflow.com/quickstart/traffic.jpg"},
    "point_coords": [[380, 350]],
    "point_labels": [1],
    "image_id": "example_image_id",
}

response = requests.post(
    f"{base_url}/sam/segment_image?api_key={api_key}",
    json=payload,
)
masks = response.json()["masks"]

The response contains segmentation masks for the object of interest.

To find point coordinates for a test image, upload it to the PolygonZone web tool and hover over the object. In a pipeline, a common pattern is to run an object detector first and use each box's center point as the SAM prompt.

Set base_url to match your deployment target:

Further reading