@ultralytics/yolo 0.0.20

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/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ultralytics/yolo",
3
+ "version": "0.0.20",
4
+ "description": "Run Ultralytics YOLO models in the browser on WebGPU (WebAssembly, ONNX Runtime Web via ort-web). Python-like API.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./pkg/*": "./pkg/*"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "pkg",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "sideEffects": [
26
+ "./pkg/*"
27
+ ],
28
+ "scripts": {
29
+ "build:wasm": "wasm-pack build ../crates/web --release --target web --out-dir \"$PWD/pkg\" --out-name ultralytics_inference_web && rm -f \"$PWD/pkg/.gitignore\"",
30
+ "build:ts": "tsc",
31
+ "build": "npm run build:wasm && npm run build:ts",
32
+ "prepack": "cp ../LICENSE ./LICENSE",
33
+ "clean": "rm -rf dist pkg LICENSE"
34
+ },
35
+ "keywords": [
36
+ "yolo",
37
+ "ultralytics",
38
+ "inference",
39
+ "webgpu",
40
+ "wasm",
41
+ "onnx",
42
+ "object-detection",
43
+ "computer-vision"
44
+ ],
45
+ "license": "AGPL-3.0",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/ultralytics/inference"
49
+ },
50
+ "homepage": "https://www.ultralytics.com",
51
+ "devDependencies": {
52
+ "typescript": "^5.4.0"
53
+ }
54
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "ultralytics-inference-web",
3
+ "type": "module",
4
+ "description": "Browser/WebAssembly bindings for ultralytics-inference (WebGPU via ort-web)",
5
+ "version": "0.0.20",
6
+ "license": "AGPL-3.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ultralytics/inference"
10
+ },
11
+ "files": [
12
+ "ultralytics_inference_web_bg.wasm",
13
+ "ultralytics_inference_web.js",
14
+ "ultralytics_inference_web.d.ts"
15
+ ],
16
+ "main": "ultralytics_inference_web.js",
17
+ "types": "ultralytics_inference_web.d.ts",
18
+ "sideEffects": [
19
+ "./snippets/*"
20
+ ]
21
+ }
@@ -0,0 +1,152 @@
1
+ const INIT_SYMBOL = Symbol('@ort-web.init');
2
+
3
+ const FEATURES_NONE = 0;
4
+ const FEATURES_WEBGL = 1 << 0;
5
+ const FEATURES_WEBGPU = 1 << 1;
6
+ const FEATURES_ALL = FEATURES_WEBGL | FEATURES_WEBGPU;
7
+
8
+ /**
9
+ * @typedef {Object} Dist
10
+ * @property {string} baseUrl
11
+ * @property {string} scriptName
12
+ * @property {string | null} [binaryName]
13
+ * @property {string | null} [wrapperName] defaults to `binaryName` s/\.wasm$/.mjs
14
+ * @property {Record<'main' | 'wrapper' | 'binary', string> | null} integrities
15
+ */
16
+
17
+ const DEFAULT_DIST_BASE = 'https://cdn.pyke.io/0/pyke:ort-rs/web@1.24.3/';
18
+
19
+ /** @type {Record<number, Dist>} */
20
+ const DEFAULT_DIST = {
21
+ [FEATURES_NONE]: {
22
+ baseUrl: DEFAULT_DIST_BASE,
23
+ scriptName: 'ort.wasm.min.js',
24
+ binaryName: 'ort-wasm-simd-threaded.wasm',
25
+ integrities: {
26
+ main: '1SBQgvQsxJRGAOAJ6K2nPaLO1SKelZwoF+biXgv2/D9fPspYLhvG4WIMDb/BUoJC',
27
+ wrapper: '/xM/eq8aUBJZgBuVwTQcLA5KlNmP6HOaENdJVgCkA/06cOMdL9EIQtmMuXOlMZEd',
28
+ binary: 'sZw0EVBgUn+dNhQfjHDg8lwtmicKMm1bTvWS4rIRNxoVN1S9HkVyJ2nreMpYruEZ'
29
+ }
30
+ },
31
+ [FEATURES_WEBGL]: {
32
+ baseUrl: DEFAULT_DIST_BASE,
33
+ scriptName: 'ort.webgl.min.js',
34
+ binaryName: 'ort-wasm-simd-threaded.wasm',
35
+ integrities: {
36
+ main: 'tBplgcsp8KUmgpc7glC4gbb5MdN/LBa1x90w++Y5WHDLaLo0m75wK+OtxIVa0KB6',
37
+ wrapper: '/xM/eq8aUBJZgBuVwTQcLA5KlNmP6HOaENdJVgCkA/06cOMdL9EIQtmMuXOlMZEd',
38
+ binary: 'sZw0EVBgUn+dNhQfjHDg8lwtmicKMm1bTvWS4rIRNxoVN1S9HkVyJ2nreMpYruEZ'
39
+ }
40
+ },
41
+ [FEATURES_WEBGPU]: {
42
+ baseUrl: DEFAULT_DIST_BASE,
43
+ scriptName: 'ort.webgpu.min.js',
44
+ binaryName: 'ort-wasm-simd-threaded.jsep.wasm',
45
+ integrities: {
46
+ main: 'ryEl5/BLEvDIrNfBZGTpwZzs0EFe+rDt9wM/Xs5DbM7mwJm2V6/BPE7AGVtwKKiL',
47
+ wrapper: 'C9DMcnZCIFFbpwJbX9QrGnrhpRt+2yD/FCcEZRrYf0iuzpOlCibPY0zsN8Wh1O+U',
48
+ binary: 'SLG1FQY8ZmHhts4OFaia4WTuj6Ttjqb3U7uJQXb8L19O+HB0DG8zJh+vdmXdxD53'
49
+ }
50
+ },
51
+ [FEATURES_ALL]: {
52
+ baseUrl: DEFAULT_DIST_BASE,
53
+ scriptName: 'ort.all.min.js',
54
+ binaryName: 'ort-wasm-simd-threaded.jsep.wasm',
55
+ integrities: {
56
+ main: 'aSKRQamw4Hf1ggcSUR2ZFriC21oCir1YibdErfC+xmh/w1ijdn/GP4Vr4rMu1dvs',
57
+ wrapper: 'C9DMcnZCIFFbpwJbX9QrGnrhpRt+2yD/FCcEZRrYf0iuzpOlCibPY0zsN8Wh1O+U',
58
+ binary: 'SLG1FQY8ZmHhts4OFaia4WTuj6Ttjqb3U7uJQXb8L19O+HB0DG8zJh+vdmXdxD53'
59
+ }
60
+ }
61
+ };
62
+
63
+ /**
64
+ * @param {string} url
65
+ * @param {'fetch' | 'script' | 'module'} as
66
+ * @param {string} [type]
67
+ * @param {string | null} [integrity]
68
+ */
69
+ function preload(url, as, type, integrity) {
70
+ const el = document.createElement('link');
71
+ el.href = url;
72
+ if (as !== 'module') {
73
+ el.rel = 'preload';
74
+ el.setAttribute('as', as);
75
+ } else {
76
+ el.rel = 'modulepreload';
77
+ }
78
+ if (type) {
79
+ el.setAttribute('type', type);
80
+ }
81
+ if (integrity) {
82
+ el.setAttribute('integrity', `sha384-${integrity}`);
83
+ }
84
+ el.setAttribute('crossorigin', 'anonymous');
85
+ document.head.appendChild(el);
86
+ }
87
+
88
+ /**
89
+ * @param {number} features
90
+ * @param {Dist} [dist]
91
+ * @returns {Promise<boolean>}
92
+ */
93
+ export function initRuntime(features, dist) {
94
+ if ('ort' in window && /** @type {any} */(window).ort[INIT_SYMBOL]) {
95
+ return Promise.resolve(false);
96
+ }
97
+
98
+ if (!dist) {
99
+ if (!(features in DEFAULT_DIST)) {
100
+ return Promise.reject(new Error('Unsupported feature set'));
101
+ }
102
+
103
+ dist = DEFAULT_DIST[features];
104
+ }
105
+
106
+ /** @param {string} file */
107
+ const relative = file => new URL(file, dist.baseUrl).toString();
108
+
109
+ return new Promise((resolve, reject) => {
110
+ // since the order is load main script -> imports wrapper script -> fetches wasm, now would be a good time to
111
+ // start fetching those
112
+ if (dist.binaryName) {
113
+ preload(
114
+ relative(dist.binaryName),
115
+ 'fetch',
116
+ 'application/wasm',
117
+ dist.integrities && dist.integrities.binary
118
+ );
119
+ preload(
120
+ relative(dist.wrapperName || dist.binaryName.replace(/\.wasm$/, '.mjs')),
121
+ 'module',
122
+ undefined,
123
+ dist.integrities && dist.integrities.wrapper
124
+ );
125
+ }
126
+
127
+ const script = document.createElement('script');
128
+ script.src = new URL(dist.scriptName, dist.baseUrl).toString();
129
+ if (dist.integrities && dist.integrities.main) {
130
+ script.setAttribute('integrity', `sha384-${dist.integrities && dist.integrities.main}`);
131
+ }
132
+ script.setAttribute('crossorigin', 'anonymous');
133
+ script.addEventListener('load', () => {
134
+ if (!('ort' in window)) {
135
+ return reject(new Error('script loaded but ort not defined'));
136
+ }
137
+
138
+ Object.defineProperty(window.ort, INIT_SYMBOL, {
139
+ value: true,
140
+ configurable: false,
141
+ enumerable: false,
142
+ writable: false
143
+ });
144
+
145
+ resolve(true);
146
+ });
147
+ script.addEventListener('error', e => {
148
+ reject(e.error);
149
+ });
150
+ document.head.appendChild(script);
151
+ });
152
+ }
@@ -0,0 +1,52 @@
1
+ const EVENT_URL = 'https://signal.pyke.io/beacon/9f5be487-d137-455a-9938-2fc7ecaa9de3/vVOv73JqP3iYRqXMBNm';
2
+
3
+ const IS_LOCALHOST = /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/;
4
+
5
+ /** @param {Uint8Array<ArrayBuffer>} payload */
6
+ function track(payload) {
7
+ if (IS_LOCALHOST.test(location.hostname) || location.protocol === 'file:') {
8
+ return false;
9
+ }
10
+ if (navigator.webdriver || 'Cypress' in window) {
11
+ return false;
12
+ }
13
+
14
+ return navigator.sendBeacon(EVENT_URL, payload.buffer);
15
+ }
16
+
17
+ /** @param {Uint8Array<ArrayBuffer>[]} chunks */
18
+ function concat(...chunks) {
19
+ const concatenated = new Uint8Array(chunks.reduce((a, b) => a + b.byteLength, 0));
20
+ let offset = 0;
21
+ for (const chunk of chunks) {
22
+ concatenated.set(chunk, offset);
23
+ offset += chunk.byteLength;
24
+ }
25
+ return concatenated;
26
+ }
27
+
28
+ /** @param {number} x */
29
+ function asUint32(x) {
30
+ const view = new DataView(new ArrayBuffer(4));
31
+ view.setUint32(0, x, true);
32
+ return new Uint8Array(view.buffer);
33
+ }
34
+
35
+ const encoder = new TextEncoder();
36
+
37
+ let hasInitializedSession = false;
38
+ export function trackSessionInit() {
39
+ if (hasInitializedSession) {
40
+ return true;
41
+ }
42
+
43
+ hasInitializedSession = true;
44
+
45
+ const encodedHostname = encoder.encode(location.hostname);
46
+ return track(concat(
47
+ new Uint8Array([ 0x01 ]),
48
+ new Uint8Array([ 0x90, 0x63, 0x8A, 0xE7 ]),
49
+ asUint32(encodedHostname.byteLength),
50
+ encodedHostname
51
+ ));
52
+ }
@@ -0,0 +1,133 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A loaded YOLO model ready for inference in the browser.
6
+ *
7
+ * Created via [`YoloModel::load_bytes`]; run with [`YoloModel::predict`]. The
8
+ * TypeScript wrapper exposes this as `YOLO`.
9
+ */
10
+ export class YoloModel {
11
+ private constructor();
12
+ free(): void;
13
+ [Symbol.dispose](): void;
14
+ /**
15
+ * Load a model from raw ONNX bytes (fetched by the JS wrapper).
16
+ *
17
+ * Initializes the accelerated backend on first use, reads the embedded
18
+ * Ultralytics metadata from the model bytes, then commits an ONNX Runtime
19
+ * session on the requested execution provider. Equivalent to Python's
20
+ * `YOLO('model.onnx')`.
21
+ *
22
+ * `device` is `"webgpu"` or `"cpu"` (the browser picks the GPU adapter
23
+ * automatically). WebGPU is registered with `error_on_failure`, so if the
24
+ * session cannot commit on it the load falls back to CPU and
25
+ * [`device`](Self::device) reports what actually ran.
26
+ *
27
+ * # Errors
28
+ * Returns a JS error if the backend cannot start, the bytes are not a valid
29
+ * model, or the model lacks Ultralytics metadata.
30
+ */
31
+ static load_bytes(bytes: Uint8Array, ort_base_url: string | null | undefined, device: string): Promise<YoloModel>;
32
+ /**
33
+ * Run inference on a single encoded image (JPEG or PNG bytes).
34
+ *
35
+ * `conf` and `iou` are the confidence and NMS IoU thresholds (pass the model
36
+ * defaults 0.25 / 0.7 to match Ultralytics); `classes` optionally keeps only
37
+ * the given class ids (for semantic, other pixels become background).
38
+ * Returns a plain JS object whose shape mirrors the Ultralytics `Results` API.
39
+ *
40
+ * # Errors
41
+ * Returns a JS error if the image cannot be decoded or inference fails.
42
+ */
43
+ predict(image: Uint8Array, conf: number, iou: number, classes?: Uint32Array | null): Promise<any>;
44
+ /**
45
+ * Run inference on raw `RGBA` pixels (e.g. a canvas/webcam `ImageData`).
46
+ *
47
+ * Skips image encoding/decoding entirely, so it is the fast path for live
48
+ * video. `rgba` is `width * height * 4` bytes, row-major.
49
+ *
50
+ * # Errors
51
+ * Returns a JS error if the buffer size is wrong or inference fails.
52
+ */
53
+ predict_rgba(rgba: Uint8Array, width: number, height: number, conf: number, iou: number, classes?: Uint32Array | null): Promise<any>;
54
+ /**
55
+ * The active device: `"webgpu"` or `"cpu"` (the fallback when WebGPU is
56
+ * unavailable). Mirrors the native `Device` display.
57
+ */
58
+ readonly device: string;
59
+ /**
60
+ * Class id -> name map (like Ultralytics `model.names`), as a JS object.
61
+ *
62
+ * # Errors
63
+ * Returns a JS error only if serialization fails (not expected).
64
+ */
65
+ readonly names: any;
66
+ /**
67
+ * The model's task (`"detect"`, `"segment"`, `"pose"`, `"classify"`,
68
+ * `"obb"`, or `"semantic"`).
69
+ */
70
+ readonly task: string;
71
+ }
72
+
73
+ /**
74
+ * Return the Ultralytics pose skeleton + keypoint/limb colors (constant). The
75
+ * JS annotator calls this once to color pose overlays from the shared palette.
76
+ *
77
+ * # Errors
78
+ * Returns a JS error only if serialization fails (not expected).
79
+ */
80
+ export function pose_palette(): any;
81
+
82
+ /**
83
+ * Install a panic hook that logs Rust panics to the browser console.
84
+ */
85
+ export function start(): void;
86
+
87
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
88
+
89
+ export interface InitOutput {
90
+ readonly memory: WebAssembly.Memory;
91
+ readonly __wbg_yolomodel_free: (a: number, b: number) => void;
92
+ readonly pose_palette: (a: number) => void;
93
+ readonly yolomodel_device: (a: number, b: number) => void;
94
+ readonly yolomodel_load_bytes: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
95
+ readonly yolomodel_names: (a: number, b: number) => void;
96
+ readonly yolomodel_predict: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => number;
97
+ readonly yolomodel_predict_rgba: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => number;
98
+ readonly yolomodel_task: (a: number, b: number) => void;
99
+ readonly start: () => void;
100
+ readonly __wasm_bindgen_func_elem_1874: (a: number, b: number, c: number, d: number) => void;
101
+ readonly __wasm_bindgen_func_elem_2272: (a: number, b: number, c: number, d: number) => void;
102
+ readonly __wasm_bindgen_func_elem_1874_2: (a: number, b: number, c: number, d: number) => void;
103
+ readonly __wasm_bindgen_func_elem_2281: (a: number, b: number, c: number, d: number) => void;
104
+ readonly __wbindgen_export: (a: number, b: number) => number;
105
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
106
+ readonly __wbindgen_export3: (a: number) => void;
107
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
108
+ readonly __wbindgen_export5: (a: number, b: number) => void;
109
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
110
+ readonly __wbindgen_start: () => void;
111
+ }
112
+
113
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
114
+
115
+ /**
116
+ * Instantiates the given `module`, which can either be bytes or
117
+ * a precompiled `WebAssembly.Module`.
118
+ *
119
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
120
+ *
121
+ * @returns {InitOutput}
122
+ */
123
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
124
+
125
+ /**
126
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
127
+ * for everything else, calls `WebAssembly.instantiate` directly.
128
+ *
129
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
130
+ *
131
+ * @returns {Promise<InitOutput>}
132
+ */
133
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;