# mediapipe_palm_detection

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

## Classes

### MPPalmDetectionParser

Parser class for parsing the output of the Mediapipe Palm detection model. As the result, the node sends out the detected hands in
the form of a message containing bounding boxes, labels, and confidence scores.

> **Note**
> Emits `dai.ImgDetections` messages. dai.ImgDetections message containing bounding boxes, labels, and confidence scores of detected hands.

See also:

Official MediaPipe Hands solution:
[https://ai.google.dev/edge/mediapipe/solutions/vision/hand_landmarker](https://ai.google.dev/edge/mediapipe/solutions/vision/hand_landmarker)

#### Methods

##### init

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

Initializes the parser node.

Parameters

 * `output_layer_names` (`list[str]`): Names of the output layers relevant to the parser.
 * `conf_threshold` (`float`): Confidence score threshold for detected hands.
 * `iou_threshold` (`float`): Non-maximum suppression threshold.
 * `max_det` (`int`): Maximum number of detections to keep.
 * `scale` (`int`): Scale of the input image.

##### build

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

Configures the parser.

Parameters

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

Returns

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

##### compute

```python
def compute(bboxes: np.ndarray, scores: np.ndarray, *, anchors: np.ndarray, conf_threshold: float, iou_threshold: float, max_det: int, scale: int, label_names: list[str] | None = None) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, list[str] | None]:
```

Compute parser results from extracted tensors without sending messages.

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

Parameters

 * `bboxes` (`np.ndarray`): Per-anchor palm box and landmark predictions.
 * `scores` (`np.ndarray`): Per-anchor palm score logits.
 * `anchors` (`np.ndarray`): Precomputed anchor coordinates used to decode model predictions.
 * `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.
 * `scale` (`int`): Side length of the square model input in pixels.
 * `label_names` (`list[str] | None`): Optional class-name lookup indexed by predicted class ID.

Returns

 * `tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, list[str] | None]`: Normalized center-XY/width/height boxes, confidence
   scores, angles in degrees, zero-valued class IDs, and optional class names.

##### emit

```python
def emit(output: dai.NNData, bboxes: np.ndarray, scores: np.ndarray, angles: np.ndarray, labels: np.ndarray, label_names: list[str] | 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.
 * `bboxes` (`np.ndarray`): Normalized center-XY/width/height boxes returned by `compute()`.
 * `scores` (`np.ndarray`): Confidence scores corresponding to the computed payload.
 * `angles` (`np.ndarray`): Rotation angles in degrees corresponding to the boxes.
 * `labels` (`np.ndarray`): Integer class IDs corresponding to the boxes.
 * `label_names` (`list[str] | None`): Optional class names corresponding to the detections.

##### 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]`: Palm box/landmark predictions reshaped to `(N, 18)` and flattened scores, selected by their
   final tensor dimensions.

Raises

 * `ValueError`: If no tensors are available or box tensors cannot be reshaped to 18 values per anchor.

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

##### setOutputLayerNames

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

Sets the output layer name(s) for the parser.

Parameters

 * `output_layer_names` (`list[str]`): The name of the output layer(s) from which the scores are extracted.

##### setScale

```python
def setScale(scale: int):
```

Sets the scale of the input image.

Parameters

 * `scale` (`int`): Scale of the input image.

#### Attributes

##### conf_threshold

Confidence score threshold for detected hands.

##### iou_threshold

Non-maximum suppression threshold.

##### label_names

##### max_det

Maximum number of detections to keep.

##### output_layer_names

Names of the output layers relevant to the parser.

##### scale

Scale of the input image.
