Skip to content

blueye.sdk.cv_models

cv_models

Manage the computer vision model packages installed on the drone.

The Blueye X3 Ultra and X7 run CV model packages (an ONNX model plus a model_meta.json, see the "Bundling CV models" documentation). This module wraps the drone's HTTP API for managing those packages: listing, uploading, deleting, downloading, configuring (autolaunch/device/rate), and pre-building inference engines.

The API is plain HTTP, so listing and downloading work on a Drone(auto_connect=False) instance as well. The drone only picks up a change after a rescan, which is a control message, so make changes from a Drone(connect_as_observer=True) instance: an observer takes no control over the drone.

Classes:

  • CvModel –

    One CV model package installed on the drone.

  • CvModels –

    CV model package management on the drone.

CvModel dataclass

CvModel(
    name: str,
    directory: str,
    type: str,
    output_format: str,
    size_bytes: int,
    labels: list[str],
    enabled: bool,
    raw: dict,
)

One CV model package installed on the drone.

Attributes:

  • name (str) –

    The human-readable model name from its model_meta.json.

  • directory (str) –

    The package's directory slug on the drone — this is the identifier the other methods take as name.

  • type (str) –

    "detection", "sot", or "unknown".

  • output_format (str) –

    The decoder format (e.g. "yolov8_flat").

  • size_bytes (int) –

    Size of the model weights file.

  • labels (list[str]) –

    Class labels.

  • enabled (bool) –

    Whether the model autolaunches on the drone (runtime.enabled).

  • raw (dict) –

    The verbatim API entry, including the optional preprocessing/detection/ sot/tracking/runtime blocks when present.

Methods:

  • from_json –

    Build a CvModel from one entry of the drone's API response.

from_json classmethod

from_json(entry: dict) -> CvModel

Build a CvModel from one entry of the drone's API response.

Source code in blueye/sdk/cv_models.py
63
64
65
66
67
68
69
70
71
72
73
74
75
@classmethod
def from_json(cls, entry: dict) -> CvModel:
    """Build a CvModel from one entry of the drone's API response."""
    return cls(
        name=entry.get("name", ""),
        directory=entry.get("directory", ""),
        type=entry.get("type", "unknown"),
        output_format=entry.get("output_format", ""),
        size_bytes=int(entry.get("size_bytes", 0)),
        labels=list(entry.get("labels", [])),
        enabled=bool(entry.get("enabled", False)),
        raw=entry,
    )

CvModels

CvModels(parent_drone: 'blueye.sdk.Drone')

CV model package management on the drone.

Accessed through drone.cv_models, e.g.::

drone = blueye.sdk.Drone(auto_connect=False)
for model in drone.cv_models.list():
    print(model.name, model.enabled)

