# keypoints

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

## Classes

| Name | Description |
| --- | --- |
| [`KeypointParser`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser) | Parser class for 2D or 3D keypoints models. It expects one output layer containing keypoints. The number of keypoints must be specified. Moreover, the keypoints are normalized by a scale factor if provided. |

### class `KeypointParser`

```python
class KeypointParser(BaseParser)
```

Parser class for 2D or 3D keypoints models. It expects one output layer containing keypoints. The number of keypoints must be
specified. Moreover, the keypoints are normalized by a scale factor if provided.

> **Note**
> Emits `dai.beta.Keypoints` messages. Output containing 2D or 3D keypoints.

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the number of keypoints is not specified. |
| `ValueError` | If the number of coordinates per keypoint is not 2 or 3. |
| `ValueError` | If the number of output layers is not 1. |

Attributes

| Name | Description |
| --- | --- |
| `edges` | Pairs of keypoint indexes defining skeleton edges. For example, `[(0, 1), (1, 2)]` connects keypoint 0 to 1 and 1 to 2. |
| `label_names` | Label names for the keypoints. |
| `n_keypoints` | Number of keypoints the model detects. |
| `output_layer_name` | Name of the output layer relevant to the parser. |
| `scale_factor` | Scale factor to divide the keypoints by. |
| `score_threshold` | Confidence score threshold for detected keypoints. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-__init__) | Initializes the parser node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-build) | Configures the parser. |
| [`compute`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-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/keypoints.md#keypoints-Classes-KeypointParser-emit) | Create a `dai.beta.Keypoints` message and send it on `out`. |
| [`extract`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-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/keypoints.md#keypoints-Classes-KeypointParser-run) | Read queued network outputs, parse them, and emit results while running. |
| [`setEdges`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setEdges) | Sets the edges for the keypoints. |
| [`setLabelNames`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setLabelNames) | Sets the label names for the keypoints. |
| [`setNumKeypoints`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setNumKeypoints) | Sets the number of keypoints. |
| [`setOutputLayerName`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setOutputLayerName) | Sets the name of the output layer. |
| [`setScaleFactor`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setScaleFactor) | Sets the scale factor to divide the keypoints by. |
| [`setScoreThreshold`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/keypoints.md#keypoints-Classes-KeypointParser-setScoreThreshold) | Sets the confidence score threshold for the detected body keypoints. |

#### method `__init__`

```python
def __init__(
    output_layer_name: str = '',
    scale_factor: float = 1.0,
    n_keypoints: int = None,
    score_threshold: float = None,
    label_names: list[str] | None = None,
    edges: list[list[int]] | None = None,
)
```

Initializes the parser node.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | Name of the output layer relevant to the parser. | `''` |
| `scale_factor` | `float` | Scale factor to divide the keypoints by. | `1.0` |
| `n_keypoints` | `int` | Number of keypoints. | `None` |
| `score_threshold` | `float` | Optional confidence threshold stored in the parser configuration. | `None` |
| `label_names` | `list[str] | None` | Label names for the keypoints. | `None` |
| `edges` | `list[list[int]] | None` | Pairs of keypoint indexes defining skeleton edges. For example, `[(0, 1), (1, 2)]` connects keypoint 0 to 1 and 1 to 2. | `None` |

#### method `build`

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

Configures the parser.

Args

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

Returns

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

#### static method `compute`

```python
def compute(
    keypoints: np.ndarray,
    *,
    n_keypoints: int,
    scale_factor: float = 1.0,
) -> np.ndarray
```

Compute parser results from extracted tensors without sending messages.

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

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `keypoints` | `np.ndarray` | Model keypoint tensor. | required |
| `n_keypoints` | `int` | Number of keypoints encoded per prediction. | required |
| `scale_factor` | `float` | Nonzero divisor used to convert model coordinates to normalized coordinates. | `1.0` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Float32 coordinates of shape `(n_keypoints, 2)` or `(n_keypoints, 3)`, divided by `scale_factor` and clipped to [0, 1]. |

#### method `emit`

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

Create a `dai.beta.Keypoints` 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 |
| `keypoints` | `np.ndarray` | Normalized keypoint coordinates returned by `compute()`. | required |

#### method `extract`

```python
def extract(output: dai.NNData) -> 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 |
| --- | --- |
| `np.ndarray` | Dequantized float32 keypoint tensor. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If no output name is configured and the message does not contain exactly one layer, or configured class requirements are not met. |

#### 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 `setEdges`

```python
def setEdges(edges: list[tuple[int, int]])
```

Sets the edges for the keypoints.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `edges` | `list[tuple[int, int]]` | List of edges for the keypoints. Example: [(0,1), (1,2), (2,3), (3,0)] shows that keypoint 0 is connected to keypoint 1, keypoint 1 is connected to keypoint 2, etc. | required |

#### method `setLabelNames`

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

Sets the label names for the keypoints.

Args

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

#### method `setNumKeypoints`

```python
def setNumKeypoints(n_keypoints: int)
```

Sets the number of keypoints.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `n_keypoints` | `int` | Number of keypoints. | required |

#### method `setOutputLayerName`

```python
def setOutputLayerName(output_layer_name: str)
```

Sets the name of the output layer.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | The name of the output layer. | required |

#### method `setScaleFactor`

```python
def setScaleFactor(scale_factor: float)
```

Sets the scale factor to divide the keypoints by.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `scale_factor` | `float` | Scale factor to divide the keypoints by. | required |

#### method `setScoreThreshold`

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

Sets the confidence score threshold for the detected body keypoints.

Args

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