ultralytics-inference 0.0.39

Ultralytics YOLO inference library and CLI for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<!-- Ultralytics ๐Ÿš€ AGPL-3.0 License - https://ultralytics.com/license -->

<div align="center">
  <p>
    <a href="https://www.ultralytics.com/events/yolovision?utm_source=github&utm_medium=social&utm_campaign=yolovision26&utm_content=banner" target="_blank">
      <img width="100%" src="https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/banner-yolov8.png" alt="Ultralytics YOLO banner"></a>
  </p>

[ไธญๆ–‡](https://docs.ultralytics.com/zh/inference) | [ํ•œ๊ตญ์–ด](https://docs.ultralytics.com/ko/inference) | [ๆ—ฅๆœฌ่ชž](https://docs.ultralytics.com/ja/inference) | [ะ ัƒััะบะธะน](https://docs.ultralytics.com/ru/inference) | [Deutsch](https://docs.ultralytics.com/de/inference) | [Franรงais](https://docs.ultralytics.com/fr/inference) | [Espaรฑol](https://docs.ultralytics.com/es/inference) | [Portuguรชs](https://docs.ultralytics.com/pt/inference) | [Tรผrkรงe](https://docs.ultralytics.com/tr/inference) | [Tiแบฟng Viแป‡t](https://docs.ultralytics.com/vi/inference) | [ุงู„ุนุฑุจูŠุฉ](https://docs.ultralytics.com/ar/inference) <br>

</div>

# ๐Ÿฆ€ Ultralytics YOLO Rust Inference

<div align="center">

[English](README.md) | [็ฎ€ไฝ“ไธญๆ–‡](README.zh-CN.md)

</div>

High-performance YOLO inference library written in Rust. This library provides a fast, safe, and efficient interface for running YOLO models using ONNX Runtime, with an API designed to match the [Ultralytics Python package](https://github.com/ultralytics/ultralytics).

<div align="center">

[![Ultralytics Discord](https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue)](https://discord.com/invite/ultralytics)
[![Ultralytics Forums](https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue)](https://community.ultralytics.com)
[![Ultralytics Reddit](https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue)](https://www.reddit.com/r/Ultralytics/)
[![codecov](https://codecov.io/github/ultralytics/inference/branch/main/graph/badge.svg)](https://app.codecov.io/github/ultralytics/inference)
[![CI](https://github.com/ultralytics/inference/actions/workflows/ci.yml/badge.svg)](https://github.com/ultralytics/inference/actions/workflows/ci.yml)
[![arXiv](https://img.shields.io/badge/arXiv-2606.03748-b31b1b?logo=arxiv&logoColor=white)](https://arxiv.org/abs/2606.03748)

[![Crates.io](https://img.shields.io/crates/v/ultralytics-inference?logo=rust&logoColor=white&label=crates.io&color=CE422B)](https://crates.io/crates/ultralytics-inference)
[![docs.rs](https://img.shields.io/docsrs/ultralytics-inference?logo=docs.rs&logoColor=white&label=docs.rs&color=CE422B)](https://docs.rs/ultralytics-inference)
[![Downloads](https://img.shields.io/crates/d/ultralytics-inference?logo=rust&logoColor=white&label=downloads&color=CE422B)](https://crates.io/crates/ultralytics-inference)
[![MSRV](https://img.shields.io/crates/msrv/ultralytics-inference?logo=rust&logoColor=white&color=CE422B)](https://crates.io/crates/ultralytics-inference)
[![License](https://img.shields.io/crates/l/ultralytics-inference?label=license&color=blue)](https://github.com/ultralytics/inference/blob/main/LICENSE)
[![dependency status](https://deps.rs/repo/github/ultralytics/inference/status.svg)](https://deps.rs/repo/github/ultralytics/inference)

</div>

## โœจ Features

- ๐Ÿš€ **High Performance** - Pure Rust implementation with zero-cost abstractions
- ๐ŸŽฏ **Ultralytics API Compatible** - `Results`, `Boxes`, `Masks`, `Keypoints`, `Probs`, `SemanticMask`, and `DepthMap` types matching the Python API shape
- ๐Ÿ”ง **Multiple Backends** - CPU, XNNPACK, CUDA, TensorRT, CoreML, OpenVINO, and more via ONNX Runtime
- ๐Ÿ“ฆ **Dual Use** - Library for Rust projects + standalone CLI application
- ๐Ÿท๏ธ **Auto Metadata** - Automatically reads class names, task type, and input size from ONNX models
- โฌ‡๏ธ **Auto Download** - Downloads supported YOLO26, YOLO11, and YOLOv8 ONNX models (sizes: n/s/m/l/x) when not found locally
- ๐Ÿ–ผ๏ธ **Multiple Sources** - Images, directories, glob patterns, video files, webcams, and streams
- ๐Ÿชถ **Lean Runtime** - No PyTorch, TensorFlow, or Python runtime required

## โœจ Models

<a href="https://docs.ultralytics.com/tasks" target="_blank">
    <img width="100%" src="https://cdn.ul.run/i/c99d914c3958d0755b5a3d7204b6f24a.avif" alt="Ultralytics YOLO supported tasks">
</a>
<br>
<br>

This crate runs [YOLOv8](https://docs.ultralytics.com/models/yolov8), [YOLO11](https://docs.ultralytics.com/models/yolo11), and [YOLO26](https://docs.ultralytics.com/models/yolo26) ONNX models. They are pretrained on [COCO](https://docs.ultralytics.com/datasets/detect/coco) for [Detection](https://docs.ultralytics.com/tasks/detect), [Segmentation](https://docs.ultralytics.com/tasks/segment), and [Pose Estimation](https://docs.ultralytics.com/tasks/pose); on [DOTA](https://docs.ultralytics.com/datasets/obb/dota-v2) for [OBB](https://docs.ultralytics.com/tasks/obb); on [Cityscapes](https://docs.ultralytics.com/datasets/semantic/cityscapes) for [Semantic Segmentation](https://docs.ultralytics.com/tasks/semantic); on [ImageNet](https://docs.ultralytics.com/datasets/classify/imagenet) for [Classification](https://docs.ultralytics.com/tasks/classify); and for monocular [Depth Estimation](https://docs.ultralytics.com/tasks/depth) (YOLO26 only). All [models](https://docs.ultralytics.com/models) download automatically from the latest Ultralytics [release](https://github.com/ultralytics/assets/releases) on first use.

## ๐Ÿš€ Quick Start

### Prerequisites

- [Rust 1.89+](https://rustup.rs/) (install via rustup)
- A YOLO ONNX model (export from Ultralytics: `yolo export model=yolo26n.pt format=onnx`)

### System dependencies

Building from source (this includes `cargo install`) compiles native crates, so you need a C compiler. On Linux you additionally need `pkg-config` and the OpenSSL development headers, which the HTTPS model/asset downloader links against. macOS and Windows use their system TLS backends, so a C toolchain is all that is required.

```bash
# Debian/Ubuntu
sudo apt install build-essential pkg-config libssl-dev

# Fedora/RHEL
sudo dnf install gcc gcc-c++ pkgconf-pkg-config openssl-devel

# Arch
sudo pacman -S base-devel openssl pkgconf

# macOS (Xcode Command Line Tools provide the clang compiler)
xcode-select --install

# Windows: install the "Desktop development with C++" workload from
# Visual Studio Build Tools (https://visualstudio.microsoft.com/downloads/)
```

### Installation

```bash
# Install CLI globally from crates.io
cargo install ultralytics-inference

# Install CLI globally with custom features
# Minimal build (no default features)
cargo install ultralytics-inference --no-default-features

# Enable video support
cargo install ultralytics-inference --features video

# Enable multiple accelerators
cargo install ultralytics-inference --features "cuda,tensorrt"
```

### Development install

```bash
# Install CLI directly from the git repository
cargo install --git https://github.com/ultralytics/inference.git ultralytics-inference

# Or clone, build, and install from source
git clone https://github.com/ultralytics/inference.git
cd inference
cargo build --release

# Install from local checkout
cargo install --path . --locked
```

`cargo install` places binaries in Cargo's default bin directory:

- macOS/Linux: `~/.cargo/bin`
- Windows: `%USERPROFILE%\\.cargo\\bin`

Ensure this directory is in your `PATH`, then run from anywhere:

```bash
ultralytics-inference help
```

### Export a YOLO Model to ONNX

```bash
# Using Ultralytics CLI (FP32, default)
yolo export model=yolo26n.pt format=onnx

# FP16 - ~50% smaller model
yolo export model=yolo26n.pt format=onnx quantize=16
```

```python
# Or with Python
from ultralytics import YOLO

model = YOLO("yolo26n.pt")
model.export(format="onnx")  # FP32 (default)
model.export(format="onnx", quantize=16)  # FP16
```

> **Precision / quantization:** Ultralytics โ‰ฅ8.4 uses a single `quantize`
> argument instead of the deprecated `half=True` / `int8=True` flags. For ONNX
> the supported values are `32`/`fp32` (FP32, the default), `16`/`fp16` (FP16),
> and `8`/`int8` (INT8 - requires a calibration dataset via `data=`). The old
> flags still work but emit a deprecation warning. See the
> [export docs](https://docs.ultralytics.com/modes/export) and the
> [ONNX integration guide](https://docs.ultralytics.com/integrations/onnx).

### Run Inference

```bash
# With defaults (auto-downloads yolo26n.onnx and sample images)
ultralytics-inference predict

# Select task: auto-downloads the nano model for that task
ultralytics-inference predict --task segment  # downloads yolo26n-seg.onnx
ultralytics-inference predict --task pose     # downloads yolo26n-pose.onnx
ultralytics-inference predict --task obb      # downloads yolo26n-obb.onnx
ultralytics-inference predict --task classify # downloads yolo26n-cls.onnx
ultralytics-inference predict --task semantic # downloads yolo26n-sem.onnx (YOLO26 only)
ultralytics-inference predict --task depth    # downloads yolo26n-depth.onnx (YOLO26 only)

# With explicit model (task is read from model metadata)
ultralytics-inference predict --model yolo26n.onnx --source image.jpg

# Auto-download any supported size (n/s/m/l/x) across YOLO26, YOLO11, and YOLOv8
ultralytics-inference predict --model yolo26l.onnx --source image.jpg
ultralytics-inference predict --model yolo11x-seg.onnx --source image.jpg
ultralytics-inference predict --model yolov8n.onnx --source image.jpg

# On a directory of images
ultralytics-inference predict --model yolo26n.onnx --source assets/

# With custom thresholds
ultralytics-inference predict -m yolo26n.onnx -s image.jpg --conf 0.5 --iou 0.45

# Filter by class IDs
ultralytics-inference predict --model yolo26n.onnx --source image.jpg --classes 0
ultralytics-inference predict --model yolo26n.onnx --source image.jpg --classes "0,1,2"

# With visualization and custom image size
ultralytics-inference predict --model yolo26n.onnx --source video.mp4 --show --imgsz 1280

# Save individual frames for video input
ultralytics-inference predict --model yolo26n.onnx --source video.mp4 --save-frames

# Rectangular inference
ultralytics-inference predict --model yolo26n.onnx --source image.jpg --rect

# Semantic segmentation: write per-image PNG class maps to runs/semantic/predictN/results/
ultralytics-inference predict --task semantic --source cityscapes/ --save-json

# Depth estimation: blend the colorized depth over the image into runs/depth/predictN/
ultralytics-inference predict --task depth --source image.jpg
```

### Example Output

```bash
ultralytics-inference predict
```

```text
WARNING โš ๏ธ 'model' argument is missing. Using default '--model=yolo26n.onnx'.
WARNING โš ๏ธ 'source' argument is missing. Using default images: https://ultralytics.com/images/bus.jpg, https://ultralytics.com/images/zidane.jpg
Ultralytics Inference 0.0.39 ๐Ÿš€ Rust ONNX FP32 CPU
Using ONNX Runtime CPUExecutionProvider
YOLO26n summary: 80 classes, imgsz=(640, 640)

image 1/2 /home/ultralytics/inference/bus.jpg: 640x480 4 persons, 1 bus, 36.4ms
image 2/2 /home/ultralytics/inference/zidane.jpg: 384x640 2 persons, 1 tie, 28.6ms
Speed: 1.5ms preprocess, 32.5ms inference, 0.5ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs/detect/predict1
๐Ÿ’ก Learn more at https://docs.ultralytics.com/modes/predict
```

**With `--task` (auto-downloads the matching nano model):**

```bash
ultralytics-inference predict --task segment
```

```text
WARNING โš ๏ธ 'model' argument is missing. Using default '--model=yolo26n-seg.onnx'.
WARNING โš ๏ธ 'source' argument is missing. Using default images: https://ultralytics.com/images/bus.jpg, https://ultralytics.com/images/zidane.jpg
Ultralytics Inference 0.0.39 ๐Ÿš€ Rust ONNX FP32 CPU
Using ONNX Runtime CPUExecutionProvider
YOLO26n-seg summary: 80 classes, imgsz=(640, 640)

image 1/2 /home/ultralytics/inference/bus.jpg: 640x480 4 persons, 1 bus, 48.2ms
image 2/2 /home/ultralytics/inference/zidane.jpg: 384x640 2 persons, 1 tie, 38.1ms
Speed: 1.6ms preprocess, 44.3ms inference, 1.2ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs/segment/predict1
๐Ÿ’ก Learn more at https://docs.ultralytics.com/modes/predict
```

## ๐Ÿ“š Usage

### As a CLI Tool

```bash
# Show help
ultralytics-inference help

# Show version
ultralytics-inference version

# Run inference
ultralytics-inference predict --model <model.onnx> --source <source>
```

`--help` and `--version` are also supported as standard flag aliases.

**CLI Options:**

| Option          | Short | Description                                                                                                                                                                                           | Default                               |
| --------------- | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| `--model`       | `-m`  | Path to ONNX model file; auto-downloaded if a known YOLOv8/YOLO11/YOLO26 name                                                                                                                         | `yolo26n.onnx`                        |
| `--task`        |       | Task type (`detect`, `segment`, `pose`, `obb`, `classify`, `semantic`\*, `depth`\*); selects nano model when `--model` is omitted                                                                     | `detect`                              |
| `--source`      | `-s`  | Input source (image, directory, glob, video, webcam index, or URL)                                                                                                                                    | Task-dependent Ultralytics URL assets |
| `--conf`        |       | Confidence threshold                                                                                                                                                                                  | `0.25`                                |
| `--iou`         |       | IoU threshold for NMS                                                                                                                                                                                 | `0.7`                                 |
| `--max-det`     |       | Maximum number of detections                                                                                                                                                                          | `300`                                 |
| `--imgsz`       |       | Inference image size                                                                                                                                                                                  | `Model metadata`                      |
| `--rect`        |       | Enable rectangular inference (minimal padding)                                                                                                                                                        | `true`                                |
| `--batch`       |       | Batch size for inference                                                                                                                                                                              | `1`                                   |
| `--quantize`    |       | Inference precision: `8`, `16`, `32`, `int8`, `fp16`, `fp32`, `w8a8`, `w16a16`, `w8a16`, or `w8a32`                                                                                                   | Model precision                       |
| `--save`        |       | Save annotated results to runs/\<task\>/predict                                                                                                                                                       | `true`                                |
| `--save-frames` |       | Save individual frames for video input (instead of video file)                                                                                                                                        | `false`                               |
| `--save-json`   |       | Save semantic segmentation class-map PNGs for external evaluation                                                                                                                                     | `false`                               |
| `--show`        |       | Display results in a window                                                                                                                                                                           | `false`                               |
| `--device`      |       | Device string, e.g. cpu, cuda:0, coreml, directml:0, intel:cpu, intel:gpu, intel:npu, tensorrt:0, rocm:0, xnnpack; additional providers selectable when their feature is enabled (see Features table) | `cpu`                                 |
| `--verbose`     |       | Show verbose output                                                                                                                                                                                   | `true`                                |
| `--classes`     |       | Filter by class IDs, e.g. `0` or `"0,1,2"` or `"[0, 1, 2]"`                                                                                                                                           | all classes                           |

The legacy `--half` flag remains accepted for backward compatibility, maps to `--quantize 16`, and emits the same deprecation warning as Ultralytics Python. An explicit `--quantize` value wins.

On CPU, ONNX Runtime widens an FP16 ONNX graph to FP32 weights while it builds the inference session, so an FP16 export runs at FP32 speed. The compact FP16 source file is never modified.

**Task and Model Resolution:**

| Invocation                                        | Model used             | Notes                                                               |
| ------------------------------------------------- | ---------------------- | ------------------------------------------------------------------- |
| `predict`                                         | `yolo26n.onnx`         | Default detect model, auto-downloaded                               |
| `predict --task segment`                          | `yolo26n-seg.onnx`     | Nano seg model, auto-downloaded                                     |
| `predict --task pose`                             | `yolo26n-pose.onnx`    | Nano pose model, auto-downloaded                                    |
| `predict --task obb`                              | `yolo26n-obb.onnx`     | Nano OBB model, auto-downloaded                                     |
| `predict --task classify`                         | `yolo26n-cls.onnx`     | Nano classify model, auto-downloaded                                |
| `predict --task semantic`                         | `yolo26n-sem.onnx`\*   | Nano semantic segmentation model, auto-downloaded (YOLO26 only)     |
| `predict --task depth`                            | `yolo26n-depth.onnx`\* | Nano depth estimation model, auto-downloaded (YOLO26 only)          |
| `predict --model yolo26l-seg.onnx`                | `yolo26l-seg.onnx`     | Task read from model metadata                                       |
| `predict --task segment --model yolo26l-seg.onnx` | `yolo26l-seg.onnx`     | `--task` matches metadata, proceeds normally                        |
| `predict --task segment --model yolo26n.onnx`     | error                  | `--task` conflicts with model metadata (`detect`), exits with error |

\* `semantic` (semantic segmentation) and `depth` (depth estimation) are YOLO26-only.

**Auto-downloadable models:**

YOLOv8, YOLO11, and YOLO26 ONNX models in sizes **n / s / m / l / x** are supported for auto-download across the standard task variants. YOLO26 also includes `-sem` for semantic segmentation and `-depth` for depth estimation:

| Family | Variants                                                                                              |
| ------ | ----------------------------------------------------------------------------------------------------- |
| YOLO26 | `yolo26{n,s,m,l,x}.onnx`, `yolo26{n,s,m,l,x}-seg.onnx`, `-pose`, `-obb`, `-cls`, `-sem`\*, `-depth`\* |
| YOLO11 | `yolo11{n,s,m,l,x}.onnx`, `yolo11{n,s,m,l,x}-seg.onnx`, `-pose`, `-obb`, `-cls`                       |
| YOLOv8 | `yolov8{n,s,m,l,x}.onnx`, `yolov8{n,s,m,l,x}-seg.onnx`, `-pose`, `-obb`, `-cls`                       |

\* `-sem` (semantic segmentation) and `-depth` (depth estimation) are YOLO26-only.

**Source Options:**

| Source Type | Example Input                   | Description                       |
| ----------- | ------------------------------- | --------------------------------- |
| Image       | `image.jpg`                     | Single image file                 |
| Directory   | `images/`                       | Directory of images               |
| Glob        | `images/*.jpg`                  | Glob pattern for images           |
| Video       | `video.mp4`                     | Video file                        |
| Webcam      | `0`,`1`                         | Webcam index (0 = default webcam) |
| URL         | `https://example.com/image.jpg` | Remote image URL                  |

### As a Rust Library

Add to your `Cargo.toml` (choose one):

```toml
# Stable release from crates.io
[dependencies]
ultralytics-inference = "0.0.39"
```

```toml
# Development version (latest unreleased code from GitHub)
[dependencies]
ultralytics-inference = { git = "https://github.com/ultralytics/inference.git" }
```

**Basic Usage:**

```rust
use ultralytics_inference::YOLOModel;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load model - metadata (classes, task, imgsz) is read automatically
    let mut model = YOLOModel::load("yolo26n.onnx")?;

    // Run inference
    let results = model.predict("image.jpg")?;

    // Process results
    for result in &results {
        if let Some(ref boxes) = result.boxes {
            println!("Found {} detections", boxes.len());
            for i in 0..boxes.len() {
                let cls = boxes.cls()[i] as usize;
                let conf = boxes.conf()[i];
                let name = result.names.get(&cls).map(|s| s.as_str()).unwrap_or("unknown");
                println!("  {} {:.2}", name, conf);
            }
        }
    }

    Ok(())
}
```

**With Custom Configuration:**

```rust
use ultralytics_inference::{YOLOModel, InferenceConfig};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = InferenceConfig::new()
        .with_confidence(0.5)
        .with_iou(0.45)
        .with_max_det(300);

    let mut model = YOLOModel::load_with_config("yolo26n.onnx", config)?;
    let results = model.predict("image.jpg")?;

    Ok(())
}
```

**Accessing Detection Data:**

```rust
if let Some(ref boxes) = result.boxes {
    // Bounding boxes in different formats
    let xyxy = boxes.xyxy();      // [x1, y1, x2, y2]
    let xywh = boxes.xywh();      // [x_center, y_center, width, height]
    let xyxyn = boxes.xyxyn();    // Normalized [0-1]
    let xywhn = boxes.xywhn();    // Normalized [0-1]

    // Confidence scores and class IDs
    let conf = boxes.conf();      // Confidence scores
    let cls = boxes.cls();        // Class IDs
}
```

**Selecting a Device:**

```rust
use ultralytics_inference::{Device, InferenceConfig, YOLOModel};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Select a device (e.g., CUDA, CoreML, CPU)
    let device = Device::Cuda(0);

    // Configure the model to use this device
    let config = InferenceConfig::new().with_device(device);

    let mut model = YOLOModel::load_with_config("yolo26n.onnx", config)?;
    let results = model.predict("image.jpg")?;

    Ok(())
}
```

**Depth Visualization:**

Depth results are rendered by blending the colorized depth map over the source image at
`alpha = 0.6`, using the `jet` colormap and `disparity` normalization. This matches the
Ultralytics Python `Annotator.depth_map` default, so the CLI needs no depth flags and
`--save` produces the same image Python's `plot()` does.

Set the colormap, normalization, and overlay opacity explicitly through the library
(`Jet` + `Disparity` + `0.6` below match the CLI default; swap in any other variant):

```rust
use ultralytics_inference::YOLOModel;
use ultralytics_inference::annotate::{annotate_image_with, load_image};
use ultralytics_inference::visualizer::color::{Colormap, DepthViz};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut model = YOLOModel::load("yolo26n-depth.onnx")?;
    let results = model.predict("image.jpg")?;

    // Per-pixel depth in meters, at the original image resolution
    if let Some(depth) = &results[0].depth {
        println!("{:?} {:?}m", depth.data.shape(), depth.min_depth());
    }

    // Colormap: inferno / jet / spectral / gray. Normalization: disparity / metric.
    // Opacity: 0.6 blends over the image (CLI default), 1.0 is the full colorized map.
    let image = load_image("image.jpg")?;
    let annotated = annotate_image_with(&image, &results[0], None, Colormap::Jet, DepthViz::Disparity, 0.6);
    annotated.save("depth.jpg")?;

    Ok(())
}
```

`disparity` colors `1/d` instead of `d`, clipped to the 2nd-98th percentile. Inverting the
depth spends the color range on nearby detail rather than the distant background, so near
objects read warm and a few stray pixels cannot wash out the rest. `metric` colors depth
directly, linearly between its min and max.

For runnable programs you can copy and adapt, see the [examples](examples/README.md) directory.

## ๐Ÿ—‚๏ธ Project Structure

```text
inference/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib.rs              # Library entry point and public exports
โ”‚   โ”œโ”€โ”€ main.rs             # CLI application
โ”‚   โ”œโ”€โ”€ model.rs            # YOLOModel - ONNX session and inference
โ”‚   โ”œโ”€โ”€ results.rs          # Results, Boxes, Masks, Keypoints, Probs, Obb, SemanticMask, DepthMap
โ”‚   โ”œโ”€โ”€ preprocessing.rs    # Image preprocessing (letterbox, normalize, SIMD)
โ”‚   โ”œโ”€โ”€ postprocessing.rs   # Post-processing for all tasks (NMS/decode for detection, argmax for semantic, resize for depth)
โ”‚   โ”œโ”€โ”€ metadata.rs         # ONNX model metadata parsing
โ”‚   โ”œโ”€โ”€ source.rs           # Input source handling (images, video, webcam)
โ”‚   โ”œโ”€โ”€ task.rs             # Task enum (Detect, Segment, Pose, Classify, Obb, Semantic, Depth)
โ”‚   โ”œโ”€โ”€ inference.rs        # InferenceConfig
โ”‚   โ”œโ”€โ”€ batch.rs            # Batch processing pipeline
โ”‚   โ”œโ”€โ”€ device.rs           # Device enum (CPU, CUDA, CoreML, etc.)
โ”‚   โ”œโ”€โ”€ cuda_inference.rs   # Fused CUDA preprocess kernel (cuda-preprocess feature)
โ”‚   โ”œโ”€โ”€ parallel.rs         # Rayon parallelism shims (sequential on wasm)
โ”‚   โ”œโ”€โ”€ download.rs         # Model and asset downloading
โ”‚   โ”œโ”€โ”€ annotate.rs         # Image annotation (bounding boxes, instance masks, keypoints, semantic overlay, depth colormap)
โ”‚   โ”œโ”€โ”€ io.rs               # Result saving (images, videos)
โ”‚   โ”œโ”€โ”€ logging.rs          # Logging macros
โ”‚   โ”œโ”€โ”€ error.rs            # Error types
โ”‚   โ”œโ”€โ”€ utils.rs            # Utility functions (NMS, IoU)
โ”‚   โ”œโ”€โ”€ cli/                # CLI module
โ”‚   โ”‚   โ”œโ”€โ”€ mod.rs          # CLI module exports
โ”‚   โ”‚   โ”œโ”€โ”€ args.rs         # CLI argument parsing
โ”‚   โ”‚   โ””โ”€โ”€ predict.rs      # Predict command implementation
โ”‚   โ””โ”€โ”€ visualizer/         # Real-time visualization (minifb)
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ integration_test.rs # Integration tests
โ”œโ”€โ”€ examples/               # Runnable library examples
โ”‚   โ”œโ”€โ”€ basic.rs            # Load a model, run inference, print detections
โ”‚   โ”œโ”€โ”€ config.rs           # Set confidence, IoU, image size, and device
โ”‚   โ”œโ”€โ”€ tasks.rs            # Summary for detect, segment, pose, obb, classify
โ”‚   โ”œโ”€โ”€ annotate.rs         # Draw boxes and labels, save the annotated image
โ”‚   โ””โ”€โ”€ README.md           # Examples guide
โ”œโ”€โ”€ assets/                 # Test images
โ”‚   โ”œโ”€โ”€ boats.jpg
โ”‚   โ”œโ”€โ”€ bus.jpg
โ”‚   โ””โ”€โ”€ zidane.jpg
โ”œโ”€โ”€ crates/web/             # ultralytics-inference-web: wasm32 WebGPU bindings
โ”œโ”€โ”€ web/                    # @ultralytics/yolo npm package (TypeScript wrapper)
โ”œโ”€โ”€ docs/                   # Additional guides
โ”‚   โ”œโ”€โ”€ CUDA.md             # NVIDIA CUDA and TensorRT setup
โ”‚   โ””โ”€โ”€ DGX.md              # DGX Spark (aarch64) setup and benchmarks
โ”œโ”€โ”€ Cargo.toml              # Rust dependencies and features
โ”œโ”€โ”€ LICENSE                 # AGPL-3.0 License
โ”œโ”€โ”€ README.md               # English README
โ””โ”€โ”€ README.zh-CN.md         # Simplified Chinese README
```

## โšก Hardware Acceleration

Enable hardware acceleration by adding features to your build:

```bash
# NVIDIA GPU (CUDA)
cargo build --release --features cuda

# NVIDIA TensorRT
cargo build --release --features tensorrt

# NVIDIA GPU preprocessing + zero-copy TensorRT input (fastest; needs CUDA toolkit)
cargo build --release --features cuda-preprocess

# Apple CoreML (macOS/iOS)
cargo build --release --features coreml

# Intel OpenVINO (select the target hardware with intel:cpu, intel:gpu, or intel:npu)
cargo build --release --features openvino
ultralytics-inference predict --source bus.jpg --device intel:gpu

# Multiple features
cargo build --release --features "cuda,tensorrt"
```

> NVIDIA setup, requirements, and the GPU preprocessing fast path are documented in [`docs/CUDA.md`](docs/CUDA.md).

Each accelerator feature links a prebuilt ONNX Runtime containing that provider. Not every combination is published, and asking for one that is not stops the build with `no builds available that satisfy the requested feature set`. To take the closest available build instead of an error, enable `lax-feature-matching` on `ort`:

```toml
[dependencies]
ultralytics-inference = { version = "0.0.39", features = ["coreml", "xnnpack"] }
ort = { version = "=2.0.0-rc.13", features = ["lax-feature-matching"] }
```

Providers missing from the chosen build are then absent at runtime and inference falls back to CPU. On `aarch64-unknown-linux-gnu` only a CPU distribution is published, so every GPU feature hits this; see [`docs/DGX.md`](docs/DGX.md) for linking a GPU ONNX Runtime instead of falling back.

> The CUDA and TensorRT binaries are built against CUDA 13, and no CUDA 12 build is published. To run them on CUDA 12, compile ONNX Runtime yourself and link it with `ORT_LIB_PATH`.

**Available Features:**

Default features (enabled unless `--no-default-features` is passed): `annotate`, `visualize`.

| Feature           | Description                                                                                           |
| ----------------- | ----------------------------------------------------------------------------------------------------- |
| `annotate`        | Image annotation for `--save` (default)                                                               |
| `visualize`       | Real-time window display for `--show` (default)                                                       |
| `video`           | Video file decoding/encoding (requires FFmpeg)                                                        |
| `cuda`            | NVIDIA CUDA support                                                                                   |
| `tensorrt`        | NVIDIA TensorRT optimization                                                                          |
| `cuda-preprocess` | GPU preprocessing + zero-copy TensorRT input (needs CUDA toolkit; see [`docs/CUDA.md`](docs/CUDA.md)) |
| `coreml`          | Apple CoreML (macOS/iOS)                                                                              |
| `openvino`        | Intel OpenVINO                                                                                        |
| `onednn`          | Intel oneDNN                                                                                          |
| `rocm`            | AMD ROCm                                                                                              |
| `migraphx`        | AMD MIGraphX                                                                                          |
| `directml`        | DirectML (Windows)                                                                                    |
| `nnapi`           | Android Neural Networks API                                                                           |
| `qnn`             | Qualcomm Neural Networks                                                                              |
| `xnnpack`         | XNNPACK (cross-platform)                                                                              |
| `acl`             | ARM Compute Library                                                                                   |
| `tvm`             | Apache TVM                                                                                            |
| `rknpu`           | Rockchip NPU                                                                                          |
| `cann`            | Huawei CANN                                                                                           |
| `webgpu`          | WebGPU                                                                                                |
| `azure`           | Azure                                                                                                 |
| `nvidia`          | Convenience: CUDA + TensorRT                                                                          |
| `amd`             | Convenience: ROCm + MIGraphX                                                                          |
| `intel`           | Convenience: OpenVINO + oneDNN                                                                        |
| `mobile`          | Convenience: NNAPI + CoreML + QNN                                                                     |
| `all`             | Convenience: annotate + visualize + video                                                             |

## ๐ŸŒ Browser / WebGPU (WASM)

[![npm version](https://img.shields.io/npm/v/@ultralytics/yolo?logo=npm&logoColor=white&label=npm&color=CB3837)](https://www.npmjs.com/package/@ultralytics/yolo)
[![npm downloads](https://img.shields.io/npm/dm/@ultralytics/yolo?logo=npm&logoColor=white&label=downloads&color=CB3837)](https://www.npmjs.com/package/@ultralytics/yolo)

The same engine runs in the browser on **WebGPU**, compiled to WebAssembly. The
shared Rust preprocessing and postprocessing run in wasm, so results match the
native path, while the forward pass runs on a pluggable backend: the official
ONNX Runtime Web build (bridged through [`ort-web`](https://ort.pyke.io/backends/web))
for `.onnx` models, or [**LiteRT.js**](https://developers.google.com/edge/litert/web)
for `.tflite` models.

It ships as the [`@ultralytics/yolo`](web/README.md) npm package:

```ts
import { YOLO } from "@ultralytics/yolo";

const model = await YOLO.load("/models/yolo26n.onnx");
const results = await model.predict("bus.jpg");
console.log(results.boxes); // [{ x1, y1, x2, y2, conf, cls, name, color }, ...]
```

Pass `{ device: "webgpu" | "cpu" }` to pick the accelerator (`"auto"` is the
default), and read `model.device` to see what actually ran.

The backend is picked automatically from the model format (its extension when
available, otherwise the model bytes), so switching is just a matter of the model
you load. LiteRT.js (Google's LiteRT for Web) is optional and
often **~2ร— faster than ONNX Runtime Web on WebGPU** โ€” point `YOLO.load` at an
Ultralytics `.tflite` export and `npm install @litertjs/core` alongside the package.
See the [LiteRT.js section](web/README.md#-litertjs-backend) for details.

The browser bindings live in [`crates/web`](crates/web) (the
`ultralytics-inference-web` cdylib); the JS/TS wrapper and build instructions are
in [`web/`](web/README.md). A WebGPU-capable browser and a secure context
(`https`/`localhost`) are required.

## ๐Ÿ“ฆ Dependencies

One of the key benefits of this library is a Rust/ONNX Runtime stack with no PyTorch, TensorFlow, or Python runtime required.

### Core Dependencies (always included)

| Crate               | Purpose                         |
| ------------------- | ------------------------------- |
| `ort`               | ONNX Runtime bindings           |
| `ndarray`           | N-dimensional arrays            |
| `image`             | Image loading/decoding          |
| `jpeg-decoder`      | JPEG decoding                   |
| `fast_image_resize` | SIMD-optimized resizing         |
| `half`              | FP16 support                    |
| `lru`               | LRU cache for preprocessing LUT |
| `wide`              | SIMD for fast preprocessing     |

### Optional Dependencies (for the `annotate` feature)

| Crate       | Purpose                        |
| ----------- | ------------------------------ |
| `imageproc` | Drawing boxes and shapes       |
| `ab_glyph`  | Text rendering (embedded font) |

### Optional Dependencies (for Video & Visualization)

| Crate      | Purpose                            |
| ---------- | ---------------------------------- |
| `minifb`   | Window creation and buffer display |
| `video-rs` | Video decoding/encoding (ffmpeg)   |

### Video Support (FFmpeg)

Video features require FFmpeg (6, 7 or 8) installed on your system:

```bash
# macOS
brew install ffmpeg

# Ubuntu/Debian
apt-get install -y ffmpeg libavutil-dev libavformat-dev libavfilter-dev libavdevice-dev libclang-dev

# Build with video support
cargo build --release --features video
```

To build without annotation and visualization support (smaller binary):

```bash
cargo build --release --no-default-features
```

## ๐Ÿงช Testing

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_boxes_creation
```

## ๐Ÿ“Š Performance

Benchmarks on Apple M4 MacBook Pro (CPU, ONNX Runtime):

### YOLO26n Detection Model (640x640)

| Precision | Model Size | Preprocess | Inference | Postprocess | Total |
| --------- | ---------- | ---------- | --------- | ----------- | ----- |
| FP32      | 10.2 MB    | ~9ms       | ~21ms     | <1ms        | ~31ms |
| FP16      | 5.2 MB     | ~9ms       | ~24ms     | <1ms        | ~34ms |

**Key findings:**

- **FP16 models are ~50% smaller** (5.2 MB vs 10.2 MB)
- **FP32 is slightly faster on CPU** (~21ms vs ~24ms) due to the CPU's native FP32 support
- FP16 requires upcasting to FP32 for computation on most CPUs, adding overhead
- Use **FP32 for CPU** inference, **FP16 for GPU** (where it provides speedup)

### Threading Optimization

ONNX Runtime threading is set to auto (`num_threads: 0`), which lets ORT choose the optimal thread count:

- Manual threading (4 threads): ~40ms inference
- Auto threading (0 = ORT decides): ~21ms inference

## ๐Ÿ”ฎ Roadmap

### Completed

- [x] Detection, Segmentation, Pose, Classification, OBB, Semantic Segmentation, and Depth Estimation inference
- [x] ONNX model metadata parsing (auto-detect classes, task, imgsz)
- [x] Hardware acceleration support (CUDA, TensorRT, CoreML, OpenVINO, XNNPACK)
- [x] Ultralytics-compatible Results API (`Boxes`, `Masks`, `Keypoints`, `Probs`, `Obb`, `SemanticMask`, `DepthMap`)
- [x] Multiple input sources (images, directories, globs, URLs)
- [x] Video file support and webcam/RTSP streaming
- [x] Image annotation and visualization
- [x] FP16 inference
- [x] Batch inference support
- [x] Rectangular inference support and optimization
- [x] Class filtering support
- [x] Auto-download all YOLO26, YOLO11, and YOLOv8 ONNX models (all sizes n/s/m/l/x, all tasks)
- [x] `--task` CLI flag: selects and auto-downloads the matching nano model when `--model` is omitted; errors on task/model metadata conflict
- [x] WebAssembly (WASM) browser inference on WebGPU (npm [`@ultralytics/yolo`](https://www.npmjs.com/package/@ultralytics/yolo))

### In Progress

- [ ] Python bindings (PyO3)

## ๐Ÿ’ก Contribute

Ultralytics thrives on community collaboration, and we deeply value your contributions! Whether it's reporting bugs,
suggesting features, or submitting code changes, your involvement is crucial.

- **Report Issues**: Found a bug? [Open an issue](https://github.com/ultralytics/inference/issues)
- **Feature Requests**: Have an idea? [Share it](https://github.com/ultralytics/inference/issues)
- **Pull Requests**: Read our [Contributing Guide](https://docs.ultralytics.com/help/contributing) first
- **Feedback**: Take our [Survey](https://www.ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey)

A heartfelt thank you ๐Ÿ™ goes out to all our contributors! Your efforts help make Ultralytics tools better for everyone.

[![Ultralytics open-source contributors](https://raw.githubusercontent.com/ultralytics/assets/main/im/image-contributors.png)](https://github.com/ultralytics/ultralytics/graphs/contributors)

## ๐Ÿ“„ License

Ultralytics offers two licensing options to suit different needs:

- **AGPL-3.0 License**: This [OSI-approved](https://opensource.org/license/agpl-3.0) open-source license is perfect for students, researchers, and enthusiasts. It encourages open collaboration and knowledge sharing. See the [LICENSE](https://github.com/ultralytics/inference/blob/main/LICENSE) file for full details.
- **Ultralytics Enterprise License**: Designed for commercial use, this license allows for the seamless integration of Ultralytics software and AI models into commercial products and services, bypassing the open-source requirements of AGPL-3.0. If your use case involves commercial deployment, please contact us via [Ultralytics Licensing](https://www.ultralytics.com/license).

## ๐Ÿ“ฎ Contact

- **GitHub Issues**: [Bug reports and feature requests](https://github.com/ultralytics/inference/issues)
- **Discord**: [Join our community](https://discord.com/invite/ultralytics)
- **Documentation**: [docs.ultralytics.com](https://docs.ultralytics.com)

<br>
<div align="center">
  <a href="https://github.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-github.png" width="3%" alt="Ultralytics GitHub"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://www.linkedin.com/company/ultralytics/"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-linkedin.png" width="3%" alt="Ultralytics LinkedIn"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://x.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-twitter.png" width="3%" alt="Ultralytics Twitter"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://www.youtube.com/ultralytics?sub_confirmation=1"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-youtube.png" width="3%" alt="Ultralytics YouTube"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://www.tiktok.com/@ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-tiktok.png" width="3%" alt="Ultralytics TikTok"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://ultralytics.com/bilibili"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-bilibili.png" width="3%" alt="Ultralytics BiliBili"></a>
  <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
  <a href="https://discord.com/invite/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-discord.png" width="3%" alt="Ultralytics Discord"></a>
</div>