# Segment Anything Model

> SAM.


## Overview

- **Endpoint**: `https://fal.run/fal-ai/imageutils/sam`
- **Model ID**: `fal-ai/imageutils/sam`
- **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 to input image
  - Examples: "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.jpg"

- **`text_prompt`** (`string`, _optional_):
  The prompt to use when generating masks
  - Examples: "a photo of elephant"

- **`size`** (`integer`, _optional_):
  Image size Default value: `1024`
  - Default: `1024`

- **`iou`** (`float`, _optional_):
  IOU threshold for filtering the annotations Default value: `0.9`
  - Default: `0.9`

- **`retina`** (`boolean`, _optional_):
  Draw high-resolution segmentation masks Default value: `true`
  - Default: `true`

- **`confidence`** (`float`, _optional_):
  Object confidence threshold Default value: `0.4`
  - Default: `0.4`

- **`box_prompt`** (`list<array>`, _optional_):
  Coordinates for multiple boxes, e.g. [[x,y,w,h],[x2,y2,w2,h2]]
  - Default: `[[0,0,0,0]]`
  - Array of array

- **`point_prompt`** (`list<array>`, _optional_):
  Coordinates for multiple points [[x1,y1],[x2,y2]]
  - Default: `[[0,0]]`
  - Array of array

- **`point_label`** (`list<integer>`, _optional_):
  Label for point, [1,0], 0 = background, 1 = foreground
  - Default: `[0]`
  - Array of integer

- **`with_contours`** (`boolean`, _optional_):
  Draw the edges of the masks
  - Default: `false`

- **`better_quality`** (`boolean`, _optional_):
  Attempt better quality output using morphologyEx
  - Default: `false`

- **`black_white`** (`boolean`, _optional_):
  Output black and white, multiple masks will be combined into one mask
  - Default: `false`

- **`invert`** (`boolean`, _optional_):
  Invert mask colors
  - Default: `false`



**Required Parameters Example**:

```json
{
  "image_url": "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.jpg"
}
```

**Full Example**:

```json
{
  "image_url": "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.jpg",
  "text_prompt": "a photo of elephant",
  "size": 1024,
  "iou": 0.9,
  "retina": true,
  "confidence": 0.4,
  "box_prompt": [
    [
      0,
      0,
      0,
      0
    ]
  ],
  "point_prompt": [
    [
      0,
      0
    ]
  ],
  "point_label": [
    0
  ]
}
```


### Output Schema

The API returns the following output format:

- **`image`** (`Image`, _optional_):
  Combined image of all detected masks



**Example Response**:

```json
{}
```


## Usage Examples

### cURL

```bash
curl --request POST \
  --url https://fal.run/fal-ai/imageutils/sam \
  --header "Authorization: Key $FAL_KEY" \
  --header "Content-Type: application/json" \
  --data '{
     "image_url": "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.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/imageutils/sam",
    arguments={
        "image_url": "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.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/imageutils/sam", {
  input: {
    image_url: "https://storage.googleapis.com/falserverless/model_tests/remove_background/elephant.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/imageutils/sam)
- [API Documentation](https://fal.ai/models/fal-ai/imageutils/sam/api)
- [OpenAPI Schema](https://fal.ai/api/openapi/queue/openapi.json?endpoint_id=fal-ai/imageutils/sam)
- [GitHub Repository](https://github.com/CASIA-IVA-Lab/FastSAM/blob/main/LICENSE)

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