All methods raise requests.exceptions.ConnectionError/ConnectTimeout when the drone is unreachable, and requests.exceptions.HTTPError (with the drone's error message included) when the drone rejects a request.

The listing and the changes go over HTTP and need no connection. The drone only picks up a change after a rescan, which is a control message, so upload, delete and the set_* methods send one when the drone is connected (an observer connection is enough) and log a warning when it is not. rescan sends one on its own.

Methods:

  • delete –

    Delete a model package from the drone.

  • download –

    Download a model package from the drone as a zip.

  • list –

    List the model packages installed on the drone.

  • rescan –

    Ask the drone's vision pipeline to rescan the installed packages.

  • set_device –

    Set the execution provider a model runs on.

  • set_enabled –

    Enable or disable a model's autolaunch on the drone.

  • set_hz –

    Set a model's maximum inference rate.

  • upload –

    Upload a model package zip to the drone.

  • warmup –

    Pre-build the model's inference engine on the drone.

Source code in blueye/sdk/cv_models.py
 99
100
def __init__(self, parent_drone: "blueye.sdk.Drone"):
    self._parent_drone = parent_drone

delete

delete(name: str, timeout: float = 5) -> None

Delete a model package from the drone.

Parameters:

  • name (str) –

    The package's directory slug (see CvModel.directory).

  • timeout (float, default: 5 ) –

    Request timeout in seconds.

Source code in blueye/sdk/cv_models.py
159
160
161
162
163
164
165
166
167
def delete(self, name: str, timeout: float = 5) -> None:
    """Delete a model package from the drone.

    Args:
        name: The package's directory slug (see CvModel.directory).
        timeout: Request timeout in seconds.
    """
    self._check(requests.delete(f"{self._base_url}/{name}", timeout=timeout))
    self._request_rescan()

download

download(
    name: str,
    output_path: Path | str | None = None,
    timeout: float = 60,
) -> Path

Download a model package from the drone as a zip.

Parameters:

  • name (str) –

    The package's directory slug.

  • output_path (Path | str | None, default: None ) –

    Destination file or directory. Defaults to the filename the drone suggests (or <name>.zip) in the current directory.

  • timeout (float, default: 60 ) –

    Request timeout in seconds.

Returns:

  • Path –

    The path the zip was written to.

Source code in blueye/sdk/cv_models.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def download(
    self, name: str, output_path: Path | str | None = None, timeout: float = 60
) -> Path:
    """Download a model package from the drone as a zip.

    Args:
        name: The package's directory slug.
        output_path: Destination file or directory. Defaults to the filename the
            drone suggests (or `<name>.zip`) in the current directory.
        timeout: Request timeout in seconds.

    Returns:
        The path the zip was written to.
    """
    response = self._check(requests.get(f"{self._base_url}/{name}/download", timeout=timeout))
    disposition = response.headers.get("Content-Disposition", "")
    matches = re.findall('filename="([^"]+)"', disposition)
    filename = matches[0] if matches else f"{name}.zip"

    if output_path is None:
        output_path = Path(filename)
    else:
        output_path = Path(output_path)
        if output_path.is_dir():
            output_path = output_path / filename
    output_path.write_bytes(response.content)
    return output_path

list

list(timeout: float = 5) -> list[CvModel]

List the model packages installed on the drone.

Parameters:

  • timeout (float, default: 5 ) –

    Request timeout in seconds.

Returns:

  • list[CvModel] –

    One CvModel per installed package, sorted by directory name.

Source code in blueye/sdk/cv_models.py
121
122
123
124
125
126
127
128
129
130
131
132
def list(self, timeout: float = 5) -> list[CvModel]:
    """List the model packages installed on the drone.

    Args:
        timeout: Request timeout in seconds.

    Returns:
        One CvModel per installed package, sorted by directory name.
    """
    response = self._check(requests.get(f"{self._base_url}/", timeout=timeout))
    models = [CvModel.from_json(entry) for entry in response.json()]
    return sorted(models, key=lambda model: model.directory)

rescan

rescan(timeout: float | None = None) -> None

Ask the drone's vision pipeline to rescan the installed packages.

The request is the RescanCvModelsCtrl control message, so the drone has to be connected; an observer connection is enough. The drone has no file watcher, so a package uploaded, deleted or reconfigured stays as it was until a rescan or a reboot. upload, delete and the set_* methods send one themselves while connected, so this is for changes made some other way. A rescan restarts every running model, and models only run while a client in control pets the watchdog.

Parameters:

  • timeout (float | None, default: None ) –

    Ignored. The rescan used to be an HTTP request with a timeout; the argument stays so code written for 2.8.0 keeps working.

Raises:

  • ConnectionError –

    If the drone is not connected.

Source code in blueye/sdk/cv_models.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def rescan(self, timeout: float | None = None) -> None:
    """Ask the drone's vision pipeline to rescan the installed packages.

    The request is the `RescanCvModelsCtrl` control message, so the drone has to be
    connected; an observer connection is enough. The drone has no file watcher, so a
    package uploaded, deleted or reconfigured stays as it was until a rescan or a reboot.
    [`upload`][blueye.sdk.cv_models.CvModels.upload],
    [`delete`][blueye.sdk.cv_models.CvModels.delete] and the `set_*` methods send one
    themselves while connected, so this is for changes made some other way. A rescan
    restarts every running model, and models only run while a client in control pets the
    watchdog.

    Args:
        timeout: Ignored. The rescan used to be an HTTP request with a timeout; the
            argument stays so code written for 2.8.0 keeps working.

    Raises:
        ConnectionError: If the drone is not connected.
    """
    if timeout is not None:
        warnings.warn(
            "`CvModels.rescan` sends a control message and no longer takes a timeout, "
            "the argument is ignored and will be removed in the next major version",
            DeprecationWarning,
            stacklevel=2,
        )
    self._parent_drone._ctrl_client.rescan_cv_models()

set_device

set_device(
    name: str, device: str, timeout: float = 5
) -> None

Set the execution provider a model runs on.

Parameters:

  • name (str) –

    The package's directory slug.

  • device (str) –

    One of :data:VALID_DEVICES ("cuda", "tensorrt", "tensorrt-dla0", "tensorrt-dla1").

  • timeout (float, default: 5 ) –

    Request timeout in seconds.

Source code in blueye/sdk/cv_models.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def set_device(self, name: str, device: str, timeout: float = 5) -> None:
    """Set the execution provider a model runs on.

    Args:
        name: The package's directory slug.
        device: One of :data:`VALID_DEVICES` ("cuda", "tensorrt",
            "tensorrt-dla0", "tensorrt-dla1").
        timeout: Request timeout in seconds.
    """
    self._check(
        requests.patch(
            f"{self._base_url}/{name}/device", json={"device": device}, timeout=timeout
        )
    )
    self._request_rescan()

set_enabled

set_enabled(
    name: str, enabled: bool, timeout: float = 5
) -> None

Enable or disable a model's autolaunch on the drone.

Parameters:

  • name (str) –

    The package's directory slug.

  • enabled (bool) –

    True to autolaunch the model, False to disable it.

  • timeout (float, default: 5 ) –

    Request timeout in seconds.

Source code in blueye/sdk/cv_models.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def set_enabled(self, name: str, enabled: bool, timeout: float = 5) -> None:
    """Enable or disable a model's autolaunch on the drone.

    Args:
        name: The package's directory slug.
        enabled: True to autolaunch the model, False to disable it.
        timeout: Request timeout in seconds.
    """
    self._check(
        requests.patch(
            f"{self._base_url}/{name}/enabled", json={"enabled": enabled}, timeout=timeout
        )
    )
    self._request_rescan()

set_hz

set_hz(name: str, hz: int, timeout: float = 5) -> None

Set a model's maximum inference rate.

Parameters:

  • name (str) –

    The package's directory slug.

  • hz (int) –

    One of :data:VALID_HZ (0, 5, 10, 15); 0 means unlimited.

  • timeout (float, default: 5 ) –

    Request timeout in seconds.

Source code in blueye/sdk/cv_models.py
228
229
230
231
232
233
234
235
236
237
def set_hz(self, name: str, hz: int, timeout: float = 5) -> None:
    """Set a model's maximum inference rate.

    Args:
        name: The package's directory slug.
        hz: One of :data:`VALID_HZ` (0, 5, 10, 15); 0 means unlimited.
        timeout: Request timeout in seconds.
    """
    self._check(requests.patch(f"{self._base_url}/{name}/hz", json={"hz": hz}, timeout=timeout))
    self._request_rescan()

upload

upload(package: Path | str, timeout: float = 60) -> CvModel

Upload a model package zip to the drone.

The drone validates the archive (it must contain a valid model_meta.json and the model file it references) and installs it into a directory named after the slugified model name — an existing package with the same slug is replaced.

Parameters:

  • package (Path | str) –

    Path to the package zip (e.g. produced by blueye bundle-model).

  • timeout (float, default: 60 ) –

    Request timeout in seconds; model files can be large.

Returns:

  • CvModel –

    The installed model as reported by the drone.

Source code in blueye/sdk/cv_models.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def upload(self, package: Path | str, timeout: float = 60) -> CvModel:
    """Upload a model package zip to the drone.

    The drone validates the archive (it must contain a valid model_meta.json and
    the model file it references) and installs it into a directory named after
    the slugified model name — an existing package with the same slug is
    replaced.

    Args:
        package: Path to the package zip (e.g. produced by
            `blueye bundle-model`).
        timeout: Request timeout in seconds; model files can be large.

    Returns:
        The installed model as reported by the drone.
    """
    package = Path(package)
    with package.open("rb") as file_handle:
        response = requests.post(
            f"{self._base_url}/upload", files={"file": file_handle}, timeout=timeout
        )
    model = CvModel.from_json(self._check(response).json())
    self._request_rescan()
    return model

warmup

warmup(name: str, timeout: float = 600) -> None

Pre-build the model's inference engine on the drone.

For TensorRT devices this compiles the engine, which can take several minutes — subsequent launches then start instantly. Only available on the drone itself (the API answers 503 elsewhere).

Parameters:

  • name (str) –

    The package's directory slug.

  • timeout (float, default: 600 ) –

    Request timeout in seconds; engine builds are slow.

Source code in blueye/sdk/cv_models.py
239
240
241
242
243
244
245
246
247
248
249
250
def warmup(self, name: str, timeout: float = 600) -> None:
    """Pre-build the model's inference engine on the drone.

    For TensorRT devices this compiles the engine, which can take several
    minutes — subsequent launches then start instantly. Only available on the
    drone itself (the API answers 503 elsewhere).

    Args:
        name: The package's directory slug.
        timeout: Request timeout in seconds; engine builds are slow.
    """
    self._check(requests.post(f"{self._base_url}/{name}/warmup", timeout=timeout))