# fastsam

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

## Classes

### FastSAMParser

Parser class for parsing the output of the FastSAM model.

> **Note**
> Emits `dai.SegmentationMask` messages. dai.SegmentationMask message containing the resulting segmentation masks given the prompt.

#### Methods

##### init

```python
def __init__(conf_threshold: float = 0.5, n_classes: int = 1, iou_threshold: float = 0.5, mask_conf: float = 0.5, prompt: str = 'everything', points: tuple[int, int] | None = None, point_label: int | None = None, bbox: tuple[int, int, int, int] | None = None, yolo_outputs: list[str] = None, mask_outputs: list[str] = None, protos_output: str = 'protos_output'):
```

Initializes the parser node.

Parameters

 * `conf_threshold` (`float`): The confidence threshold for the detections
 * `n_classes` (`int`): The number of classes in the model
 * `iou_threshold` (`float`): The intersection over union threshold
 * `mask_conf` (`float`): The mask confidence threshold
 * `prompt` (`str`): The prompt type
 * `points` (`tuple[int, int] | None`): The points
 * `point_label` (`int | None`): The point label
 * `bbox` (`tuple[int, int, int, int] | None`): The bounding box
 * `yolo_outputs` (`list[str]`): The YOLO outputs
 * `mask_outputs` (`list[str]`): The mask outputs
 * `protos_output` (`str`): The protos output

##### build

```python
def build(head_config: dict[str, Any]) -> FastSAMParser:
```

Configures the parser.

Parameters

 * `head_config` (`dict[str, Any]`): The head configuration for the parser.

Returns

 * `FastSAMParser`: The parser object with the head configuration set.

##### compute

```python
def compute(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]:
```

Compute parser results from extracted tensors without sending messages.

> **Note**
> Uses [depthai_nodes.node.parsers.utils.fastsam.compute_fastsam_mask](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/fastsam.md); see that helper for tensor layout and validation details.

Parameters

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

Returns

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

##### emit

```python
def emit(output: dai.NNData, results_masks: np.ndarray, mask_count: int):
```

Create a `dai.SegmentationMask` message and send it on `out`.

Copies source timestamps and sequence number, and carries the source image transformation when present.

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.
 * `results_masks` (`np.ndarray`): Merged instance mask returned by `compute()`.
 * `mask_count` (`int`): Number of selected masks, used for logging.

##### extract

```python
def extract(output: dai.NNData) -> tuple[list[np.ndarray], list[np.ndarray], np.ndarray, int]:
```

Select and dequantize the model tensors needed for parsing.

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.

Returns

 * `tuple[list[np.ndarray], list[np.ndarray], np.ndarray, int]`: Detection heads in lexical layer-name order, mask coefficient
   heads, prototype tensor, and prototype channel count; arrays are dequantized float32 in NCHW order.

##### run

```python
def run(self):
```

Read queued network outputs, parse them, and emit results while running.

The pipeline invokes this processing loop. It exits when the input queue closes or the node stops.

##### setBoundingBox

```python
def setBoundingBox(bbox: tuple[int, int, int, int]):
```

Sets the bounding box.

Parameters

 * `bbox` (`tuple[int, int, int, int]`): The bounding box

##### setConfidenceThreshold

```python
def setConfidenceThreshold(threshold: float):
```

Sets the confidence score threshold.

Parameters

 * `threshold` (`float`): Confidence score threshold.

##### setIouThreshold

```python
def setIouThreshold(iou_threshold: float):
```

Sets the intersection over union threshold.

Parameters

 * `iou_threshold` (`float`): The intersection over union threshold.

##### setMaskConfidence

```python
def setMaskConfidence(mask_conf: float):
```

Sets the mask confidence threshold.

Parameters

 * `mask_conf` (`float`): The mask confidence threshold.

##### setMaskOutputs

```python
def setMaskOutputs(mask_outputs: list[str]):
```

Sets the mask outputs.

Parameters

 * `mask_outputs` (`list[str]`): The mask outputs

##### setNumClasses

```python
def setNumClasses(n_classes: int):
```

Sets the number of classes in the model.

Parameters

 * `n_classes` (`int`): The number of classes in the model.

##### setPointLabel

```python
def setPointLabel(point_label: int):
```

Sets the point label.

Parameters

 * `point_label` (`int`): The point label

##### setPoints

```python
def setPoints(points: tuple[int, int]):
```

Sets the points.

Parameters

 * `points` (`tuple[int, int]`): The points

##### setPrompt

```python
def setPrompt(prompt: str):
```

Sets the prompt type.

Parameters

 * `prompt` (`str`): The prompt type

##### setProtosOutput

```python
def setProtosOutput(protos_output: str):
```

Sets the protos output.

Parameters

 * `protos_output` (`str`): The protos output

##### setYoloOutputs

```python
def setYoloOutputs(yolo_outputs: list[str]):
```

Sets the YOLO outputs.

Parameters

 * `yolo_outputs` (`list[str]`): The YOLO outputs

#### Attributes

##### bbox

Bounding box.

##### conf_threshold

Confidence score threshold for detected objects.

##### iou_threshold

Non-maximum suppression threshold.

##### mask_conf

Mask confidence threshold.

##### mask_outputs

Names of the mask outputs.

##### n_classes

Number of classes in the model.

##### point_label

Point label.

##### points

Points.

##### prompt

Prompt type.

##### protos_output

Name of the protos output.

##### yolo_outputs

Names of the YOLO outputs.
