@ultralytics/yolo 0.0.24 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -7
- package/dist/index.d.ts +24 -8
- package/dist/index.js +249 -24
- package/package.json +10 -1
- package/pkg/package.json +1 -1
- package/pkg/ultralytics_inference_web.d.ts +94 -6
- package/pkg/ultralytics_inference_web.js +234 -17
- package/pkg/ultralytics_inference_web_bg.wasm +0 -0
- package/pkg/ultralytics_inference_web_bg.wasm.d.ts +12 -4
package/README.md
CHANGED
|
@@ -75,6 +75,14 @@ const results = await model.predict(canvas, { conf: 0.25, iou: 0.7 });
|
|
|
75
75
|
console.log(model.device); // "webgpu" or "cpu"
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
+
`YOLO.load` also takes a `Blob`/`File`, so you can load a model the user drops or
|
|
79
|
+
picks. The backend is detected from the bytes, so the same call handles `.onnx`
|
|
80
|
+
and `.tflite`:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const model = await YOLO.load(fileInput.files[0]); // a dropped/picked .onnx or .tflite
|
|
84
|
+
```
|
|
85
|
+
|
|
78
86
|
### Webcam / Video
|
|
79
87
|
|
|
80
88
|
Drawable sources (`<video>`, canvas, `ImageBitmap`, `ImageData`) take a
|
|
@@ -97,9 +105,9 @@ async function frame() {
|
|
|
97
105
|
<br>
|
|
98
106
|
<br>
|
|
99
107
|
|
|
100
|
-
Runs [YOLOv8](https://docs.ultralytics.com/models/yolov8),
|
|
101
|
-
[YOLO11](https://docs.ultralytics.com/models/yolo11), and
|
|
102
|
-
[YOLO26](https://docs.ultralytics.com/models/yolo26) ONNX exports for detection,
|
|
108
|
+
Runs [Ultralytics YOLOv8](https://docs.ultralytics.com/models/yolov8),
|
|
109
|
+
[Ultralytics YOLO11](https://docs.ultralytics.com/models/yolo11), and
|
|
110
|
+
[Ultralytics YOLO26](https://docs.ultralytics.com/models/yolo26) ONNX exports for detection,
|
|
103
111
|
segmentation, pose, OBB, classification, and semantic segmentation.
|
|
104
112
|
|
|
105
113
|
Pass a bare ONNX name and it is **auto-downloaded** from the
|
|
@@ -110,8 +118,8 @@ same weights the native crate and Python use):
|
|
|
110
118
|
await YOLO.load("yolo26n.onnx"); // auto-downloads from the release: .../download/v8.4.0/yolo26n.onnx
|
|
111
119
|
```
|
|
112
120
|
|
|
113
|
-
Auto-download covers **
|
|
114
|
-
with task suffixes `-seg`, `-pose`, `-cls`, `-obb`, and `-sem` (semantic,
|
|
121
|
+
Auto-download covers **Ultralytics YOLO26**, **Ultralytics YOLO11**, and **Ultralytics YOLOv8** in sizes `n/s/m/l/x`
|
|
122
|
+
with task suffixes `-seg`, `-pose`, `-cls`, `-obb`, and `-sem` (semantic, Ultralytics YOLO26
|
|
115
123
|
only). A value containing a `/` or a scheme is used as a URL/path as-is.
|
|
116
124
|
|
|
117
125
|
> **CORS note:** GitHub release assets do not send `Access-Control-Allow-Origin`,
|
|
@@ -181,6 +189,86 @@ the native renderer. None of this is duplicated in JS.
|
|
|
181
189
|
creation. See the [ort-web docs](https://ort.pyke.io/backends/web) to review or
|
|
182
190
|
disable it.
|
|
183
191
|
|
|
192
|
+
## ⚡ LiteRT.js backend
|
|
193
|
+
|
|
194
|
+
An alternative inference engine that runs an Ultralytics **`.tflite`** export
|
|
195
|
+
through [**LiteRT.js**](https://developers.google.com/edge/litert/web) (Google's
|
|
196
|
+
LiteRT for Web), which is often **~2× faster than ONNX Runtime Web on WebGPU**.
|
|
197
|
+
Only the inference engine changes; the preprocessing, postprocessing, drawing,
|
|
198
|
+
and `Results` shape are the same shared Rust code, so output matches the `ort`
|
|
199
|
+
path.
|
|
200
|
+
|
|
201
|
+
The backend is picked from the file extension: a `.tflite` runs on LiteRT.js, a
|
|
202
|
+
`.onnx` on ONNX Runtime Web. The LiteRT.js wasm loads from a CDN by default, so
|
|
203
|
+
the only setup is making `@litertjs/core` resolve (along with its `@litertjs/wasm-utils`
|
|
204
|
+
dependency, which npm installs automatically and the import map below lists explicitly).
|
|
205
|
+
|
|
206
|
+
**With npm (a bundler):**
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
npm install @ultralytics/yolo @litertjs/core
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { YOLO, annotate } from "@ultralytics/yolo";
|
|
214
|
+
|
|
215
|
+
const model = await YOLO.load("/models/yolo26n.tflite"); // .tflite -> LiteRT.js
|
|
216
|
+
const results = await model.predict("bus.jpg");
|
|
217
|
+
await annotate(document.querySelector("canvas"), "bus.jpg", results);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**Without a build step (CDN):** map the modules to a CDN, then use the exact same
|
|
221
|
+
code as above:
|
|
222
|
+
|
|
223
|
+
```html
|
|
224
|
+
<script type="importmap">
|
|
225
|
+
{
|
|
226
|
+
"imports": {
|
|
227
|
+
"@ultralytics/yolo": "https://esm.sh/@ultralytics/yolo",
|
|
228
|
+
"@litertjs/core": "https://esm.sh/@litertjs/core",
|
|
229
|
+
"@litertjs/wasm-utils": "https://esm.sh/@litertjs/wasm-utils"
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
</script>
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
For webcam or video, pass the `<video>` element each frame:
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
const results = await model.predict(video);
|
|
239
|
+
await annotate(canvas, video, results);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
The wasm loads from the jsDelivr CDN by default; pass `litertWasmUrl: "/litert/"` to
|
|
243
|
+
`YOLO.load` to self-host it (copy `node_modules/@litertjs/core/wasm/`).
|
|
244
|
+
|
|
245
|
+
Notes:
|
|
246
|
+
|
|
247
|
+
- **Model**: export with Ultralytics to `.tflite` (float32 for WebGPU). It loads
|
|
248
|
+
from the single file. The metadata (task, class names, `imgsz`, stride) is read
|
|
249
|
+
straight from the `.tflite`, the same as the `.onnx` path. No sidecar.
|
|
250
|
+
- **Requires Ultralytics `>= 8.4.83`**: the single-file LiteRT export (with
|
|
251
|
+
embedded metadata) ships in
|
|
252
|
+
[v8.4.83](https://github.com/ultralytics/ultralytics/releases/tag/v8.4.83) and
|
|
253
|
+
later. Earlier versions emit the legacy TFLite format and won't load here.
|
|
254
|
+
- **Export end2end-free models** (`end2end=False`): Ultralytics YOLO26 defaults to an
|
|
255
|
+
end-to-end, NMS-free head whose `int64` / `gather_nd` ops the LiteRT
|
|
256
|
+
**WebGPU** delegate cannot run, so those exports silently fall back to CPU/wasm.
|
|
257
|
+
Export them with `end2end=False` so the standard head is used and NMS runs in
|
|
258
|
+
this package's Rust, keeping inference on WebGPU:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
yolo export model=yolo26n.pt format=litert end2end=False
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
If you load an end2end `.tflite` anyway, the backend auto-switches it to wasm
|
|
265
|
+
(slower) and logs a warning rather than returning empty results.
|
|
266
|
+
|
|
267
|
+
- **Tasks**: detect, segment, pose, obb, classify, and semantic are all supported.
|
|
268
|
+
- **Cross-origin isolation**: LiteRT's threaded wasm wants `SharedArrayBuffer`,
|
|
269
|
+
so serve with `Cross-Origin-Opener-Policy: same-origin` and
|
|
270
|
+
`Cross-Origin-Embedder-Policy: require-corp`.
|
|
271
|
+
|
|
184
272
|
## 🔨 Building From Source
|
|
185
273
|
|
|
186
274
|
This package builds the wasm from the Rust crate with
|
|
@@ -190,8 +278,8 @@ This package builds the wasm from the Rust crate with
|
|
|
190
278
|
npm run build # wasm-pack build + tsc
|
|
191
279
|
```
|
|
192
280
|
|
|
193
|
-
Serve the built
|
|
194
|
-
in a WebGPU browser.
|
|
281
|
+
Serve the built package over `localhost` (a secure context) with the two
|
|
282
|
+
cross-origin isolation headers above, then open it in a WebGPU browser.
|
|
195
283
|
|
|
196
284
|
## 💡 Contributing
|
|
197
285
|
|
package/dist/index.d.ts
CHANGED
|
@@ -84,7 +84,16 @@ export interface LoadOptions {
|
|
|
84
84
|
* falls back to CPU; read {@link YOLO.device} to see what actually ran.
|
|
85
85
|
*/
|
|
86
86
|
device?: "auto" | "webgpu" | "cpu";
|
|
87
|
+
/**
|
|
88
|
+
* Base URL of the `@litertjs/core` wasm assets folder, used when the model is a
|
|
89
|
+
* `.tflite` (run through LiteRT.js). Defaults to the jsDelivr CDN. Self-host by
|
|
90
|
+
* copying `node_modules/@litertjs/core/wasm/` and pointing here (absolute URL
|
|
91
|
+
* ending in `/`). Requires the optional `@litertjs/core` peer dependency.
|
|
92
|
+
*/
|
|
93
|
+
litertWasmUrl?: string | URL;
|
|
87
94
|
}
|
|
95
|
+
/** A model source: a URL/path, raw bytes, or a `Blob`/`File` (e.g. a dropped file). */
|
|
96
|
+
export type ModelSource = string | URL | ArrayBuffer | Uint8Array | Blob;
|
|
88
97
|
/** Options for {@link YOLO.predict}. */
|
|
89
98
|
export interface PredictOptions {
|
|
90
99
|
/** Confidence threshold. Default `0.25` (matches Ultralytics). */
|
|
@@ -117,18 +126,25 @@ export type ImageInput = string | URL | Blob | ArrayBuffer | Uint8Array | ImageD
|
|
|
117
126
|
* of a constructor.
|
|
118
127
|
*/
|
|
119
128
|
export declare class YOLO {
|
|
120
|
-
private readonly
|
|
129
|
+
private readonly engine;
|
|
121
130
|
private constructor();
|
|
122
131
|
/**
|
|
123
|
-
* Load a model and initialize the
|
|
132
|
+
* Load a model and initialize the inference backend.
|
|
133
|
+
*
|
|
134
|
+
* The engine is chosen from the model: a `.tflite` runs on LiteRT.js, an
|
|
135
|
+
* `.onnx` on ONNX Runtime Web.
|
|
124
136
|
*
|
|
125
|
-
* @param source
|
|
126
|
-
*
|
|
137
|
+
* @param source Model URL/path, raw bytes, or a `Blob`/`File` (e.g. from a file
|
|
138
|
+
* input or drag-and-drop). `.onnx` or `.tflite`.
|
|
139
|
+
* @param options Loader options.
|
|
127
140
|
*/
|
|
128
|
-
static load(source:
|
|
141
|
+
static load(source: ModelSource, options?: LoadOptions): Promise<YOLO>;
|
|
142
|
+
/** Load the LiteRT (`.tflite`) backend. Metadata (task, class names, `imgsz`)
|
|
143
|
+
* is read from the single `.tflite` file, the same as the `.onnx` path. */
|
|
144
|
+
private static loadLiteRt;
|
|
129
145
|
/** The model's task (`detect`, `segment`, `pose`, `classify`, `obb`, `semantic`). */
|
|
130
146
|
get task(): string;
|
|
131
|
-
/** The active device that ran inference (`"webgpu"` or `"cpu"`). */
|
|
147
|
+
/** The active device that ran inference (`"webgpu"`, `"wasm"`, or `"cpu"`). */
|
|
132
148
|
get device(): string;
|
|
133
149
|
/** Class id -> name map (like Ultralytics `model.names`). */
|
|
134
150
|
get names(): Record<number, string>;
|
|
@@ -138,13 +154,13 @@ export declare class YOLO {
|
|
|
138
154
|
* Drawable sources (`ImageData`, `HTMLImageElement`, `HTMLCanvasElement`,
|
|
139
155
|
* `HTMLVideoElement`, `ImageBitmap`) take a fast path that reads raw pixels
|
|
140
156
|
* with no re-encoding, ideal for webcam/video. URLs, `Blob`s, and raw bytes
|
|
141
|
-
* are decoded
|
|
157
|
+
* are decoded (in wasm for `ort`, via a canvas for `litert`).
|
|
142
158
|
*
|
|
143
159
|
* @param image The image to run on.
|
|
144
160
|
* @param options Confidence/IoU thresholds.
|
|
145
161
|
*/
|
|
146
162
|
predict(image: ImageInput, options?: PredictOptions): Promise<Results>;
|
|
147
|
-
/** Release the underlying wasm model. Call when you are done with it. */
|
|
163
|
+
/** Release the underlying wasm model/engine. Call when you are done with it. */
|
|
148
164
|
free(): void;
|
|
149
165
|
}
|
|
150
166
|
/**
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* }
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
import init, { YoloModel, start, pose_palette } from "../pkg/ultralytics_inference_web.js";
|
|
20
|
+
import init, { YoloModel, YoloPipeline, start, pose_palette } from "../pkg/ultralytics_inference_web.js";
|
|
21
21
|
let poseSchemeCache = null;
|
|
22
22
|
/** The Ultralytics pose skeleton + per-keypoint/limb colors (constant). */
|
|
23
23
|
function poseScheme() {
|
|
@@ -55,6 +55,26 @@ const DEFAULT_KEYPOINT_THRESHOLD = 0.25;
|
|
|
55
55
|
function resolveModel(src) {
|
|
56
56
|
return /^[\w.-]+\.onnx$/i.test(src) ? ASSETS + src : src;
|
|
57
57
|
}
|
|
58
|
+
/** Whether `bytes` begin with a TFLite flatbuffer (the `TFL3` identifier at offset 4). */
|
|
59
|
+
function isTflite(bytes) {
|
|
60
|
+
return bytes.length >= 8 && bytes[4] === 0x54 && bytes[5] === 0x46 && bytes[6] === 0x4c && bytes[7] === 0x33;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Pick the inference backend from the model: `.tflite` -> `litert`, `.onnx` ->
|
|
64
|
+
* `ort`. When the source has no known extension (raw bytes, a `Blob`/`File`, or an
|
|
65
|
+
* extensionless/signed URL), the already-fetched `bytes` are sniffed for `TFL3`.
|
|
66
|
+
*/
|
|
67
|
+
function inferBackend(source, bytes) {
|
|
68
|
+
if (typeof source === "string" || source instanceof URL) {
|
|
69
|
+
// Match on the pathname only so `model.tflite?v=1` or a signed URL still works.
|
|
70
|
+
const path = (source instanceof URL ? source.pathname : source).split(/[?#]/, 1)[0];
|
|
71
|
+
if (/\.tflite$/i.test(path))
|
|
72
|
+
return "litert";
|
|
73
|
+
if (/\.onnx$/i.test(path))
|
|
74
|
+
return "ort";
|
|
75
|
+
}
|
|
76
|
+
return isTflite(bytes) ? "litert" : "ort";
|
|
77
|
+
}
|
|
58
78
|
/**
|
|
59
79
|
* Reinterpret the wasm payload's little-endian `semantic_mask` bytes as a
|
|
60
80
|
* `Uint16Array` of class ids (a zero-copy view), leaving every other field as-is.
|
|
@@ -136,46 +156,251 @@ function get2d(canvas, options) {
|
|
|
136
156
|
throw new Error("could not acquire a 2D canvas context");
|
|
137
157
|
return ctx;
|
|
138
158
|
}
|
|
159
|
+
/** Default CDN for the LiteRT.js wasm assets (override via `litertWasmUrl`). */
|
|
160
|
+
const DEFAULT_LITERT_WASM = "https://cdn.jsdelivr.net/npm/@litertjs/core/wasm/";
|
|
161
|
+
/** Import the optional `@litertjs/core` peer dep. An indirect specifier keeps it
|
|
162
|
+
* out of the build graph, so the package never hard-depends on it. */
|
|
163
|
+
async function importLiteRt() {
|
|
164
|
+
const pkg = "@litertjs/core";
|
|
165
|
+
try {
|
|
166
|
+
return (await import(/* @vite-ignore */ pkg));
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
// Keep the original error visible so a real load failure is not mistaken for
|
|
170
|
+
// a missing install.
|
|
171
|
+
throw new Error(`failed to load the optional peer dependency "@litertjs/core" for the .tflite backend (run \`npm install @litertjs/core\`): ${e}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// LiteRT.js initializes its wasm runtime once per page; calling `loadLiteRt`
|
|
175
|
+
// again throws "LiteRT is already loading / loaded". Cache the first init so
|
|
176
|
+
// loading a second model (e.g. switching models in a demo) reuses it. The init
|
|
177
|
+
// is process-global, so a later load asking for a different `wasmUrl` is rejected
|
|
178
|
+
// (reload to switch) rather than silently reusing the original assets.
|
|
179
|
+
let litertInit = null;
|
|
180
|
+
let litertInitUrl = null;
|
|
181
|
+
function ensureLiteRtRuntime(litert, wasmUrl) {
|
|
182
|
+
if (litertInit) {
|
|
183
|
+
if (litertInitUrl !== wasmUrl) {
|
|
184
|
+
return Promise.reject(new Error(`LiteRT.js wasm is initialized once per page and shared by every model: already loaded from "${litertInitUrl}", cannot switch to "${wasmUrl}". Use the same litertWasmUrl, or reload the page.`));
|
|
185
|
+
}
|
|
186
|
+
return litertInit;
|
|
187
|
+
}
|
|
188
|
+
litertInitUrl = wasmUrl;
|
|
189
|
+
litertInit = litert.loadLiteRt(wasmUrl).catch((e) => {
|
|
190
|
+
litertInit = null; // let a later load retry if the first init failed
|
|
191
|
+
litertInitUrl = null;
|
|
192
|
+
throw e;
|
|
193
|
+
});
|
|
194
|
+
return litertInit;
|
|
195
|
+
}
|
|
196
|
+
/** Runs a `.tflite` model with LiteRT.js; inference only, pre/post stay in Rust. */
|
|
197
|
+
class LiteRtBackend {
|
|
198
|
+
constructor(litert, model, device) {
|
|
199
|
+
this.litert = litert;
|
|
200
|
+
this.model = model;
|
|
201
|
+
this.device = device;
|
|
202
|
+
}
|
|
203
|
+
/** Import LiteRT.js, init its wasm, and compile the model, falling back from
|
|
204
|
+
* WebGPU to wasm (CPU) if the GPU accelerator cannot compile. */
|
|
205
|
+
static async load(tflite, wasmUrl, accelerator) {
|
|
206
|
+
const litert = await importLiteRt();
|
|
207
|
+
await ensureLiteRtRuntime(litert, wasmUrl);
|
|
208
|
+
try {
|
|
209
|
+
const model = await litert.loadAndCompile(tflite, { accelerator });
|
|
210
|
+
return new LiteRtBackend(litert, model, accelerator);
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
if (accelerator === "wasm")
|
|
214
|
+
throw e;
|
|
215
|
+
const model = await litert.loadAndCompile(tflite, { accelerator: "wasm" });
|
|
216
|
+
return new LiteRtBackend(litert, model, "wasm");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** Upload the input tensor, run, and read every output back to the CPU. */
|
|
220
|
+
async run(input, shape) {
|
|
221
|
+
const t0 = performance.now();
|
|
222
|
+
const cpuIn = new this.litert.Tensor(input, shape);
|
|
223
|
+
let gpuIn;
|
|
224
|
+
try {
|
|
225
|
+
gpuIn = this.device === "wasm" ? cpuIn : await cpuIn.moveTo("webgpu");
|
|
226
|
+
const tensors = await this.model.run(gpuIn);
|
|
227
|
+
const outputs = [];
|
|
228
|
+
const shapes = [];
|
|
229
|
+
for (const t of tensors) {
|
|
230
|
+
const host = await t.moveTo("wasm");
|
|
231
|
+
// Copy out before deleting; toTypedArray() may view the freed buffer.
|
|
232
|
+
outputs.push(new Float32Array(host.toTypedArray()));
|
|
233
|
+
shapes.push(Array.from(host.type.layout.dimensions));
|
|
234
|
+
host.delete?.();
|
|
235
|
+
// moveTo("wasm") returns the same tensor when it is already on wasm; only
|
|
236
|
+
// delete the original separately when it is a distinct GPU tensor.
|
|
237
|
+
if (t !== host)
|
|
238
|
+
t.delete?.();
|
|
239
|
+
}
|
|
240
|
+
return { outputs, shapes, inferenceMs: performance.now() - t0 };
|
|
241
|
+
}
|
|
242
|
+
finally {
|
|
243
|
+
// Free the input tensors on both success and failure (per-frame loops leak fast).
|
|
244
|
+
if (gpuIn && gpuIn !== cpuIn)
|
|
245
|
+
gpuIn.delete?.();
|
|
246
|
+
cpuIn.delete?.();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
free() {
|
|
250
|
+
this.model.delete?.();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** ONNX Runtime Web engine: the full pipeline runs inside wasm. */
|
|
254
|
+
class OrtEngine {
|
|
255
|
+
constructor(model) {
|
|
256
|
+
this.model = model;
|
|
257
|
+
}
|
|
258
|
+
get task() {
|
|
259
|
+
return this.model.task;
|
|
260
|
+
}
|
|
261
|
+
get device() {
|
|
262
|
+
return this.model.device;
|
|
263
|
+
}
|
|
264
|
+
get names() {
|
|
265
|
+
return this.model.names;
|
|
266
|
+
}
|
|
267
|
+
predictDrawable(data, width, height, conf, iou, classes) {
|
|
268
|
+
return this.model.predict_rgba(data, width, height, conf, iou, classes);
|
|
269
|
+
}
|
|
270
|
+
predictEncoded(bytes, conf, iou, classes) {
|
|
271
|
+
return this.model.predict(bytes, conf, iou, classes);
|
|
272
|
+
}
|
|
273
|
+
free() {
|
|
274
|
+
this.model.free();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** LiteRT.js engine: Rust preprocess → LiteRT inference → Rust postprocess. */
|
|
278
|
+
class LiteRtEngine {
|
|
279
|
+
constructor(pipeline, backend) {
|
|
280
|
+
this.pipeline = pipeline;
|
|
281
|
+
this.backend = backend;
|
|
282
|
+
// `YoloPipeline` keeps exactly one pending frame in wasm state between
|
|
283
|
+
// `preprocess_rgba` and `postprocess`, so predictions must not interleave.
|
|
284
|
+
// Serialize them: a new call waits for the in-flight one to finish.
|
|
285
|
+
this.queue = Promise.resolve();
|
|
286
|
+
}
|
|
287
|
+
get task() {
|
|
288
|
+
return this.pipeline.task;
|
|
289
|
+
}
|
|
290
|
+
get device() {
|
|
291
|
+
// Report the CPU path as "cpu" (the package-wide label); the LiteRT backend
|
|
292
|
+
// tracks it internally as "wasm".
|
|
293
|
+
return this.backend.device === "wasm" ? "cpu" : this.backend.device;
|
|
294
|
+
}
|
|
295
|
+
get names() {
|
|
296
|
+
return this.pipeline.names;
|
|
297
|
+
}
|
|
298
|
+
predictDrawable(data, width, height, conf, iou, classes) {
|
|
299
|
+
return this.serialize(() => this.runDrawable(data, width, height, conf, iou, classes));
|
|
300
|
+
}
|
|
301
|
+
/** LiteRT takes raw pixels, so decode encoded inputs to RGBA in JS first. */
|
|
302
|
+
predictEncoded(bytes, conf, iou, classes) {
|
|
303
|
+
return this.serialize(async () => {
|
|
304
|
+
const bitmap = await createImageBitmap(new Blob([bytes]));
|
|
305
|
+
try {
|
|
306
|
+
const { data, width, height } = toImageData(bitmap);
|
|
307
|
+
return await this.runDrawable(data, width, height, conf, iou, classes);
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
bitmap.close();
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
async runDrawable(data, width, height, conf, iou, classes) {
|
|
315
|
+
const input = this.pipeline.preprocess_rgba(data, width, height);
|
|
316
|
+
const { outputs, shapes, inferenceMs } = await this.backend.run(input, Array.from(this.pipeline.inputShape));
|
|
317
|
+
// Flatten shapes as [rank, dims..., rank, dims...] for the wasm boundary.
|
|
318
|
+
const flatShapes = [];
|
|
319
|
+
for (const s of shapes)
|
|
320
|
+
flatShapes.push(s.length, ...s);
|
|
321
|
+
return this.pipeline.postprocess(outputs, new Uint32Array(flatShapes), inferenceMs, conf, iou, classes);
|
|
322
|
+
}
|
|
323
|
+
/** Chain `fn` after any in-flight prediction so wasm frame state is never shared. */
|
|
324
|
+
serialize(fn) {
|
|
325
|
+
const result = this.queue.then(fn, fn);
|
|
326
|
+
this.queue = result.then(() => undefined, () => undefined);
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
free() {
|
|
330
|
+
this.pipeline.free();
|
|
331
|
+
this.backend.free();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** Fetch model bytes from a URL/path, or pass provided bytes through. */
|
|
335
|
+
async function fetchModelBytes(source) {
|
|
336
|
+
if (typeof source === "string" || source instanceof URL) {
|
|
337
|
+
const resp = await fetchOk(source.toString(), "model");
|
|
338
|
+
return new Uint8Array(await resp.arrayBuffer());
|
|
339
|
+
}
|
|
340
|
+
if (source instanceof Blob)
|
|
341
|
+
return new Uint8Array(await source.arrayBuffer());
|
|
342
|
+
return source instanceof Uint8Array ? source : new Uint8Array(source);
|
|
343
|
+
}
|
|
139
344
|
/**
|
|
140
345
|
* A loaded YOLO model. Loading is asynchronous, so use {@link YOLO.load} instead
|
|
141
346
|
* of a constructor.
|
|
142
347
|
*/
|
|
143
348
|
export class YOLO {
|
|
144
|
-
constructor(
|
|
145
|
-
this.
|
|
349
|
+
constructor(engine) {
|
|
350
|
+
this.engine = engine;
|
|
146
351
|
}
|
|
147
352
|
/**
|
|
148
|
-
* Load a model and initialize the
|
|
353
|
+
* Load a model and initialize the inference backend.
|
|
149
354
|
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
355
|
+
* The engine is chosen from the model: a `.tflite` runs on LiteRT.js, an
|
|
356
|
+
* `.onnx` on ONNX Runtime Web.
|
|
357
|
+
*
|
|
358
|
+
* @param source Model URL/path, raw bytes, or a `Blob`/`File` (e.g. from a file
|
|
359
|
+
* input or drag-and-drop). `.onnx` or `.tflite`.
|
|
360
|
+
* @param options Loader options.
|
|
152
361
|
*/
|
|
153
362
|
static async load(source, options) {
|
|
154
363
|
await ensureInit(options?.wasmUrl);
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
bytes = source instanceof Uint8Array ? source : new Uint8Array(source);
|
|
364
|
+
// Fetch once (a bare `.onnx` name resolves to the Ultralytics assets release),
|
|
365
|
+
// then pick the backend from the extension, or the bytes when it is unknown.
|
|
366
|
+
const bytes = await fetchModelBytes(typeof source === "string" ? resolveModel(source) : source);
|
|
367
|
+
if (inferBackend(source, bytes) === "litert") {
|
|
368
|
+
return YOLO.loadLiteRt(bytes, options);
|
|
163
369
|
}
|
|
164
370
|
const ortBaseUrl = options?.ortBaseUrl ? options.ortBaseUrl.toString() : undefined;
|
|
165
371
|
const device = await resolveDevice(options?.device);
|
|
166
|
-
return new YOLO(await YoloModel.load_bytes(bytes, ortBaseUrl, device));
|
|
372
|
+
return new YOLO(new OrtEngine(await YoloModel.load_bytes(bytes, ortBaseUrl, device)));
|
|
373
|
+
}
|
|
374
|
+
/** Load the LiteRT (`.tflite`) backend. Metadata (task, class names, `imgsz`)
|
|
375
|
+
* is read from the single `.tflite` file, the same as the `.onnx` path. */
|
|
376
|
+
static async loadLiteRt(tflite, options) {
|
|
377
|
+
const pipeline = new YoloPipeline(tflite);
|
|
378
|
+
const wasmUrl = options?.litertWasmUrl ? options.litertWasmUrl.toString() : DEFAULT_LITERT_WASM;
|
|
379
|
+
// Honor `device` including `"auto"`, which feature-detects WebGPU (so a
|
|
380
|
+
// browser without it goes straight to wasm instead of a failed compile).
|
|
381
|
+
let accelerator = (await resolveDevice(options?.device)) === "webgpu" ? "webgpu" : "wasm";
|
|
382
|
+
// End-to-end exports (YOLO26) do NMS/top-k with int64 + gather_nd ops the
|
|
383
|
+
// LiteRT WebGPU delegate cannot run: it fails to invoke and returns zeros.
|
|
384
|
+
// Force CPU/wasm so the model works. For WebGPU speed, re-export the model
|
|
385
|
+
// with `end2end=False` so the standard head (with NMS in Rust) is used.
|
|
386
|
+
if (accelerator === "webgpu" && pipeline.end2end) {
|
|
387
|
+
accelerator = "wasm";
|
|
388
|
+
console.warn("LiteRT: this is an end2end (NMS-free) model; its WebGPU delegate can't run the int64 ops, so it runs on CPU/wasm. Re-export with `end2end=False` for WebGPU.");
|
|
389
|
+
}
|
|
390
|
+
const backend = await LiteRtBackend.load(tflite, wasmUrl, accelerator);
|
|
391
|
+
return new YOLO(new LiteRtEngine(pipeline, backend));
|
|
167
392
|
}
|
|
168
393
|
/** The model's task (`detect`, `segment`, `pose`, `classify`, `obb`, `semantic`). */
|
|
169
394
|
get task() {
|
|
170
|
-
return this.
|
|
395
|
+
return this.engine.task;
|
|
171
396
|
}
|
|
172
|
-
/** The active device that ran inference (`"webgpu"` or `"cpu"`). */
|
|
397
|
+
/** The active device that ran inference (`"webgpu"`, `"wasm"`, or `"cpu"`). */
|
|
173
398
|
get device() {
|
|
174
|
-
return this.
|
|
399
|
+
return this.engine.device;
|
|
175
400
|
}
|
|
176
401
|
/** Class id -> name map (like Ultralytics `model.names`). */
|
|
177
402
|
get names() {
|
|
178
|
-
return this.
|
|
403
|
+
return this.engine.names;
|
|
179
404
|
}
|
|
180
405
|
/**
|
|
181
406
|
* Run inference on an image.
|
|
@@ -183,7 +408,7 @@ export class YOLO {
|
|
|
183
408
|
* Drawable sources (`ImageData`, `HTMLImageElement`, `HTMLCanvasElement`,
|
|
184
409
|
* `HTMLVideoElement`, `ImageBitmap`) take a fast path that reads raw pixels
|
|
185
410
|
* with no re-encoding, ideal for webcam/video. URLs, `Blob`s, and raw bytes
|
|
186
|
-
* are decoded
|
|
411
|
+
* are decoded (in wasm for `ort`, via a canvas for `litert`).
|
|
187
412
|
*
|
|
188
413
|
* @param image The image to run on.
|
|
189
414
|
* @param options Confidence/IoU thresholds.
|
|
@@ -194,14 +419,14 @@ export class YOLO {
|
|
|
194
419
|
const classes = options?.classes ? new Uint32Array(options.classes) : undefined;
|
|
195
420
|
if (isDrawable(image)) {
|
|
196
421
|
const { data, width, height } = toImageData(image);
|
|
197
|
-
return decodeResults(await this.
|
|
422
|
+
return decodeResults(await this.engine.predictDrawable(data, width, height, conf, iou, classes));
|
|
198
423
|
}
|
|
199
424
|
const bytes = await toEncodedBytes(image);
|
|
200
|
-
return decodeResults(await this.
|
|
425
|
+
return decodeResults(await this.engine.predictEncoded(bytes, conf, iou, classes));
|
|
201
426
|
}
|
|
202
|
-
/** Release the underlying wasm model. Call when you are done with it. */
|
|
427
|
+
/** Release the underlying wasm model/engine. Call when you are done with it. */
|
|
203
428
|
free() {
|
|
204
|
-
this.
|
|
429
|
+
this.engine.free();
|
|
205
430
|
}
|
|
206
431
|
}
|
|
207
432
|
// Colors come from the Rust engine (the Ultralytics palette) on each detection,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultralytics/yolo",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "Run Ultralytics YOLO models in the browser on WebGPU (WebAssembly, ONNX Runtime Web via ort-web).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -57,7 +57,16 @@
|
|
|
57
57
|
"url": "https://github.com/ultralytics/inference/issues"
|
|
58
58
|
},
|
|
59
59
|
"homepage": "https://www.ultralytics.com",
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"@litertjs/core": "^2.5.0"
|
|
62
|
+
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"@litertjs/core": {
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
60
68
|
"devDependencies": {
|
|
69
|
+
"@litertjs/core": "^2.5.0",
|
|
61
70
|
"typescript": "^5.4.0"
|
|
62
71
|
}
|
|
63
72
|
}
|
package/pkg/package.json
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* A loaded
|
|
5
|
+
* A loaded ONNX model that runs end to end in wasm (preprocess, inference on
|
|
6
|
+
* ONNX Runtime Web, and postprocess), for the `.onnx` path.
|
|
6
7
|
*
|
|
7
8
|
* Created via [`YoloModel::load_bytes`]; run with [`YoloModel::predict`]. The
|
|
8
|
-
* TypeScript
|
|
9
|
+
* TypeScript `YOLO` class wraps this for ONNX models, and [`YoloPipeline`] for
|
|
10
|
+
* `.tflite` ones.
|
|
9
11
|
*/
|
|
10
12
|
export class YoloModel {
|
|
11
13
|
private constructor();
|
|
@@ -69,6 +71,84 @@ export class YoloModel {
|
|
|
69
71
|
readonly task: string;
|
|
70
72
|
}
|
|
71
73
|
|
|
74
|
+
/**
|
|
75
|
+
* A metadata-only YOLO pre/post pipeline for use with an **external** inference
|
|
76
|
+
* engine (e.g. LiteRT.js running a `.tflite` model in JavaScript).
|
|
77
|
+
*
|
|
78
|
+
* Unlike [`YoloModel`], it holds no ONNX Runtime session: JavaScript loads and
|
|
79
|
+
* runs the model, while this struct reuses the shared Rust preprocessing and
|
|
80
|
+
* postprocessing so results match every other path. Per frame the flow is
|
|
81
|
+
* [`preprocess_rgba`](Self::preprocess_rgba) → (JS engine inference) →
|
|
82
|
+
* [`postprocess`](Self::postprocess). It assumes a single prediction in flight
|
|
83
|
+
* at a time (as the webcam render loop does).
|
|
84
|
+
*/
|
|
85
|
+
export class YoloPipeline {
|
|
86
|
+
free(): void;
|
|
87
|
+
[Symbol.dispose](): void;
|
|
88
|
+
/**
|
|
89
|
+
* Build a pipeline from a single-file `.tflite` model, reading the
|
|
90
|
+
* Ultralytics metadata (task, class names, `imgsz`, ...) embedded in the
|
|
91
|
+
* model bytes, the same way [`YoloModel::load_bytes`] reads it from an ONNX
|
|
92
|
+
* model, so the ONNX and LiteRT paths load identically from one file.
|
|
93
|
+
*
|
|
94
|
+
* # Errors
|
|
95
|
+
* Returns a JS error if the model carries no Ultralytics metadata or it
|
|
96
|
+
* cannot be parsed.
|
|
97
|
+
*/
|
|
98
|
+
constructor(tflite: Uint8Array);
|
|
99
|
+
/**
|
|
100
|
+
* Postprocess an external engine's raw outputs into the standard `Results`.
|
|
101
|
+
*
|
|
102
|
+
* `outputs` are the model's output tensors (one for most tasks; two for
|
|
103
|
+
* segmentation: detection head + mask prototypes). `shapes` encodes each
|
|
104
|
+
* output's dims flat as `[rank0, dims0..., rank1, dims1...]`. `inference_ms`
|
|
105
|
+
* is the time the JS engine reported, used only for the `speed` breakdown.
|
|
106
|
+
* Consumes the frame stashed by the preceding
|
|
107
|
+
* [`preprocess_rgba`](Self::preprocess_rgba).
|
|
108
|
+
*
|
|
109
|
+
* # Errors
|
|
110
|
+
* Returns a JS error if called before `preprocess_rgba`, if `shapes` is
|
|
111
|
+
* malformed, or on serialization failure.
|
|
112
|
+
*/
|
|
113
|
+
postprocess(outputs: Float32Array[], shapes: Uint32Array, inference_ms: number, conf: number, iou: number, classes?: Uint32Array | null): any;
|
|
114
|
+
/**
|
|
115
|
+
* Preprocess raw RGBA pixels (e.g. a webcam `ImageData`) into the NCHW f32
|
|
116
|
+
* input tensor an Ultralytics LiteRT/TFLite model expects, normalized to
|
|
117
|
+
* `[0, 1]`, the same preprocessing as the ONNX path.
|
|
118
|
+
*
|
|
119
|
+
* Returns the tensor as a `Float32Array` (shape `[1, 3, H, W]`); the
|
|
120
|
+
* letterbox geometry and original image are stashed for the matching
|
|
121
|
+
* [`postprocess`](Self::postprocess) call.
|
|
122
|
+
*
|
|
123
|
+
* # Errors
|
|
124
|
+
* Returns a JS error if the buffer size is not `width * height * 4`.
|
|
125
|
+
*/
|
|
126
|
+
preprocess_rgba(rgba: Uint8Array, width: number, height: number): Float32Array;
|
|
127
|
+
/**
|
|
128
|
+
* Whether this is an end-to-end (NMS-free) export, e.g. YOLO26. Its head runs
|
|
129
|
+
* the NMS/top-k with `int64`/`gather_nd` ops that the LiteRT WebGPU delegate
|
|
130
|
+
* cannot execute, so such models must run on the CPU (wasm) accelerator.
|
|
131
|
+
*/
|
|
132
|
+
readonly end2end: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* The model input shape as `[1, 3, H, W]` (NCHW), for sizing the engine's
|
|
135
|
+
* input tensor. Ultralytics LiteRT exports (`ai_edge_torch`) keep the native
|
|
136
|
+
* NCHW layout, matching the ONNX path.
|
|
137
|
+
*/
|
|
138
|
+
readonly inputShape: Uint32Array;
|
|
139
|
+
/**
|
|
140
|
+
* Class id -> name map (like `model.names`), as a JS object.
|
|
141
|
+
*
|
|
142
|
+
* # Errors
|
|
143
|
+
* Returns a JS error only if serialization fails (not expected).
|
|
144
|
+
*/
|
|
145
|
+
readonly names: any;
|
|
146
|
+
/**
|
|
147
|
+
* The model's task (`"detect"`, `"segment"`, ...).
|
|
148
|
+
*/
|
|
149
|
+
readonly task: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
72
152
|
/**
|
|
73
153
|
* Return the Ultralytics pose skeleton + keypoint/limb colors (constant). The
|
|
74
154
|
* JS annotator calls this once to color pose overlays from the shared palette.
|
|
@@ -88,6 +168,7 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
|
|
|
88
168
|
export interface InitOutput {
|
|
89
169
|
readonly memory: WebAssembly.Memory;
|
|
90
170
|
readonly __wbg_yolomodel_free: (a: number, b: number) => void;
|
|
171
|
+
readonly __wbg_yolopipeline_free: (a: number, b: number) => void;
|
|
91
172
|
readonly pose_palette: (a: number) => void;
|
|
92
173
|
readonly yolomodel_device: (a: number, b: number) => void;
|
|
93
174
|
readonly yolomodel_load_bytes: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
|
|
@@ -95,11 +176,18 @@ export interface InitOutput {
|
|
|
95
176
|
readonly yolomodel_predict: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => number;
|
|
96
177
|
readonly yolomodel_predict_rgba: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => number;
|
|
97
178
|
readonly yolomodel_task: (a: number, b: number) => void;
|
|
179
|
+
readonly yolopipeline_end2end: (a: number) => number;
|
|
180
|
+
readonly yolopipeline_inputShape: (a: number, b: number) => void;
|
|
181
|
+
readonly yolopipeline_names: (a: number, b: number) => void;
|
|
182
|
+
readonly yolopipeline_new: (a: number, b: number, c: number) => void;
|
|
183
|
+
readonly yolopipeline_postprocess: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
|
|
184
|
+
readonly yolopipeline_preprocess_rgba: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
185
|
+
readonly yolopipeline_task: (a: number, b: number) => void;
|
|
98
186
|
readonly start: () => void;
|
|
99
|
-
readonly
|
|
100
|
-
readonly
|
|
101
|
-
readonly
|
|
102
|
-
readonly
|
|
187
|
+
readonly __wasm_bindgen_func_elem_1968: (a: number, b: number, c: number, d: number) => void;
|
|
188
|
+
readonly __wasm_bindgen_func_elem_2573: (a: number, b: number, c: number, d: number) => void;
|
|
189
|
+
readonly __wasm_bindgen_func_elem_1968_2: (a: number, b: number, c: number, d: number) => void;
|
|
190
|
+
readonly __wasm_bindgen_func_elem_2583: (a: number, b: number, c: number, d: number) => void;
|
|
103
191
|
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
104
192
|
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
105
193
|
readonly __wbindgen_export3: (a: number) => void;
|
|
@@ -4,10 +4,12 @@ import { trackSessionInit } from './snippets/ort-web-c7b42e3e9fb58316/_telemetry
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* A loaded
|
|
7
|
+
* A loaded ONNX model that runs end to end in wasm (preprocess, inference on
|
|
8
|
+
* ONNX Runtime Web, and postprocess), for the `.onnx` path.
|
|
8
9
|
*
|
|
9
10
|
* Created via [`YoloModel::load_bytes`]; run with [`YoloModel::predict`]. The
|
|
10
|
-
* TypeScript
|
|
11
|
+
* TypeScript `YOLO` class wraps this for ONNX models, and [`YoloPipeline`] for
|
|
12
|
+
* `.tflite` ones.
|
|
11
13
|
*/
|
|
12
14
|
export class YoloModel {
|
|
13
15
|
static __wrap(ptr) {
|
|
@@ -171,6 +173,207 @@ export class YoloModel {
|
|
|
171
173
|
}
|
|
172
174
|
if (Symbol.dispose) YoloModel.prototype[Symbol.dispose] = YoloModel.prototype.free;
|
|
173
175
|
|
|
176
|
+
/**
|
|
177
|
+
* A metadata-only YOLO pre/post pipeline for use with an **external** inference
|
|
178
|
+
* engine (e.g. LiteRT.js running a `.tflite` model in JavaScript).
|
|
179
|
+
*
|
|
180
|
+
* Unlike [`YoloModel`], it holds no ONNX Runtime session: JavaScript loads and
|
|
181
|
+
* runs the model, while this struct reuses the shared Rust preprocessing and
|
|
182
|
+
* postprocessing so results match every other path. Per frame the flow is
|
|
183
|
+
* [`preprocess_rgba`](Self::preprocess_rgba) → (JS engine inference) →
|
|
184
|
+
* [`postprocess`](Self::postprocess). It assumes a single prediction in flight
|
|
185
|
+
* at a time (as the webcam render loop does).
|
|
186
|
+
*/
|
|
187
|
+
export class YoloPipeline {
|
|
188
|
+
__destroy_into_raw() {
|
|
189
|
+
const ptr = this.__wbg_ptr;
|
|
190
|
+
this.__wbg_ptr = 0;
|
|
191
|
+
YoloPipelineFinalization.unregister(this);
|
|
192
|
+
return ptr;
|
|
193
|
+
}
|
|
194
|
+
free() {
|
|
195
|
+
const ptr = this.__destroy_into_raw();
|
|
196
|
+
wasm.__wbg_yolopipeline_free(ptr, 0);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Whether this is an end-to-end (NMS-free) export, e.g. YOLO26. Its head runs
|
|
200
|
+
* the NMS/top-k with `int64`/`gather_nd` ops that the LiteRT WebGPU delegate
|
|
201
|
+
* cannot execute, so such models must run on the CPU (wasm) accelerator.
|
|
202
|
+
* @returns {boolean}
|
|
203
|
+
*/
|
|
204
|
+
get end2end() {
|
|
205
|
+
const ret = wasm.yolopipeline_end2end(this.__wbg_ptr);
|
|
206
|
+
return ret !== 0;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The model input shape as `[1, 3, H, W]` (NCHW), for sizing the engine's
|
|
210
|
+
* input tensor. Ultralytics LiteRT exports (`ai_edge_torch`) keep the native
|
|
211
|
+
* NCHW layout, matching the ONNX path.
|
|
212
|
+
* @returns {Uint32Array}
|
|
213
|
+
*/
|
|
214
|
+
get inputShape() {
|
|
215
|
+
try {
|
|
216
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
217
|
+
wasm.yolopipeline_inputShape(retptr, this.__wbg_ptr);
|
|
218
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
219
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
220
|
+
var v1 = getArrayU32FromWasm0(r0, r1).slice();
|
|
221
|
+
wasm.__wbindgen_export4(r0, r1 * 4, 4);
|
|
222
|
+
return v1;
|
|
223
|
+
} finally {
|
|
224
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Class id -> name map (like `model.names`), as a JS object.
|
|
229
|
+
*
|
|
230
|
+
* # Errors
|
|
231
|
+
* Returns a JS error only if serialization fails (not expected).
|
|
232
|
+
* @returns {any}
|
|
233
|
+
*/
|
|
234
|
+
get names() {
|
|
235
|
+
try {
|
|
236
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
237
|
+
wasm.yolopipeline_names(retptr, this.__wbg_ptr);
|
|
238
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
239
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
240
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
241
|
+
if (r2) {
|
|
242
|
+
throw takeObject(r1);
|
|
243
|
+
}
|
|
244
|
+
return takeObject(r0);
|
|
245
|
+
} finally {
|
|
246
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Build a pipeline from a single-file `.tflite` model, reading the
|
|
251
|
+
* Ultralytics metadata (task, class names, `imgsz`, ...) embedded in the
|
|
252
|
+
* model bytes, the same way [`YoloModel::load_bytes`] reads it from an ONNX
|
|
253
|
+
* model, so the ONNX and LiteRT paths load identically from one file.
|
|
254
|
+
*
|
|
255
|
+
* # Errors
|
|
256
|
+
* Returns a JS error if the model carries no Ultralytics metadata or it
|
|
257
|
+
* cannot be parsed.
|
|
258
|
+
* @param {Uint8Array} tflite
|
|
259
|
+
*/
|
|
260
|
+
constructor(tflite) {
|
|
261
|
+
try {
|
|
262
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
263
|
+
const ptr0 = passArray8ToWasm0(tflite, wasm.__wbindgen_export);
|
|
264
|
+
const len0 = WASM_VECTOR_LEN;
|
|
265
|
+
wasm.yolopipeline_new(retptr, ptr0, len0);
|
|
266
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
267
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
268
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
269
|
+
if (r2) {
|
|
270
|
+
throw takeObject(r1);
|
|
271
|
+
}
|
|
272
|
+
this.__wbg_ptr = r0;
|
|
273
|
+
YoloPipelineFinalization.register(this, this.__wbg_ptr, this);
|
|
274
|
+
return this;
|
|
275
|
+
} finally {
|
|
276
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Postprocess an external engine's raw outputs into the standard `Results`.
|
|
281
|
+
*
|
|
282
|
+
* `outputs` are the model's output tensors (one for most tasks; two for
|
|
283
|
+
* segmentation: detection head + mask prototypes). `shapes` encodes each
|
|
284
|
+
* output's dims flat as `[rank0, dims0..., rank1, dims1...]`. `inference_ms`
|
|
285
|
+
* is the time the JS engine reported, used only for the `speed` breakdown.
|
|
286
|
+
* Consumes the frame stashed by the preceding
|
|
287
|
+
* [`preprocess_rgba`](Self::preprocess_rgba).
|
|
288
|
+
*
|
|
289
|
+
* # Errors
|
|
290
|
+
* Returns a JS error if called before `preprocess_rgba`, if `shapes` is
|
|
291
|
+
* malformed, or on serialization failure.
|
|
292
|
+
* @param {Float32Array[]} outputs
|
|
293
|
+
* @param {Uint32Array} shapes
|
|
294
|
+
* @param {number} inference_ms
|
|
295
|
+
* @param {number} conf
|
|
296
|
+
* @param {number} iou
|
|
297
|
+
* @param {Uint32Array | null} [classes]
|
|
298
|
+
* @returns {any}
|
|
299
|
+
*/
|
|
300
|
+
postprocess(outputs, shapes, inference_ms, conf, iou, classes) {
|
|
301
|
+
try {
|
|
302
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
303
|
+
const ptr0 = passArrayJsValueToWasm0(outputs, wasm.__wbindgen_export);
|
|
304
|
+
const len0 = WASM_VECTOR_LEN;
|
|
305
|
+
const ptr1 = passArray32ToWasm0(shapes, wasm.__wbindgen_export);
|
|
306
|
+
const len1 = WASM_VECTOR_LEN;
|
|
307
|
+
var ptr2 = isLikeNone(classes) ? 0 : passArray32ToWasm0(classes, wasm.__wbindgen_export);
|
|
308
|
+
var len2 = WASM_VECTOR_LEN;
|
|
309
|
+
wasm.yolopipeline_postprocess(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, inference_ms, conf, iou, ptr2, len2);
|
|
310
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
311
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
312
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
313
|
+
if (r2) {
|
|
314
|
+
throw takeObject(r1);
|
|
315
|
+
}
|
|
316
|
+
return takeObject(r0);
|
|
317
|
+
} finally {
|
|
318
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Preprocess raw RGBA pixels (e.g. a webcam `ImageData`) into the NCHW f32
|
|
323
|
+
* input tensor an Ultralytics LiteRT/TFLite model expects, normalized to
|
|
324
|
+
* `[0, 1]`, the same preprocessing as the ONNX path.
|
|
325
|
+
*
|
|
326
|
+
* Returns the tensor as a `Float32Array` (shape `[1, 3, H, W]`); the
|
|
327
|
+
* letterbox geometry and original image are stashed for the matching
|
|
328
|
+
* [`postprocess`](Self::postprocess) call.
|
|
329
|
+
*
|
|
330
|
+
* # Errors
|
|
331
|
+
* Returns a JS error if the buffer size is not `width * height * 4`.
|
|
332
|
+
* @param {Uint8Array} rgba
|
|
333
|
+
* @param {number} width
|
|
334
|
+
* @param {number} height
|
|
335
|
+
* @returns {Float32Array}
|
|
336
|
+
*/
|
|
337
|
+
preprocess_rgba(rgba, width, height) {
|
|
338
|
+
try {
|
|
339
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
340
|
+
const ptr0 = passArray8ToWasm0(rgba, wasm.__wbindgen_export);
|
|
341
|
+
const len0 = WASM_VECTOR_LEN;
|
|
342
|
+
wasm.yolopipeline_preprocess_rgba(retptr, this.__wbg_ptr, ptr0, len0, width, height);
|
|
343
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
344
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
345
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
346
|
+
if (r2) {
|
|
347
|
+
throw takeObject(r1);
|
|
348
|
+
}
|
|
349
|
+
return takeObject(r0);
|
|
350
|
+
} finally {
|
|
351
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The model's task (`"detect"`, `"segment"`, ...).
|
|
356
|
+
* @returns {string}
|
|
357
|
+
*/
|
|
358
|
+
get task() {
|
|
359
|
+
let deferred1_0;
|
|
360
|
+
let deferred1_1;
|
|
361
|
+
try {
|
|
362
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
363
|
+
wasm.yolopipeline_task(retptr, this.__wbg_ptr);
|
|
364
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
365
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
366
|
+
deferred1_0 = r0;
|
|
367
|
+
deferred1_1 = r1;
|
|
368
|
+
return getStringFromWasm0(r0, r1);
|
|
369
|
+
} finally {
|
|
370
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
371
|
+
wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (Symbol.dispose) YoloPipeline.prototype[Symbol.dispose] = YoloPipeline.prototype.free;
|
|
376
|
+
|
|
174
377
|
/**
|
|
175
378
|
* Return the Ultralytics pose skeleton + keypoint/limb colors (constant). The
|
|
176
379
|
* JS annotator calls this once to color pose overlays from the shared palette.
|
|
@@ -450,6 +653,10 @@ function __wbg_get_imports() {
|
|
|
450
653
|
const ret = getObject(arg0).length;
|
|
451
654
|
return ret;
|
|
452
655
|
},
|
|
656
|
+
__wbg_length_e6bdba0734e5c7f5: function(arg0) {
|
|
657
|
+
const ret = getObject(arg0).length;
|
|
658
|
+
return ret;
|
|
659
|
+
},
|
|
453
660
|
__wbg_location_40d7e9906760d235: function(arg0) {
|
|
454
661
|
const ret = getObject(arg0).location;
|
|
455
662
|
return (__wbindgen_enum_DataLocation.indexOf(ret) + 1 || 7) - 1;
|
|
@@ -478,6 +685,10 @@ function __wbg_get_imports() {
|
|
|
478
685
|
const ret = new ort.Tensor(__wbindgen_enum_DataType[arg0], takeObject(arg1), takeObject(arg2));
|
|
479
686
|
return addHeapObject(ret);
|
|
480
687
|
}, arguments); },
|
|
688
|
+
__wbg_new_from_slice_6696ab0c133d3f19: function(arg0, arg1) {
|
|
689
|
+
const ret = new Float32Array(getArrayF32FromWasm0(arg0, arg1));
|
|
690
|
+
return addHeapObject(ret);
|
|
691
|
+
},
|
|
481
692
|
__wbg_new_typed_00a409eb4ec4f2d9: function(arg0, arg1) {
|
|
482
693
|
try {
|
|
483
694
|
var state0 = {a: arg0, b: arg1};
|
|
@@ -485,7 +696,7 @@ function __wbg_get_imports() {
|
|
|
485
696
|
const a = state0.a;
|
|
486
697
|
state0.a = 0;
|
|
487
698
|
try {
|
|
488
|
-
return
|
|
699
|
+
return __wasm_bindgen_func_elem_2583(a, state0.b, arg0, arg1);
|
|
489
700
|
} finally {
|
|
490
701
|
state0.a = a;
|
|
491
702
|
}
|
|
@@ -574,6 +785,9 @@ function __wbg_get_imports() {
|
|
|
574
785
|
const ret = getObject(arg0).performance;
|
|
575
786
|
return isLikeNone(ret) ? 0 : addHeapObject(ret);
|
|
576
787
|
},
|
|
788
|
+
__wbg_prototypesetcall_46d9a0b93af9df2d: function(arg0, arg1, arg2) {
|
|
789
|
+
Float32Array.prototype.set.call(getArrayF32FromWasm0(arg0, arg1), getObject(arg2));
|
|
790
|
+
},
|
|
577
791
|
__wbg_prototypesetcall_d721637c7ca66eb8: function(arg0, arg1, arg2) {
|
|
578
792
|
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2));
|
|
579
793
|
},
|
|
@@ -672,18 +886,18 @@ function __wbg_get_imports() {
|
|
|
672
886
|
return addHeapObject(ret);
|
|
673
887
|
},
|
|
674
888
|
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
675
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
676
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
889
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 689, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
890
|
+
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_1968);
|
|
677
891
|
return addHeapObject(ret);
|
|
678
892
|
},
|
|
679
893
|
__wbindgen_cast_0000000000000002: function(arg0, arg1) {
|
|
680
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
681
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
894
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 916, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
895
|
+
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_2573);
|
|
682
896
|
return addHeapObject(ret);
|
|
683
897
|
},
|
|
684
898
|
__wbindgen_cast_0000000000000003: function(arg0, arg1) {
|
|
685
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("boolean")], shim_idx:
|
|
686
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
899
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("boolean")], shim_idx: 689, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
900
|
+
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_1968_2);
|
|
687
901
|
return addHeapObject(ret);
|
|
688
902
|
},
|
|
689
903
|
__wbindgen_cast_0000000000000004: function(arg0) {
|
|
@@ -770,10 +984,10 @@ function __wbg_get_imports() {
|
|
|
770
984
|
};
|
|
771
985
|
}
|
|
772
986
|
|
|
773
|
-
function
|
|
987
|
+
function __wasm_bindgen_func_elem_1968(arg0, arg1, arg2) {
|
|
774
988
|
try {
|
|
775
989
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
776
|
-
wasm.
|
|
990
|
+
wasm.__wasm_bindgen_func_elem_1968(retptr, arg0, arg1, addHeapObject(arg2));
|
|
777
991
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
778
992
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
779
993
|
if (r1) {
|
|
@@ -784,10 +998,10 @@ function __wasm_bindgen_func_elem_1879(arg0, arg1, arg2) {
|
|
|
784
998
|
}
|
|
785
999
|
}
|
|
786
1000
|
|
|
787
|
-
function
|
|
1001
|
+
function __wasm_bindgen_func_elem_2573(arg0, arg1, arg2) {
|
|
788
1002
|
try {
|
|
789
1003
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
790
|
-
wasm.
|
|
1004
|
+
wasm.__wasm_bindgen_func_elem_2573(retptr, arg0, arg1, addHeapObject(arg2));
|
|
791
1005
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
792
1006
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
793
1007
|
if (r1) {
|
|
@@ -798,10 +1012,10 @@ function __wasm_bindgen_func_elem_2269(arg0, arg1, arg2) {
|
|
|
798
1012
|
}
|
|
799
1013
|
}
|
|
800
1014
|
|
|
801
|
-
function
|
|
1015
|
+
function __wasm_bindgen_func_elem_1968_2(arg0, arg1, arg2) {
|
|
802
1016
|
try {
|
|
803
1017
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
804
|
-
wasm.
|
|
1018
|
+
wasm.__wasm_bindgen_func_elem_1968_2(retptr, arg0, arg1, addHeapObject(arg2));
|
|
805
1019
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
806
1020
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
807
1021
|
if (r1) {
|
|
@@ -812,8 +1026,8 @@ function __wasm_bindgen_func_elem_1879_2(arg0, arg1, arg2) {
|
|
|
812
1026
|
}
|
|
813
1027
|
}
|
|
814
1028
|
|
|
815
|
-
function
|
|
816
|
-
wasm.
|
|
1029
|
+
function __wasm_bindgen_func_elem_2583(arg0, arg1, arg2, arg3) {
|
|
1030
|
+
wasm.__wasm_bindgen_func_elem_2583(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
|
|
817
1031
|
}
|
|
818
1032
|
|
|
819
1033
|
|
|
@@ -824,6 +1038,9 @@ const __wbindgen_enum_DataType = ["bool", "float16", "float32", "float64", "int4
|
|
|
824
1038
|
const YoloModelFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
825
1039
|
? { register: () => {}, unregister: () => {} }
|
|
826
1040
|
: new FinalizationRegistry(ptr => wasm.__wbg_yolomodel_free(ptr, 1));
|
|
1041
|
+
const YoloPipelineFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
1042
|
+
? { register: () => {}, unregister: () => {} }
|
|
1043
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_yolopipeline_free(ptr, 1));
|
|
827
1044
|
|
|
828
1045
|
function addHeapObject(obj) {
|
|
829
1046
|
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
Binary file
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
export const memory: WebAssembly.Memory;
|
|
4
4
|
export const __wbg_yolomodel_free: (a: number, b: number) => void;
|
|
5
|
+
export const __wbg_yolopipeline_free: (a: number, b: number) => void;
|
|
5
6
|
export const pose_palette: (a: number) => void;
|
|
6
7
|
export const yolomodel_device: (a: number, b: number) => void;
|
|
7
8
|
export const yolomodel_load_bytes: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
|
|
@@ -9,11 +10,18 @@ export const yolomodel_names: (a: number, b: number) => void;
|
|
|
9
10
|
export const yolomodel_predict: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => number;
|
|
10
11
|
export const yolomodel_predict_rgba: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => number;
|
|
11
12
|
export const yolomodel_task: (a: number, b: number) => void;
|
|
13
|
+
export const yolopipeline_end2end: (a: number) => number;
|
|
14
|
+
export const yolopipeline_inputShape: (a: number, b: number) => void;
|
|
15
|
+
export const yolopipeline_names: (a: number, b: number) => void;
|
|
16
|
+
export const yolopipeline_new: (a: number, b: number, c: number) => void;
|
|
17
|
+
export const yolopipeline_postprocess: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => void;
|
|
18
|
+
export const yolopipeline_preprocess_rgba: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
19
|
+
export const yolopipeline_task: (a: number, b: number) => void;
|
|
12
20
|
export const start: () => void;
|
|
13
|
-
export const
|
|
14
|
-
export const
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
21
|
+
export const __wasm_bindgen_func_elem_1968: (a: number, b: number, c: number, d: number) => void;
|
|
22
|
+
export const __wasm_bindgen_func_elem_2573: (a: number, b: number, c: number, d: number) => void;
|
|
23
|
+
export const __wasm_bindgen_func_elem_1968_2: (a: number, b: number, c: number, d: number) => void;
|
|
24
|
+
export const __wasm_bindgen_func_elem_2583: (a: number, b: number, c: number, d: number) => void;
|
|
17
25
|
export const __wbindgen_export: (a: number, b: number) => number;
|
|
18
26
|
export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
19
27
|
export const __wbindgen_export3: (a: number) => void;
|