# detection

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

## Classes

### DetectionParser

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.

#### Methods

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

Parameters

 * `conf_threshold` (`float`): Confidence score threshold of detected bounding boxes.
 * `iou_threshold` (`float`): Non-maximum suppression threshold.
 * `max_det` (`int`): Maximum number of detections to keep.
 * `label_names` (`list[str] | None`): Optional class names for detected objects.

##### build

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

Configures the parser.

Parameters

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

Returns

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

##### 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); see that helper for tensor layout and validation details.

Parameters

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

Returns

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

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

Parameters

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

##### extract

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

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]`: A pair of boxes reshaped to `(N, 4)` and flattened scores. The box tensor is identified by its
   final dimension of four.

Raises

 * `ValueError`: If exactly two tensors are not available, or boxes and scores cannot be identified.

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

##### setIouThreshold

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

Sets the non-maximum suppression threshold.

Parameters

 * `threshold` (`float`): Non-maximum suppression threshold.

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

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

#### Attributes

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