# mlsd

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

## Classes

### MLSDParser

Parser class for parsing the output of the M-LSD line detection model. The parser is specifically designed to parse the output of
the M-LSD model. As the result, the node sends out the detected lines in the form of a message.

> **Note**
> Emits `dai.beta.Lines` messages. Native message containing detected lines and confidence scores.

#### Methods

##### init

```python
def __init__(output_layer_tpmap: str = '', output_layer_heat: str = '', topk_n: int = 200, score_thr: float = 0.1, dist_thr: float = 20.0):
```

Initializes the parser node.

Parameters

 * `output_layer_tpmap` (`str`): Name of the output tensor containing line displacements.
 * `output_layer_heat` (`str`): Name of the output tensor containing the heatmap.
 * `topk_n` (`int`): Number of top candidates to keep.
 * `score_thr` (`float`): Confidence score threshold for detected lines.
 * `dist_thr` (`float`): Minimum line length in output-map pixels.

##### build

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

Configures the parser.

Parameters

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

Returns

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

##### compute

```python
def compute(tpMap: np.ndarray, heat_np: np.ndarray, *, topk_n: int, score_thr: float, dist_thr: float) -> tuple[np.ndarray, np.ndarray]:
```

Compute parser results from extracted tensors without sending messages.

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

Parameters

 * `tpMap` (`np.ndarray`): Four-dimensional line-displacement tensor in NCHW layout.
 * `heat_np` (`np.ndarray`): Heat tensor used to rank line-center candidates.
 * `topk_n` (`int`): Maximum number of line-center candidates to examine.
 * `score_thr` (`float`): Minimum candidate score.
 * `dist_thr` (`float`): Minimum line length in output-map pixels.

Returns

 * `tuple[np.ndarray, np.ndarray]`: Normalized endpoint coordinates of shape `(N, 4)` and float32 line scores.

##### emit

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

Create a `dai.beta.Lines` 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.
 * `lines` (`np.ndarray`): Normalized `[x1, y1, x2, y2]` line endpoints.
 * `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]`: Float32 displacement and heat tensors. Displacement is requested in NCHW order.

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

##### setDistanceThreshold

```python
def setDistanceThreshold(dist_thr: float):
```

Sets the distance threshold for merging lines.

Parameters

 * `dist_thr` (`float`): Minimum line length in output-map pixels.

##### setOutputLayerHeat

```python
def setOutputLayerHeat(output_layer_heat: str):
```

Sets the name of the output layer containing the heat tensor.

Parameters

 * `output_layer_heat` (`str`): Name of the output layer containing the heat tensor.

##### setOutputLayerTPMap

```python
def setOutputLayerTPMap(output_layer_tpmap: str):
```

Sets the name of the output layer containing the tpMap tensor.

Parameters

 * `output_layer_tpmap` (`str`): Name of the output layer containing the tpMap tensor.

##### setScoreThreshold

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

Sets the confidence score threshold for detected lines.

Parameters

 * `score_thr` (`float`): Confidence score threshold for detected lines.

##### setTopK

```python
def setTopK(topk_n: int):
```

Sets the number of top candidates to keep.

Parameters

 * `topk_n` (`int`): Number of top candidates to keep.

#### Attributes

##### dist_thr

Minimum line length in output-map pixels.

##### output_layer_heat

Name of the output layer containing the heat tensor.

##### output_layer_tpmap

Name of the output layer containing the tpMap tensor.

##### score_thr

Confidence score threshold for detected lines.

##### topk_n

Number of top candidates to keep.
