# fastsam

Python API: `depthai_nodes.node.parsers.utils.fastsam`

## Functions

| Name | Description |
| --- | --- |
| [`adjust_bboxes_to_image_border`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-adjust_bboxes_to_image_border) | Source: [https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L6](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L6) (Ultralytics) Adjust bounding boxes to stick to image border if they are within a certain threshold. |
| [`bbox_iou`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-bbox_iou) | Source: [https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L30](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L30) (Ultralytics - rewritten to numpy) Compute the Intersection-Over-Union of a bounding box with respect to an array of other bounding boxes. |
| [`box_prompt`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-box_prompt) | Modifies the bounding box properties and calculates IoU between masks and bounding box. |
| [`build_mask_coeffs`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-build_mask_coeffs) | Gather mask coefficients for all detections, grouped by head. |
| [`compute_fastsam_mask`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-compute_fastsam_mask) | Decode FastSAM outputs and apply the configured mask prompt. |
| [`decode_fastsam_output`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-decode_fastsam_output) | Decode the output of the FastSAM model. |
| [`format_results`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-format_results) | Formats detection results into list of annotations each containing ID, segmentation, bounding box, score and area. |
| [`merge_masks`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-merge_masks) | Merge masks to a 2D array where each object is represented by a unique label. |
| [`point_prompt`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-point_prompt) | Adjusts points on detected masks based on user input and returns the modified results. |
| [`process_masks`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md#fastsam-Functions-process_masks) | Process output into full-size masks for all detections. |

### function `adjust_bboxes_to_image_border`

```python
def adjust_bboxes_to_image_border(
    boxes: np.ndarray,
    image_shape: tuple[int, int],
    threshold: int = 20,
) -> np.ndarray
```

Source:
[https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L6](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L6)
(Ultralytics) Adjust bounding boxes to stick to image border if they are within a certain threshold.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `boxes` | `np.ndarray` | Bounding boxes | required |
| `image_shape` | `tuple[int, int]` | Image shape | required |
| `threshold` | `int` | Pixel threshold | `20` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Adjusted bounding boxes |

### function `bbox_iou`

```python
def bbox_iou(
    box1: np.ndarray,
    boxes: np.ndarray,
    iou_thres: float = 0.9,
    image_shape: tuple[int, int] = (640, 640),
    raw_output: bool = False,
) -> np.ndarray
```

Source:
[https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L30](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L30)
(Ultralytics - rewritten to numpy) Compute the Intersection-Over-Union of a bounding box with respect to an array of other
bounding boxes.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `box1` | `np.ndarray` | Array of shape (4, ) representing a single bounding box. | required |
| `boxes` | `np.ndarray` | Array of shape (n, 4) representing multiple bounding boxes. | required |
| `iou_thres` | `float` | IoU threshold | `0.9` |
| `image_shape` | `tuple[int, int]` | Image shape (height, width) | `(640, 640)` |
| `raw_output` | `bool` | If True, return the raw IoU values instead of the indices | `False` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Indices of boxes with IoU > thres, or the raw IoU values if raw_output is True |

### function `box_prompt`

```python
def box_prompt(
    masks: np.ndarray,
    bbox: tuple[int, int, int, int],
    orig_shape: tuple[int, int],
) -> np.ndarray
```

Modifies the bounding box properties and calculates IoU between masks and bounding box.

Source:
[https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L284](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L284)
Modified so it uses numpy instead of torch.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `masks` | `np.ndarray` | The resulting masks of the FastSAM model | required |
| `bbox` | `tuple[int, int, int, int]` | The prompt bounding box coordinates | required |
| `orig_shape` | `tuple[int, int]` | The original shape of the image | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | The modified masks |

### function `build_mask_coeffs`

```python
def build_mask_coeffs(
    parsed_results: np.ndarray,
    masks_outputs_values: list[np.ndarray],
    protos_len: int,
) -> np.ndarray
```

Gather mask coefficients for all detections, grouped by head.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `parsed_results` | `np.ndarray` | FastSAM decoded outputs | required |
| `masks_outputs_values` | `list[np.ndarray]` | Model mask outputs | required |
| `protos_len` | `int` | Number of protos | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` |  |

### function `compute_fastsam_mask`

```python
def compute_fastsam_mask(
    outputs_values: list[np.ndarray],
    masks_outputs_values: list[np.ndarray],
    protos_output: np.ndarray,
    protos_len: int,
    *,
    conf_threshold: float,
    n_classes: int,
    iou_threshold: float,
    mask_conf: float,
    prompt: str,
    points: tuple[int, int] | None,
    point_label: int | None,
    bbox: tuple[int, int, int, int] | None,
) -> tuple[np.ndarray, int]
```

Decode FastSAM outputs and apply the configured mask prompt.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `outputs_values` | `list[np.ndarray]` | Detection tensors ordered by output head. | required |
| `masks_outputs_values` | `list[np.ndarray]` | Mask coefficient tensors ordered to match the detection heads. | required |
| `protos_output` | `np.ndarray` | Batched prototype tensor with shape `(1, channels, height, width)`. | required |
| `protos_len` | `int` | Number of prototype channels used by each mask coefficient vector. | required |
| `conf_threshold` | `float` | Minimum detection confidence used to filter candidates. | required |
| `n_classes` | `int` | Number of object classes encoded in the detection tensors. | required |
| `iou_threshold` | `float` | Intersection-over-union threshold for non-maximum suppression. | required |
| `mask_conf` | `float` | Probability threshold used to binarize mask logits. | required |
| `prompt` | `str` | Mask selection mode: `"everything"`, `"bbox"`, or `"point"`. | required |
| `points` | `tuple[int, int] | None` | Prompt point in image pixel coordinates for point selection. | required |
| `point_label` | `int | None` | Point-prompt label used to include or exclude matching masks. | required |
| `bbox` | `tuple[int, int, int, int] | None` | Bounding-box prompt in image pixel coordinates. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[np.ndarray, int]` | A pair of the merged instance mask and the number of selected masks. No selected masks produces a background mask with value -1 and count 0. |

### function `decode_fastsam_output`

```python
def decode_fastsam_output(
    outputs: list[np.ndarray],
    strides: list[int],
    anchors: list[np.ndarray | None],
    img_shape: tuple[int, int],
    conf_thres: float = 0.5,
    iou_thres: float = 0.45,
    num_classes: int = 1,
) -> np.ndarray
```

Decode the output of the FastSAM model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `outputs` | `list[np.ndarray]` | List of FastSAM outputs | required |
| `strides` | `list[int]` | List of strides | required |
| `anchors` | `list[np.ndarray | None]` | List of anchors | required |
| `img_shape` | `tuple[int, int]` | Image shape | required |
| `conf_thres` | `float` | Confidence threshold | `0.5` |
| `iou_thres` | `float` | IoU threshold | `0.45` |
| `num_classes` | `int` | Number of classes | `1` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | NMS output |

### function `format_results`

```python
def format_results(
    bboxes: np.ndarray,
    masks: np.ndarray,
    filter: int = 0,
) -> list[dict[str, Any]]
```

Formats detection results into list of annotations each containing ID, segmentation, bounding box, score and area.

Source:
[https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L59](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L59)

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bboxes` | `np.ndarray` | The bounding boxes of the detected objects | required |
| `masks` | `np.ndarray` | The masks of the detected objects | required |
| `filter` | `int` | The filter value | `0` |

Returns

| Type | Description |
| --- | --- |
| `list[dict[str, Any]]` | The formatted annotations |

### function `merge_masks`

```python
def merge_masks(masks: np.ndarray) -> np.ndarray
```

Merge masks to a 2D array where each object is represented by a unique label.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `masks` | `np.ndarray` | 3D array of masks | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | 2D array of masks |

### function `point_prompt`

```python
def point_prompt(
    bboxes: np.ndarray,
    masks: np.ndarray,
    points: list[tuple[int, int]],
    pointlabel: list[int],
    orig_shape: tuple[int, int],
) -> np.ndarray
```

Adjusts points on detected masks based on user input and returns the modified results.

Source:
[https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L317](https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L317)
Modified so it uses numpy instead of torch.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bboxes` | `np.ndarray` | The bounding boxes of the detected objects | required |
| `masks` | `np.ndarray` | The masks of the detected objects | required |
| `points` | `list[tuple[int, int]]` | The points to adjust | required |
| `pointlabel` | `list[int]` | The point labels | required |
| `orig_shape` | `tuple[int, int]` | The original shape of the image | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | The modified masks |

### function `process_masks`

```python
def process_masks(
    parsed_results: np.ndarray,
    mask_coeffs: np.ndarray,
    protos: np.ndarray,
    orig_shape: tuple[int, int],
    mask_conf: float,
) -> np.ndarray
```

Process output into full-size masks for all detections.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `parsed_results` | `np.ndarray` | FastSAM decoded outputs | required |
| `mask_coeffs` | `np.ndarray` | Mask coefficients | required |
| `protos` | `np.ndarray` | Protos from model output | required |
| `orig_shape` | `tuple[int, int]` | Input shape of the model | required |
| `mask_conf` | `float` | Mask confidence | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` |  |
