# utils

Python API: `modelconverter.cli.utils`

Helpers shared by the `modelconverter` CLI commands.

Turns what the user typed on the command line into what the platform packages expect: the directory a run writes its results to,
the parsed configuration (from a config file or an NN Archive), and the preprocessing that is handed to the NN Archive instead of
being baked into the model.

## Functions

| Name | Description |
| --- | --- |
| [`display_output_path`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-display_output_path) | Return the host-visible representation of an output path when available. |
| [`extract_preprocessing`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-extract_preprocessing) | Move the preprocessing out of the config into archive blocks. |
| [`get_configs`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-get_configs) | Set up the configuration. |
| [`get_output_dir_name`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-get_output_dir_name) | Determine the directory the conversion writes its results to. |
| [`init_dirs`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-init_dirs) | Create the directories a conversion reads from and writes to. |
| [`resolve_output_dir`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/cli/utils.md#utils-Functions-resolve_output_dir) | Resolve a user-provided `--output-dir` under [OUTPUTS_DIR](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/constants.md#constants-Module-Attributes-OUTPUTS_DIR). |

### function `display_output_path`

```python
def display_output_path(path: Path) -> str
```

Return the host-visible representation of an output path when available.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `path` | `Path` | Artifact path produced by the current conversion environment. | required |

Returns

| Type | Description |
| --- | --- |
| `str` | Host-visible path for a Docker output when mapping context is available, otherwise the original path representation. |

### function `extract_preprocessing`

```python
def extract_preprocessing(cfg: Config) -> tuple[Config, dict[str, PreprocessingBlock]]
```

Move the preprocessing out of the config into archive blocks.

Mean values, scale values and the color encoding are cleared on every input of the config -- so they are not baked into the
converted model -- and returned as `PreprocessingBlock` objects to be stored in the NN Archive instead. A raw input only gets a
block when it has mean or scale values.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cfg` | `Config` | Single-stage config to take the preprocessing from. Its inputs are modified in place. | required |

Returns

| Type | Description |
| --- | --- |
| `tuple[Config, dict[str, PreprocessingBlock]]` | Tuple of the modified config and the preprocessing blocks keyed by input name. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the config has more than one stage. |

### function `get_configs`

```python
def get_configs(
    platform: Platform,
    path: str | None,
    opts: list[str] | Params | None = None,
) -> tuple[Config, NNArchiveConfig | None, str | None]
```

Set up the configuration.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | Platform the config is built for. | required |
| `path` | `str | None` | Path to the configuration file or NN Archive. If `None`, the config is built from the overrides alone. | required |
| `opts` | `list[str] | Params | None` | Optional CLI overrides of the config file. Either a mapping, or a flat list alternating keys and values. | `None` |

Returns

| Type | Description |
| --- | --- |
| `tuple[Config, NNArchiveConfig | None, str | None]` | Tuple of the parsed modelconverter [Config](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/config.md#config-Classes-Config), the `NNArchiveConfig` if the input was an NN Archive and `None` otherwise, and the key of the main stage -- `None` for a multi-stage config in which no main stage was recognized. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `opts` is a list of odd length. |

### function `get_output_dir_name`

```python
def get_output_dir_name(platform: Platform, name: str, output_dir: str | None) -> Path
```

Determine the directory the conversion writes its results to.

An existing destination is cleared first, but only when it is a directory that a previous conversion produced -- one holding a
[CONVERSION_MARKER](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/constants.md#constants-Module-Attributes-CONVERSION_MARKER)
file -- or an empty one.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `platform` | `Platform` | Platform the model is converted for, used in the generated name. | required |
| `name` | `str` | Name of the model, sanitized before use. | required |
| `output_dir` | `str | None` | Directory named by the user, resolved under [OUTPUTS_DIR](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/constants.md#constants-Module-Attributes-OUTPUTS_DIR). If `None`, a directory named `<name>_to_<platform>_<timestamp>` is used instead. | required |

Returns

| Type | Description |
| --- | --- |
| `Path` | Path to the output directory. It is not created here. |

Raises

| Type | Description |
| --- | --- |
| `ModelconverterException` | If, in a containerized run, `output_dir` is empty, absolute or contains `..`; or if the destination exists but is not a directory, or is a non-empty directory that does not hold the results of a previous conversion. |

### function `init_dirs`

```python
def init_dirs()
```

Create the directories a conversion reads from and writes to.

### function `resolve_output_dir`

```python
def resolve_output_dir(output_dir: str) -> Path
```

Resolve a user-provided `--output-dir` under
[OUTPUTS_DIR](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/constants.md#constants-Module-Attributes-OUTPUTS_DIR).

Only the host's `./output` is mounted into the container, so an absolute path -- which `pathlib` would let win over the mount
point -- or one escaping upwards through `..` would be written to a container-only location and lost when the container is
removed. A native run writes straight to the host filesystem, where every path the user names is reachable, so any path is honored
there.

Returns

| Type | Description |
| --- | --- |
| `Path` | `OUTPUTS_DIR` joined with `output_dir`, or `output_dir` itself if it is absolute. |

Raises

| Type | Description |
| --- | --- |
| `ModelconverterException` | If the run is in a container and `output_dir` is empty or `.`, absolute, or contains `..`. |
