# Segment Anything Model 2

> SAM 2 is a model for segmenting images and videos in real-time.


## Overview

- **Endpoint**: `https://fal.run/fal-ai/sam2/image`
- **Model ID**: `fal-ai/sam2/image`
- **Category**: image-to-image
- **Kind**: inference
**Tags**: segmentation, mask, real-time



## Pricing

- **Price**: $0 per compute seconds

For more details, see [fal.ai pricing](https://fal.ai/pricing).

## API Information

This model can be used via our HTTP API or more conveniently via our client libraries.
See the input and output schema below, as well as the usage examples.


### Input Schema

The API accepts the following input parameters:


- **`image_url`** (`string`, _required_):
  URL of the image to be segmented
  - Examples: "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"

- **`prompts`** (`list<PointPrompt>`, _optional_):
  List of prompts to segment the image
  - Default: `[]`
  - Array of PointPrompt
  - Examples: [{"label":1,"x":500,"y":375}]

- **`box_prompts`** (`list<BoxPrompt>`, _optional_):
  Coordinates for boxes
  - Default: `[]`
  - Array of BoxPrompt
  - Examples: [{"x_max":700,"y_min":600,"x_min":425,"y_max":875}]

- **`apply_mask`** (`boolean`, _optional_):
  Apply the mask on the image.
  - Default: `false`

- **`sync_mode`** (`boolean`, _optional_):
  If `True`, the media will be returned as a data URI and the output data won't be available in the request history.
  - Default: `false`

- **`output_format`** (`OutputFormatEnum`, _optional_):
  The format of the generated image. Default value: `"png"`
  - Default: `"png"`
  - Options: `"jpeg"`, `"png"`, `"webp"`



**Required Parameters Example**:

```json
{
  "image_url": "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"
}
```

**Full Example**:

```json
{
  "image_url": "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg",
  "prompts": [
    {
      "label": 1,
      "x": 500,
      "y": 375
    }
  ],
  "box_prompts": [
    {
      "x_max": 700,
      "y_min": 600,
      "x_min": 425,
      "y_max": 875
    }
  ],
  "output_format": "png"
}
```


### Output Schema

The API returns the following output format:

- **`image`** (`Image`, _required_):
  Segmented image.



**Example Response**:

```json
{
  "image": {
    "url": "",
    "content_type": "image/png",
    "file_name": "z9RV14K95DvU.png",
    "file_size": 4404019,
    "width": 1024,
    "height": 1024
  }
}
```


## Usage Examples

### cURL

```bash
curl --request POST \
  --url https://fal.run/fal-ai/sam2/image \
  --header "Authorization: Key $FAL_KEY" \
  --header "Content-Type: application/json" \
  --data '{
     "image_url": "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"
   }'
```

### Python

Ensure you have the Python client installed:

```bash
pip install fal-client
```

Then use the API client to make requests:

```python
import fal_client

def on_queue_update(update):
    if isinstance(update, fal_client.InProgress):
        for log in update.logs:
           print(log["message"])

result = fal_client.subscribe(
    "fal-ai/sam2/image",
    arguments={
        "image_url": "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"
    },
    with_logs=True,
    on_queue_update=on_queue_update,
)
print(result)
```

### JavaScript

Ensure you have the JavaScript client installed:

```bash
npm install --save @fal-ai/client
```

Then use the API client to make requests:

```javascript
import { fal } from "@fal-ai/client";

const result = await fal.subscribe("fal-ai/sam2/image", {
  input: {
    image_url: "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"
  },
  logs: true,
  onQueueUpdate: (update) => {
    if (update.status === "IN_PROGRESS") {
      update.logs.map((log) => log.message).forEach(console.log);
    }
  },
});
console.log(result.data);
console.log(result.requestId);
```


## Additional Resources

### Documentation

- [Model Playground](https://fal.ai/models/fal-ai/sam2/image)
- [API Documentation](https://fal.ai/models/fal-ai/sam2/image/api)
- [OpenAPI Schema](https://fal.ai/api/openapi/queue/openapi.json?endpoint_id=fal-ai/sam2/image)

### fal.ai Platform

- [Platform Documentation](https://docs.fal.ai)
- [Python Client](https://docs.fal.ai/clients/python)
- [JavaScript Client](https://docs.fal.ai/clients/javascript)
