# device_handlers

Python API: `modelconverter.utils.device_handlers`

Handlers for driving a physical device.

Benchmarking and analysis of an RVC4 model happen on a real device rather than in the conversion container: the model and its
inputs are pushed to the device, a command is run there and the results are pulled back. This module wraps the two transports used
for that -- ADB and SSH -- behind one interface, so the callers do not have to care which one the device is reachable over.

## Classes

| Name | Description |
| --- | --- |
| [`AdbHandler`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler) | Device handler implementation based on Android Debug Bridge. |
| [`DeviceHandler`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler) | Abstract interface for communicating with a device. |
| [`SSHHandler`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler) | Device handler implementation based on SSH and SCP. |

### class `AdbHandler`

```python
class AdbHandler(DeviceHandler)
```

Device handler implementation based on Android Debug Bridge.

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler-__init__) | Initialize an ADB handler for the target device. |
| [`pull`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler-pull) | Copy a file or directory from the device to the local machine. |
| [`push`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler-push) | Copy a file or directory from the local machine to the device. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler-run) | Run a subprocess command and return its result. |
| [`shell`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler-shell) | Execute a shell command on the target device. |

#### method `__init__`

```python
def __init__(device_id: str | None = None, *, silent: bool = True)
```

Initialize an ADB handler for the target device.

If no device ID is provided, the first connected device is selected.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `device_id` | `str | None` | Optional ADB device identifier. | `None` |
| `silent` | `bool` | If `True`, suppress command logging. | `True` |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If device enumeration fails or no connected device is available. |
| `ValueError` | If the specified device is not connected. |
| `FileNotFoundError` | If the `adb` executable is not available. |

#### method `pull`

```python
def pull(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the device to the local machine.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the device. | required |
| `dst` | `PathType` | Destination path on the local machine. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `push`

```python
def push(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the local machine to the device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the local machine. | required |
| `dst` | `PathType` | Destination path on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `run`

```python
def run(
    *args: PathType,
    check: bool = True,
    silent: bool = True,
) -> tuple[int, str, str]
```

Run a subprocess command and return its result.

The command arguments are converted to strings before execution. Output is always captured and decoded using a best-effort
strategy.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `*args` | `PathType` | Positional command arguments passed to `subprocess.run`. |  |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |
| `silent` | `bool` | If `True`, suppress command logging. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

Raises

| Type | Description |
| --- | --- |
| `subprocess.CalledProcessError` | If `check` is enabled and the subprocess exits with a non-zero status. |

#### method `shell`

```python
def shell(
    cmd: str,
    *,
    check: bool = True,
    silent: bool | None = None,
) -> tuple[int, str, str]
```

Execute a shell command on the target device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cmd` | `str` | Shell command to execute on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |
| `silent` | `bool | None` | If `True`, suppress command logging. If `None`, use the default logging behavior of the handler. | `None` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

### class `DeviceHandler`

```python
class DeviceHandler(ABC)
```

Abstract interface for communicating with a device.

Implementations provide shell access and file transfer operations over a concrete transport such as SSH or ADB.

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler-__init__) | Initialize the device handler. |
| [`pull`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler-pull) | Copy a file or directory from the device to the local machine. |
| [`push`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler-push) | Copy a file or directory from the local machine to the device. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler-run) | Run a subprocess command and return its result. |
| [`shell`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-DeviceHandler-shell) | Execute a shell command on the target device. |

#### method `__init__`

```python
def __init__(*, silent: bool = True)
```

Initialize the device handler.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `silent` | `bool` | If `True`, suppress command logging by default. | `True` |

#### method `pull`

```python
def pull(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the device to the local machine.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the device. | required |
| `dst` | `PathType` | Destination path on the local machine. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `push`

```python
def push(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the local machine to the device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the local machine. | required |
| `dst` | `PathType` | Destination path on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `run`

```python
def run(
    *args: PathType,
    check: bool = False,
    silent: bool = True,
) -> tuple[int, str, str]
```

Run a subprocess command and return its result.

The command arguments are converted to strings before execution. Output is always captured and decoded using a best-effort
strategy.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `*args` | `PathType` | Positional command arguments passed to `subprocess.run`. |  |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `False` |
| `silent` | `bool` | If `True`, suppress command logging. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

Raises

| Type | Description |
| --- | --- |
| `subprocess.CalledProcessError` | If `check` is enabled and the subprocess exits with a non-zero status. |

#### method `shell`

```python
def shell(
    cmd: str,
    *,
    check: bool = True,
    silent: bool | None = None,
) -> tuple[int, str, str]
```

Execute a shell command on the target device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cmd` | `str` | Shell command to execute on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |
| `silent` | `bool | None` | If `True`, suppress command logging. If `None`, use the default logging behavior of the handler. | `None` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

### class `SSHHandler`

```python
class SSHHandler(DeviceHandler)
```

Device handler implementation based on SSH and SCP.

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler-__init__) | Initialize an SSH handler for the target device. |
| [`pull`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler-pull) | Copy a file or directory from the device to the local machine. |
| [`push`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler-push) | Copy a file or directory from the local machine to the device. |
| [`shell`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler-shell) | Execute a shell command on the target device. |

#### method `__init__`

```python
def __init__(ip: str, *, silent: bool = True)
```

Initialize an SSH handler for the target device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `ip` | `str` | Target device IP address. | required |
| `silent` | `bool` | If `True`, suppress command logging. | `True` |

#### method `pull`

```python
def pull(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the device to the local machine.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the device. | required |
| `dst` | `PathType` | Destination path on the local machine. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `push`

```python
def push(src: PathType, dst: PathType, *, check: bool = True) -> tuple[int, str, str]
```

Copy a file or directory from the local machine to the device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `src` | `PathType` | Source path on the local machine. | required |
| `dst` | `PathType` | Destination path on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

#### method `shell`

```python
def shell(
    cmd: str,
    *,
    check: bool = True,
    silent: bool | None = None,
) -> tuple[int, str, str]
```

Execute a shell command on the target device.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cmd` | `str` | Shell command to execute on the device. | required |
| `check` | `bool` | If `True`, propagate subprocess failures as exceptions. | `True` |
| `silent` | `bool | None` | If `True`, suppress command logging. If `None`, use the default logging behavior of the handler. | `None` |

Returns

| Type | Description |
| --- | --- |
| `tuple[int, str, str]` | A tuple containing return code, stdout, and stderr. |

## Functions

| Name | Description |
| --- | --- |
| [`create_handler`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Functions-create_handler) | Create a handler for the device to run on. |

### function `create_handler`

```python
def create_handler(device_ip: str | None, device_adb_id: str | None) -> DeviceHandler
```

Create a handler for the device to run on.

ADB is preferred; an
[SSHHandler](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-SSHHandler)
is only built if
[AdbHandler](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/device_handlers.md#device_handlers-Classes-AdbHandler)
cannot be created and an IP address is available to fall back to.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `device_ip` | `str | None` | IP address of the device, used for the SSH fallback. `None` if no fallback is available. | required |
| `device_adb_id` | `str | None` | ADB identifier of the device, or `None` to use the first connected one. | required |

Returns

| Type | Description |
| --- | --- |
| `DeviceHandler` | A handler for the device. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If no ADB device is connected, or ADB cannot be queried, and no IP address was given. |
| `ValueError` | If the requested ADB device is not connected and no IP address was given. |
| `FileNotFoundError` | If the `adb` executable is not available and no IP address was given. |
