# rf_detr

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

## Classes

### RFDETRParser

Parser class for parsing the output of the RF-DETR object detection model.

RF-DETR from Roboflow is a detection transformer model that outputs bounding boxes and class probabilities. The model can
optionally output instance segmentation masks.

> **Note**
> Emits `dai.ImgDetections` messages. dai.ImgDetections message containing bounding boxes, labels, confidence scores, and optionally instance segmentation masks.

References:

RF-DETR: [https://github.com/roboflow/rf-detr](https://github.com/roboflow/rf-detr)

#### Methods

##### init

```python
def __init__(conf_threshold: float = 0.5, max_det: int = 300, label_names: list[str] | None = None, mask_conf: float = 0.5):
```

Initializes the parser node.

Parameters

 * `conf_threshold` (`float`): Confidence score threshold for detected objects.
 * `max_det` (`int`): Maximum number of detections to keep.
 * `label_names` (`list[str] | None`): List of label names for detected objects.
 * `mask_conf` (`float`): Mask confidence threshold for instance segmentation masks.

##### build

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

Configures the parser based on the head configuration.

Parameters

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

Returns

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

##### compute

```python
def compute(boxes_tensor: np.ndarray, logits_tensor: np.ndarray, *, conf_threshold: float, max_det: int, label_names: list[str] | None, mask_conf: float, input_shape: tuple[int, int] | None, masks_tensor: np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray, np.ndarray, list[str] | None, np.ndarray | None]:
```

Compute parser results from extracted tensors without sending messages.

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

Parameters

 * `boxes_tensor` (`np.ndarray`): Batched normalized center-XY/width/height predictions.
 * `logits_tensor` (`np.ndarray`): Class logits of shape `(1, queries, classes)`.
 * `conf_threshold` (`float`): Minimum detection confidence used to filter candidates.
 * `max_det` (`int`): Maximum number of detection candidates to retain or consider during suppression.
 * `label_names` (`list[str] | None`): Optional class-name lookup indexed by predicted class ID.
 * `mask_conf` (`float`): Probability threshold used to binarize mask logits.
 * `input_shape` (`tuple[int, int] | None`): Model input image shape as `(height, width)`.
 * `masks_tensor` (`np.ndarray | None`): Optional per-query mask logits, ordered like the box predictions.

Returns

 * `tuple[np.ndarray, np.ndarray, np.ndarray, list[str] | None, np.ndarray | None]`: Boxes in normalized center-XY/width/height
   format, scores, integer class IDs, optional class names, and an optional HW uint8 instance mask. Mask values index returned
   detections; 255 is background. Segmentation retains at most 255 instances, and higher-confidence masks win overlaps.

##### emit

```python
def emit(output: dai.NNData, boxes: np.ndarray, scores: np.ndarray, labels: np.ndarray, label_names_list: list[str] | None, final_mask: np.ndarray | None):
```

Create a `dai.ImgDetections` 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.
 * `boxes` (`np.ndarray`): Normalized center-XY/width/height boxes returned by `compute()`.
 * `scores` (`np.ndarray`): Confidence scores corresponding to the computed payload.
 * `labels` (`np.ndarray`): Integer class IDs corresponding to the boxes.
 * `label_names_list` (`list[str] | None`): Optional class names corresponding to the detections.
 * `final_mask` (`np.ndarray | None`): Optional instance mask whose IDs index the returned detections; 255 is background.

##### extract

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

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[np.ndarray, np.ndarray, np.ndarray | None]`: Float32 box, class-logit, and optional mask tensors in configured layer
   order. Without a mask layer, the third value is `None`.

Raises

 * `ValueError`: If the selected outputs do not contain two or three layers.

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

##### setConfidenceThreshold

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

Sets the confidence score threshold for detected objects.

Parameters

 * `threshold` (`float`): Confidence score threshold for detected objects.

##### setLabelNames

```python
def setLabelNames(label_names: list[str]):
```

Sets the label names for detected objects.

Parameters

 * `label_names` (`list[str]`): List of label names for detected objects.

##### setMaskConfidence

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

Sets the mask confidence threshold.

Parameters

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

##### setMaxDetections

```python
def setMaxDetections(max_det: int):
```

Sets the maximum number of detections to keep.

Parameters

 * `max_det` (`int`): Maximum number of detections to keep.

##### setOutputLayerNames

```python
def setOutputLayerNames(output_layer_names: list[str]):
```

Sets the output layer names for the parser.

Parameters

 * `output_layer_names` (`list[str]`): List of output layer names.

#### Attributes

##### conf_threshold

Confidence score threshold for detected objects.

##### input

Input port accepting `dai.NNData`.

##### input_shape

##### label_names

List of label names for detected objects.

##### mask_conf

Confidence threshold for binarizing instance segmentation masks.

##### max_det

Maximum number of detections to keep.

##### out

Output port carrying parsed messages.

##### output_layer_names

Names of the output layers (boxes, logits, and optionally masks).
