@ultralytics/yolo 0.0.21 → 0.0.22
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
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<a href="https://www.ultralytics.com/"><img src="https://raw.githubusercontent.com/ultralytics/assets/main/logo/Ultralytics_Logotype_Original.svg" width="320" alt="Ultralytics logo"></a>
|
|
4
4
|
|
|
5
|
-
# Ultralytics Inference
|
|
5
|
+
# Ultralytics YOLO npm Inference
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@ultralytics/yolo)
|
|
8
8
|
[](https://www.npmjs.com/package/@ultralytics/yolo)
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
[](https://community.ultralytics.com/)
|
|
14
14
|
[](https://reddit.com/r/ultralytics)
|
|
15
15
|
|
|
16
|
-
Run [Ultralytics](https://ultralytics.com) YOLO models directly in the browser,
|
|
16
|
+
Run [Ultralytics](https://www.ultralytics.com/) YOLO models directly in the browser,
|
|
17
17
|
with no server and no Python. It runs on **WebGPU** (with an automatic CPU/wasm
|
|
18
18
|
fallback) and covers detection, segmentation, pose, classification, OBB, and
|
|
19
19
|
semantic segmentation, behind a small TypeScript API with a built-in
|
|
@@ -34,7 +34,7 @@ via [`ort-web`](https://ort.pyke.io/backends/web), and all pre/postprocessing,
|
|
|
34
34
|
colors, and the pose skeleton come from that shared Rust code, so results and
|
|
35
35
|
visuals match the native and Python paths.
|
|
36
36
|
|
|
37
|
-
## Install
|
|
37
|
+
## 📦 Install
|
|
38
38
|
|
|
39
39
|
```bash
|
|
40
40
|
npm install @ultralytics/yolo
|
|
@@ -48,7 +48,7 @@ It ships as an ES module with TypeScript types and works in any modern bundler
|
|
|
48
48
|
(Vite, webpack, esbuild, Bun) or directly via a CDN such as
|
|
49
49
|
[esm.sh](https://esm.sh/@ultralytics/yolo).
|
|
50
50
|
|
|
51
|
-
## Quick
|
|
51
|
+
## 🚀 Quick Start
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
54
|
import { YOLO, annotate } from "@ultralytics/yolo";
|
|
@@ -74,7 +74,7 @@ const results = await model.predict(canvas, { conf: 0.25, iou: 0.7 });
|
|
|
74
74
|
console.log(model.device); // "webgpu" or "cpu"
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
### Webcam /
|
|
77
|
+
### Webcam / Video
|
|
78
78
|
|
|
79
79
|
Drawable sources (`<video>`, canvas, `ImageBitmap`, `ImageData`) take a
|
|
80
80
|
raw-pixel fast path with no re-encoding, so a render loop is smooth:
|
|
@@ -119,7 +119,7 @@ only). A value containing a `/` or a scheme is used as a URL/path as-is.
|
|
|
119
119
|
> proxy. The bare-name shortcut is convenient when you mirror the assets on such
|
|
120
120
|
> a host.
|
|
121
121
|
|
|
122
|
-
## Results
|
|
122
|
+
## 📐 Results Shape
|
|
123
123
|
|
|
124
124
|
`predict()` resolves to a `Results` object shaped like the Ultralytics
|
|
125
125
|
`Results`:
|
|
@@ -143,7 +143,7 @@ detection carries its Ultralytics palette `color`, and `annotate()` draws the
|
|
|
143
143
|
`masks` overlay and the pose skeleton with the same per-limb/keypoint colors as
|
|
144
144
|
the native renderer. None of this is duplicated in JS.
|
|
145
145
|
|
|
146
|
-
## Requirements &
|
|
146
|
+
## ⚙️ Requirements & Notes
|
|
147
147
|
|
|
148
148
|
- **WebGPU** (Chrome/Edge, or Firefox with WebGPU enabled) from a **secure
|
|
149
149
|
context** (`https://` or `http://localhost`) gives the fast path. Without
|
|
@@ -175,10 +175,10 @@ the native renderer. None of this is duplicated in JS.
|
|
|
175
175
|
creation. See the [ort-web docs](https://ort.pyke.io/backends/web) to review or
|
|
176
176
|
disable it.
|
|
177
177
|
|
|
178
|
-
## Building
|
|
178
|
+
## 🔨 Building From Source
|
|
179
179
|
|
|
180
180
|
This package builds the wasm from the Rust crate with
|
|
181
|
-
[`wasm-pack`](https://github.com/
|
|
181
|
+
[`wasm-pack`](https://github.com/wasm-bindgen/wasm-pack):
|
|
182
182
|
|
|
183
183
|
```bash
|
|
184
184
|
npm run build # wasm-pack build + tsc
|
|
@@ -194,7 +194,7 @@ suggesting features, or submitting code changes, your involvement is crucial.
|
|
|
194
194
|
|
|
195
195
|
- **Report Issues**: Found a bug? [Open an issue](https://github.com/ultralytics/inference/issues)
|
|
196
196
|
- **Feature Requests**: Have an idea? [Share it](https://github.com/ultralytics/inference/issues)
|
|
197
|
-
- **Pull Requests**: Read our [Contributing Guide](https://docs.ultralytics.com/help/contributing
|
|
197
|
+
- **Pull Requests**: Read our [Contributing Guide](https://docs.ultralytics.com/help/contributing) first
|
|
198
198
|
- **Feedback**: Take our [Survey](https://www.ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey)
|
|
199
199
|
|
|
200
200
|
A heartfelt thank you 🙏 goes out to all our contributors! Your efforts help make Ultralytics tools better for everyone.
|
package/dist/index.js
CHANGED
|
@@ -43,6 +43,10 @@ async function resolveDevice(pref) {
|
|
|
43
43
|
}
|
|
44
44
|
/** Ultralytics model assets release (ONNX exports of yolo26 / yolo11 / yolov8). */
|
|
45
45
|
const ASSETS = "https://github.com/ultralytics/assets/releases/download/v8.4.0/";
|
|
46
|
+
/** Ultralytics default thresholds (mirrored in the option JSDoc above). */
|
|
47
|
+
const DEFAULT_CONF = 0.25;
|
|
48
|
+
const DEFAULT_IOU = 0.7;
|
|
49
|
+
const DEFAULT_KEYPOINT_THRESHOLD = 0.25;
|
|
46
50
|
/**
|
|
47
51
|
* Resolve a model reference to a URL. A bare ONNX name (e.g. `"yolo26n.onnx"`,
|
|
48
52
|
* `"yolo11s-seg.onnx"`, `"yolov8n.onnx"`) auto-downloads from the Ultralytics
|
|
@@ -77,6 +81,13 @@ function isDrawable(input) {
|
|
|
77
81
|
input instanceof HTMLVideoElement ||
|
|
78
82
|
input instanceof ImageBitmap);
|
|
79
83
|
}
|
|
84
|
+
/** Fetch a URL and throw a clear error naming `what` if the response is not OK. */
|
|
85
|
+
async function fetchOk(url, what) {
|
|
86
|
+
const resp = await fetch(url);
|
|
87
|
+
if (!resp.ok)
|
|
88
|
+
throw new Error(`failed to fetch ${what}: ${url} (${resp.status})`);
|
|
89
|
+
return resp;
|
|
90
|
+
}
|
|
80
91
|
/** Fetch/normalize an encoded image input to bytes the wasm side can decode. */
|
|
81
92
|
async function toEncodedBytes(input) {
|
|
82
93
|
if (input instanceof Uint8Array)
|
|
@@ -85,9 +96,7 @@ async function toEncodedBytes(input) {
|
|
|
85
96
|
return new Uint8Array(input);
|
|
86
97
|
if (input instanceof Blob)
|
|
87
98
|
return new Uint8Array(await input.arrayBuffer());
|
|
88
|
-
const resp = await
|
|
89
|
-
if (!resp.ok)
|
|
90
|
-
throw new Error(`failed to fetch image: ${input} (${resp.status})`);
|
|
99
|
+
const resp = await fetchOk(input, "image");
|
|
91
100
|
return new Uint8Array(await resp.arrayBuffer());
|
|
92
101
|
}
|
|
93
102
|
/** Read a drawable source as raw RGBA pixels (the fast path, no re-encoding). */
|
|
@@ -146,9 +155,7 @@ export class YOLO {
|
|
|
146
155
|
let bytes;
|
|
147
156
|
if (typeof source === "string" || source instanceof URL) {
|
|
148
157
|
const url = resolveModel(source.toString());
|
|
149
|
-
const resp = await
|
|
150
|
-
if (!resp.ok)
|
|
151
|
-
throw new Error(`failed to fetch model: ${url} (${resp.status})`);
|
|
158
|
+
const resp = await fetchOk(url, "model");
|
|
152
159
|
bytes = new Uint8Array(await resp.arrayBuffer());
|
|
153
160
|
}
|
|
154
161
|
else {
|
|
@@ -182,8 +189,8 @@ export class YOLO {
|
|
|
182
189
|
* @param options Confidence/IoU thresholds.
|
|
183
190
|
*/
|
|
184
191
|
async predict(image, options) {
|
|
185
|
-
const conf = options?.conf ??
|
|
186
|
-
const iou = options?.iou ??
|
|
192
|
+
const conf = options?.conf ?? DEFAULT_CONF;
|
|
193
|
+
const iou = options?.iou ?? DEFAULT_IOU;
|
|
187
194
|
const classes = options?.classes ? new Uint32Array(options.classes) : undefined;
|
|
188
195
|
if (isDrawable(image)) {
|
|
189
196
|
const { data, width, height } = toImageData(image);
|
|
@@ -244,7 +251,7 @@ export async function annotate(canvas, image, results, options) {
|
|
|
244
251
|
const font = options?.font ?? `${fontSize}px sans-serif`;
|
|
245
252
|
const showLabels = options?.labels ?? true;
|
|
246
253
|
const showKpts = options?.keypoints ?? true;
|
|
247
|
-
const kptThresh = options?.keypointThreshold ??
|
|
254
|
+
const kptThresh = options?.keypointThreshold ?? DEFAULT_KEYPOINT_THRESHOLD;
|
|
248
255
|
ctx.lineWidth = lineWidth;
|
|
249
256
|
ctx.font = font;
|
|
250
257
|
ctx.textBaseline = "top";
|
|
@@ -325,9 +332,7 @@ export async function annotate(canvas, image, results, options) {
|
|
|
325
332
|
/** Resolve any {@link ImageInput} to something `drawImage` accepts. */
|
|
326
333
|
async function resolveBitmap(input) {
|
|
327
334
|
if (typeof input === "string" || input instanceof URL) {
|
|
328
|
-
const resp = await
|
|
329
|
-
if (!resp.ok)
|
|
330
|
-
throw new Error(`failed to fetch image: ${input} (${resp.status})`);
|
|
335
|
+
const resp = await fetchOk(input, "image");
|
|
331
336
|
return createImageBitmap(await resp.blob());
|
|
332
337
|
}
|
|
333
338
|
if (input instanceof Blob)
|
package/package.json
CHANGED
package/pkg/package.json
CHANGED
|
@@ -16,8 +16,7 @@ export class YoloModel {
|
|
|
16
16
|
*
|
|
17
17
|
* Initializes the accelerated backend on first use, reads the embedded
|
|
18
18
|
* Ultralytics metadata from the model bytes, then commits an ONNX Runtime
|
|
19
|
-
* session on the requested execution provider.
|
|
20
|
-
* `YOLO('model.onnx')`.
|
|
19
|
+
* session on the requested execution provider.
|
|
21
20
|
*
|
|
22
21
|
* `device` is `"webgpu"` or `"cpu"` (the browser picks the GPU adapter
|
|
23
22
|
* automatically). WebGPU is registered with `error_on_failure`, so if the
|
|
@@ -52,8 +52,7 @@ export class YoloModel {
|
|
|
52
52
|
*
|
|
53
53
|
* Initializes the accelerated backend on first use, reads the embedded
|
|
54
54
|
* Ultralytics metadata from the model bytes, then commits an ONNX Runtime
|
|
55
|
-
* session on the requested execution provider.
|
|
56
|
-
* `YOLO('model.onnx')`.
|
|
55
|
+
* session on the requested execution provider.
|
|
57
56
|
*
|
|
58
57
|
* `device` is `"webgpu"` or `"cpu"` (the browser picks the GPU adapter
|
|
59
58
|
* automatically). WebGPU is registered with `error_on_failure`, so if the
|
|
@@ -673,17 +672,17 @@ function __wbg_get_imports() {
|
|
|
673
672
|
return addHeapObject(ret);
|
|
674
673
|
},
|
|
675
674
|
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
676
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
675
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 687, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
677
676
|
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_1874);
|
|
678
677
|
return addHeapObject(ret);
|
|
679
678
|
},
|
|
680
679
|
__wbindgen_cast_0000000000000002: function(arg0, arg1) {
|
|
681
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
680
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 796, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
682
681
|
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_2272);
|
|
683
682
|
return addHeapObject(ret);
|
|
684
683
|
},
|
|
685
684
|
__wbindgen_cast_0000000000000003: function(arg0, arg1) {
|
|
686
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("boolean")], shim_idx:
|
|
685
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("boolean")], shim_idx: 687, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
687
686
|
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_1874_2);
|
|
688
687
|
return addHeapObject(ret);
|
|
689
688
|
},
|
|
Binary file
|