# Segment Anything Model 2

> SAM 2 is a model for segmenting images automatically. It can return individual masks or a single mask for the entire image.


## Overview

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



## 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 automatically segmented
  - Examples: "https://raw.githubusercontent.com/facebookresearch/segment-anything-2/main/notebooks/images/truck.jpg"

- **`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"`

- **`points_per_side`** (`integer`, _optional_):
  Number of points to sample along each side of the image. Default value: `32`
  - Default: `32`

- **`pred_iou_thresh`** (`float`, _optional_):
  Threshold for predicted IOU score. Default value: `0.88`
  - Default: `0.88`

- **`stability_score_thresh`** (`float`, _optional_):
  Threshold for stability score. Default value: `0.95`
  - Default: `0.95`

- **`min_mask_region_area`** (`integer`, _optional_):
  Minimum area of a mask region. Default value: `100`
  - Default: `100`



**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",
  "output_format": "png",
  "points_per_side": 32,
  "pred_iou_thresh": 0.88,
  "stability_score_thresh": 0.95,
  "min_mask_region_area": 100
}
```


### Output Schema

The API returns the following output format:

- **`combined_mask`** (`Image`, _required_):
  Combined segmentation mask.

- **`individual_masks`** (`list<Image>`, _required_):
  Individual segmentation masks.
  - Array of Image



**Example Response**:

```json
{
  "combined_mask": {
    "url": "",
    "content_type": "image/png",
    "file_name": "z9RV14K95DvU.png",
    "file_size": 4404019,
    "width": 1024,
    "height": 1024
  },
  "individual_masks": [
    {
      "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/auto-segment \
  --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/auto-segment",
    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/auto-segment", {
  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/auto-segment)
- [API Documentation](https://fal.ai/models/fal-ai/sam2/auto-segment/api)
- [OpenAPI Schema](https://fal.ai/api/openapi/queue/openapi.json?endpoint_id=fal-ai/sam2/auto-segment)

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