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

# Segment Anything (SAM)

[Segment Anything](https://github.com/facebookresearch/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.

{% hint style="info" %}
For new projects, prefer [SAM2](/models/supported-models/sam2.md) (faster, better masks, video support) or [SAM3](/models/supported-models/sam3.md) (segments every instance of a concept from a text prompt). SAM v1 is documented here for existing integrations.
{% endhint %}

{% hint style="info" %}
SAM is not available on the Serverless Cloud API. Run it on a [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) or [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## Code sample

{% stepper %}
{% step %}

### Get your API Key

Create a Roboflow account, find your key on the [Roboflow API settings page](https://app.roboflow.com/settings/api) and make it available to your shell:

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

{% endstep %}

{% step %}

### Install the dependencies

```bash
pip install requests
```

{% endstep %}

{% step %}

### 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.

```python
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.
{% endstep %}

{% step %}

### 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).

```python
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.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
To find point coordinates for a test image, upload it to the [PolygonZone web tool](https://roboflow.github.io/polygonzone/) 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.
{% endhint %}

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

* `http://localhost:9001` for a local [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) server.
* Your [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) URL for a private endpoint.
  {% endhint %}

## Further reading

* [What is Segment Anything Model (SAM)?](https://blog.roboflow.com/segment-anything-breakdown/)
* [SAM2](/models/supported-models/sam2.md) and [SAM3](/models/supported-models/sam3.md), the current generations of the model.
