# detection

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

## Classes

### DetectionParser

从检测模型中解析边界框和分数。

模型必须生成形状为 `(N, 4)` 的边界框，格式为 `[xmin, ymin, xmax, ymax]`，以及形状为 `(N,)` 的分数。输出是一个 `dai.ImgDetections` 消息，其中包含检测到的对象及其置信度分数。

#### 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):
```

初始化解析器节点。

Parameters

 * `conf_threshold` (`float`): 检测到的边界框的置信度分数阈值。
 * `iou_threshold` (`float`): 非极大值抑制阈值。
 * `max_det` (`int`): 要保留的最大检测数量。
 * `label_names` (`list[str] | None`): 检测到的对象的可选类名。

##### build

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

配置解析器。

Parameters

 * `head_config` (`dict[str, Any]`): 解析器的头部配置。

Returns

 * `DetectionParser`: 设置了头部配置的解析器对象。

##### compute

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

在不发送消息的情况下，从提取的张量计算解析器结果。

> **Note**
> 使用 [depthai_nodes.node.parsers.utils.detection.compute_detection_outputs](/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/detection.md)；有关张量布局和验证的详细信息，请参阅该帮助程序。

Parameters

 * `bboxes` (`np.ndarray`): 形状为 `(N, 4)` 的边界框，格式为 `[xmin, ymin, xmax, ymax]`。
 * `scores` (`np.ndarray`): 形状为 `(N,)` 的置信度分数。
 * `conf_threshold` (`float`): 用于过滤候选的最小检测置信度。
 * `iou_threshold` (`float`): 用于非极大值抑制的交并比阈值。
 * `max_det` (`int`): 在抑制期间保留或考虑的最大检测候选数量。

Returns

 * `tuple[np.ndarray, np.ndarray]`: 保留的中心-XY/宽度/高度框和相应的分数。坐标保留其输入单位。如果没有任何框存活，则两个数组都为空。

##### emit

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

创建 `dai.ImgDetections` 消息并将其发送到 `out`。

复制源时间戳和序列号，并在存在时携带源图像转换。

Parameters

 * `output` (`dai.NNData`): 携带张量和源时间戳、序列号以及可选图像转换的神经网络输出。
 * `bboxes` (`np.ndarray`): `compute()` 返回的归一化中心-XY/宽度/高度框。
 * `scores` (`np.ndarray`): 与计算负载对应的置信度分数。

##### extract

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

选择并反量化解析所需的模型张量。

Parameters

 * `output` (`dai.NNData`): 携带张量和源时间戳、序列号以及可选图像转换的神经网络输出。

Returns

 * `tuple[np.ndarray, np.ndarray]`: 一对重塑为 `(N, 4)` 的框和展平的分数。框张量由其最终维度（四个）标识。

Raises

 * `ValueError`: 如果没有正好两个张量可用，或者无法识别框和分数。

##### run

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

读取排队的网络输出，解析它们，并在运行时发出结果。

管道调用此处理循环。当输入队列关闭或节点停止时，它将退出。

##### setConfidenceThreshold

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

设置检测到的对象的置信度分数阈值。

Parameters

 * `threshold` (`float`): 检测到的对象的置信度分数阈值。

##### setIouThreshold

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

设置非极大值抑制阈值。

Parameters

 * `threshold` (`float`): 非极大值抑制阈值。

##### setLabelNames

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

设置检测到的对象的标签名称。

Parameters

 * `label_names` (`list[str]`): 检测到的对象的标签名称列表。

##### setMaxDetections

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

设置要保留的最大检测数量。

Parameters

 * `max_det` (`int`): 要保留的最大检测数量。

#### Attributes

##### conf_threshold

检测的最小置信度分数。

##### iou_threshold

用于非极大值抑制的交并比阈值。

##### label_names

检测到的对象的可选类名。

##### max_det

要保留的最大检测数量。
