# detection

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

## Classes

| Name | Description |
| --- | --- |
| [`DetectionParser`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser) | Parse bounding boxes and scores from a detection model. |

### class `DetectionParser`

```python
class DetectionParser(BaseParser)
```

Parse bounding boxes and scores from a detection model.

The model must produce bounding boxes of shape `(N, 4)` in `[xmin, ymin, xmax, ymax]` format and scores of shape `(N,)`. The
output is a `dai.ImgDetections` message containing the detected objects and their confidence scores.

Attributes

| Name | Description |
| --- | --- |
| `conf_threshold` | Minimum confidence score for detections. |
| `iou_threshold` | Intersection-over-union threshold for non-maximum suppression. |
| `label_names` | Optional class names for detected objects. |
| `max_det` | Maximum number of detections to keep. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-__init__) | Initializes the parser node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-build) | Configures the parser. |
| [`compute`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-compute) | Compute parser results from extracted tensors without sending messages. |
| [`emit`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-emit) | Create a `dai.ImgDetections` message and send it on `out`. |
| [`extract`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-extract) | Select and dequantize the model tensors needed for parsing. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-run) | Read queued network outputs, parse them, and emit results while running. |
| [`setConfidenceThreshold`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-setConfidenceThreshold) | Sets the confidence score threshold for detected objects. |
| [`setIouThreshold`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-setIouThreshold) | Sets the non-maximum suppression threshold. |
| [`setLabelNames`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-setLabelNames) | Sets the label names for detected objects. |
| [`setMaxDetections`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/detection.md#detection-Classes-DetectionParser-setMaxDetections) | Sets the maximum number of detections to keep. |

#### method `__init__`

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

Initializes the parser node.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `conf_threshold` | `float` | Confidence score threshold of detected bounding boxes. | `0.5` |
| `iou_threshold` | `float` | Non-maximum suppression threshold. | `0.5` |
| `max_det` | `int` | Maximum number of detections to keep. | `100` |
| `label_names` | `list[str] | None` | Optional class names for detected objects. | `None` |

#### method `build`

```python
def build(head_config) -> DetectionParser
```

Configures the parser.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `head_config` | `dict[str, Any]` | The head configuration for the parser. | required |

Returns

| Type | Description |
| --- | --- |
| `DetectionParser` | The parser object with the head configuration set. |

#### static method `compute`

```python
def compute(
    bboxes: np.ndarray,
    scores: np.ndarray,
    *,
    conf_threshold: float,
    iou_threshold: float,
    max_det: int,
) -> tuple[np.ndarray, np.ndarray]
```

Compute parser results from extracted tensors without sending messages.

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

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bboxes` | `np.ndarray` | Bounding boxes of shape `(N, 4)` in `[xmin, ymin, xmax, ymax]` format. | required |
| `scores` | `np.ndarray` | Confidence scores of shape `(N,)`. | required |
| `conf_threshold` | `float` | Minimum detection confidence used to filter candidates. | required |
| `iou_threshold` | `float` | Intersection-over-union threshold for non-maximum suppression. | required |
| `max_det` | `int` | Maximum number of detection candidates to retain or consider during suppression. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[np.ndarray, np.ndarray]` | Retained center-XY/width/height boxes and corresponding scores. Coordinates retain their input units. Both arrays are empty if no boxes survive. |

#### method `emit`

```python
def emit(output: dai.NNData, bboxes: np.ndarray, scores: np.ndarray)
```

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

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

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |
| `bboxes` | `np.ndarray` | Normalized center-XY/width/height boxes returned by `compute()`. | required |
| `scores` | `np.ndarray` | Confidence scores corresponding to the computed payload. | required |

#### method `extract`

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

Select and dequantize the model tensors needed for parsing.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[np.ndarray, np.ndarray]` | A pair of boxes reshaped to `(N, 4)` and flattened scores. The box tensor is identified by its final dimension of four. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If exactly two tensors are not available, or boxes and scores cannot be identified. |

#### method `run`

```python
def run()
```

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.

#### method `setConfidenceThreshold`

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

Sets the confidence score threshold for detected objects.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `threshold` | `float` | Confidence score threshold for detected objects. | required |

#### method `setIouThreshold`

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

Sets the non-maximum suppression threshold.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `threshold` | `float` | Non-maximum suppression threshold. | required |

#### method `setLabelNames`

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

Sets the label names for detected objects.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `label_names` | `list[str]` | List of label names for detected objects. | required |

#### method `setMaxDetections`

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

Sets the maximum number of detections to keep.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `max_det` | `int` | Maximum number of detections to keep. | required |
