@mauriciobenjamin700/ort-vision-sdk-web 0.2.1
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/CHANGELOG.md +128 -0
- package/LICENSE +21 -0
- package/README.md +90 -0
- package/dist/core/canvas.d.ts +21 -0
- package/dist/core/canvas.d.ts.map +1 -0
- package/dist/core/canvas.js +58 -0
- package/dist/core/canvas.js.map +1 -0
- package/dist/core/exceptions.d.ts +25 -0
- package/dist/core/exceptions.d.ts.map +1 -0
- package/dist/core/exceptions.js +28 -0
- package/dist/core/exceptions.js.map +1 -0
- package/dist/core/providers.d.ts +21 -0
- package/dist/core/providers.d.ts.map +1 -0
- package/dist/core/providers.js +29 -0
- package/dist/core/providers.js.map +1 -0
- package/dist/core/session.d.ts +47 -0
- package/dist/core/session.d.ts.map +1 -0
- package/dist/core/session.js +86 -0
- package/dist/core/session.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/io/image.d.ts +17 -0
- package/dist/io/image.d.ts.map +1 -0
- package/dist/io/image.js +104 -0
- package/dist/io/image.js.map +1 -0
- package/dist/labels.d.ts +38 -0
- package/dist/labels.d.ts.map +1 -0
- package/dist/labels.js +71 -0
- package/dist/labels.js.map +1 -0
- package/dist/postprocess/classification.d.ts +16 -0
- package/dist/postprocess/classification.d.ts.map +1 -0
- package/dist/postprocess/classification.js +46 -0
- package/dist/postprocess/classification.js.map +1 -0
- package/dist/postprocess/detection.d.ts +112 -0
- package/dist/postprocess/detection.d.ts.map +1 -0
- package/dist/postprocess/detection.js +283 -0
- package/dist/postprocess/detection.js.map +1 -0
- package/dist/postprocess/segmentation.d.ts +61 -0
- package/dist/postprocess/segmentation.d.ts.map +1 -0
- package/dist/postprocess/segmentation.js +184 -0
- package/dist/postprocess/segmentation.js.map +1 -0
- package/dist/preprocess/image.d.ts +71 -0
- package/dist/preprocess/image.d.ts.map +1 -0
- package/dist/preprocess/image.js +171 -0
- package/dist/preprocess/image.js.map +1 -0
- package/dist/results.d.ts +182 -0
- package/dist/results.d.ts.map +1 -0
- package/dist/results.js +321 -0
- package/dist/results.js.map +1 -0
- package/dist/tasks/base.d.ts +14 -0
- package/dist/tasks/base.d.ts.map +1 -0
- package/dist/tasks/base.js +17 -0
- package/dist/tasks/base.js.map +1 -0
- package/dist/tasks/classifier.d.ts +82 -0
- package/dist/tasks/classifier.d.ts.map +1 -0
- package/dist/tasks/classifier.js +135 -0
- package/dist/tasks/classifier.js.map +1 -0
- package/dist/tasks/detector.d.ts +102 -0
- package/dist/tasks/detector.d.ts.map +1 -0
- package/dist/tasks/detector.js +189 -0
- package/dist/tasks/detector.js.map +1 -0
- package/dist/tasks/segmenter.d.ts +105 -0
- package/dist/tasks/segmenter.d.ts.map +1 -0
- package/dist/tasks/segmenter.js +242 -0
- package/dist/tasks/segmenter.js.map +1 -0
- package/dist/types.d.ts +182 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +147 -0
- package/dist/types.js.map +1 -0
- package/package.json +71 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@mauriciobenjamin700/ort-vision-sdk-web` are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.2.1] - 2026-05-03
|
|
11
|
+
|
|
12
|
+
First **published** release on npm. The previous `0.2.0` tag never produced a
|
|
13
|
+
package because the CI smoke-install step still referenced the old
|
|
14
|
+
`@ort-vision-sdk/web` scope after the rename to
|
|
15
|
+
`@mauriciobenjamin700/ort-vision-sdk-web`. No public API changes.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- README rewritten with badges, GitHub repo links, npm install snippets and
|
|
20
|
+
imports updated to the new `@mauriciobenjamin700/ort-vision-sdk-web` scope.
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
24
|
+
- CI smoke-install step now captures the packed tarball name dynamically and
|
|
25
|
+
resolves the package name via `package.json`, so it survives future
|
|
26
|
+
package-name changes.
|
|
27
|
+
|
|
28
|
+
## [0.2.0] - 2026-05-02
|
|
29
|
+
|
|
30
|
+
This release brings the public API in line with the Ultralytics / PyTorch
|
|
31
|
+
idiom, mirroring the Python `ort-vision-sdk` 0.2.0 changes.
|
|
32
|
+
|
|
33
|
+
### Added
|
|
34
|
+
|
|
35
|
+
- **Per-image `Results` envelopes** (`DetectionResults`, `ClassificationResults`,
|
|
36
|
+
`SegmentationResults`) returned by every `predict()` call as a 1-element
|
|
37
|
+
array, mirroring `YOLO("img.jpg")`. Each envelope holds:
|
|
38
|
+
- `boxes` / `probs` / `masks` — bulk typed-array views with Ultralytics-style
|
|
39
|
+
accessors (`xyxy`, `xywh`, `xyxyn`, `xywhn`, `cls`, `conf`, `data`,
|
|
40
|
+
`top1`, `top1conf`, `top5`, `top5conf`).
|
|
41
|
+
- `detections` — array of per-instance objects (the previous return type).
|
|
42
|
+
- `names` — `Record<number, string>` matching `model.names`.
|
|
43
|
+
- `origImg`, `origShape`, `path`, `speed`.
|
|
44
|
+
- `[Symbol.iterator]` — `for (const d of results[0])` works directly.
|
|
45
|
+
- **Ultralytics-style aliases** on `BoundingBox`, `ClassProbability`,
|
|
46
|
+
`ClassificationResult`, `DetectionResult`, `SegmentationResult`:
|
|
47
|
+
`cls`, `conf`, `name`, `box`, `xyxy`, `xywh` (center coords),
|
|
48
|
+
plus `xyxyn(origShape)` / `xywhn(origShape)` methods on `BoundingBox`.
|
|
49
|
+
- **`call(image)` method** on every task class — delegates to `predict()`,
|
|
50
|
+
for parity with PyTorch `nn.Module.__call__`.
|
|
51
|
+
- **`names: Record<number, string>` getter** on every task class alongside
|
|
52
|
+
`labels: readonly string[]`.
|
|
53
|
+
- **`batchedNms(boxes, scores, idxs, iouThreshold)`** matching
|
|
54
|
+
`torchvision.ops.batched_nms`.
|
|
55
|
+
- **Generic YOLO decoder names**: `decodeYolo`, `decodeYoloAnchors`,
|
|
56
|
+
`decodeYoloSeg` — same code, accurate name (covers v8/v9/v10/v11/v12).
|
|
57
|
+
- **Preprocess helpers** mirroring torchvision/OpenCV:
|
|
58
|
+
- `toTensor(image)` → CHW `Float32Array / 255` (ToTensor parity).
|
|
59
|
+
- `fromCv2(bgr, w, h)` / `toCv2(image)` — channel-order bridges.
|
|
60
|
+
- **Explicit `head` option** on `Detector.create()` and `Segmenter.create()`
|
|
61
|
+
— caller declares which decoder family the model uses (`"yolo"` for
|
|
62
|
+
detect heads of v8/v9/v10/v11/v12/v26; `"yolo-seg"` for the matching seg
|
|
63
|
+
heads). The SDK does **not** auto-detect — passing an unsupported head
|
|
64
|
+
throws. New types `DetectorHead`, `SegmenterHead`.
|
|
65
|
+
- **`classes` filter** on `Detector.predict()`/`call()` and
|
|
66
|
+
`Segmenter.predict()`/`call()` predict-options — keeps only results
|
|
67
|
+
whose `classId` is in the supplied array, matching Ultralytics'
|
|
68
|
+
`model.predict(img, classes=[0, 16])`.
|
|
69
|
+
|
|
70
|
+
### Changed
|
|
71
|
+
|
|
72
|
+
- `Detector.predict(img)` now returns `Promise<DetectionResults[]>` (1
|
|
73
|
+
element) instead of `Promise<DetectionResult[]>`. Iterate the envelope
|
|
74
|
+
to recover the old shape: `for (const d of (await det.predict(img))[0]) ...`.
|
|
75
|
+
- `Classifier.predict(img)` now returns `Promise<ClassificationResults[]>`.
|
|
76
|
+
- `Segmenter.predict(img)` now returns `Promise<SegmentationResults[]>`.
|
|
77
|
+
- The detection decoder is funnelled through `batchedNms` instead of an
|
|
78
|
+
inline per-class loop.
|
|
79
|
+
|
|
80
|
+
### Deprecated
|
|
81
|
+
|
|
82
|
+
- `decodeYoloV8`, `decodeYoloV8Anchors`, `decodeYoloV8Seg` — log a one-time
|
|
83
|
+
`console.warn` and delegate to the generic `decodeYolo*` versions.
|
|
84
|
+
Will be removed in 0.3.0. The corresponding option types
|
|
85
|
+
(`DecodeYoloV8Options`, `DecodeYoloV8AnchorsOptions`,
|
|
86
|
+
`DecodeYoloV8SegOptions`) are now type aliases of the new names.
|
|
87
|
+
|
|
88
|
+
### Migration
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// Before (0.1.0)
|
|
92
|
+
const detections = await det.predict("street.jpg");
|
|
93
|
+
for (const d of detections) {
|
|
94
|
+
console.log(d.classId, d.className, d.confidence, d.bbox.asXyxy());
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// After (0.2.0)
|
|
98
|
+
const results = await det.predict("street.jpg"); // length 1
|
|
99
|
+
const r = results[0];
|
|
100
|
+
|
|
101
|
+
// Per-instance entries (legacy fields still present):
|
|
102
|
+
for (const d of r) {
|
|
103
|
+
console.log(d.classId, d.className, d.confidence, d.bbox.asXyxy());
|
|
104
|
+
}
|
|
105
|
+
// or with the new short aliases:
|
|
106
|
+
for (const d of r) {
|
|
107
|
+
console.log(d.cls, d.name, d.conf, d.box.xyxy);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Bulk typed-array access (matches Ultralytics):
|
|
111
|
+
console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## [0.1.0] - 2026-05-02
|
|
115
|
+
|
|
116
|
+
### Added
|
|
117
|
+
|
|
118
|
+
- Initial alpha release.
|
|
119
|
+
- `Classifier` and `Detector` task classes wrapping `onnxruntime-web` sessions.
|
|
120
|
+
- Browser-friendly image loading (`loadImage`) accepting URLs, `Blob`, `File`, `HTMLImageElement`, `HTMLCanvasElement`, `OffscreenCanvas`, `ImageBitmap`, `ImageData`, and `RGBImage`.
|
|
121
|
+
- Preprocessing helpers: `letterbox`, `resize`, `normalize`, `toCHW`, `toFloat32`, `toFloat32Tensor`.
|
|
122
|
+
- Postprocessing helpers: `softmax`, `topK`, `decodeYoloV8`, `nms`.
|
|
123
|
+
- Execution-provider resolution defaulting to `["webgpu", "wasm"]`.
|
|
124
|
+
- Public types mirroring the Python SDK: `BoundingBox`, `ClassProbability`, `ClassificationResult`, `DetectionResult`, `RGBImage`.
|
|
125
|
+
|
|
126
|
+
[Unreleased]: https://github.com/mauriciobenjamin700/ort-vision-sdk/compare/web-v0.2.0...HEAD
|
|
127
|
+
[0.2.0]: https://github.com/mauriciobenjamin700/ort-vision-sdk/releases/tag/web-v0.2.0
|
|
128
|
+
[0.1.0]: https://github.com/mauriciobenjamin700/ort-vision-sdk/releases/tag/web-v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mauricio Benjamin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @mauriciobenjamin700/ort-vision-sdk-web
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mauriciobenjamin700/ort-vision-sdk-web)
|
|
4
|
+
[](https://github.com/mauriciobenjamin700/ort-vision-sdk)
|
|
5
|
+
[](https://github.com/mauriciobenjamin700/ort-vision-sdk/blob/main/sdk-js-web/LICENSE)
|
|
6
|
+
|
|
7
|
+
High-level TypeScript SDK for browser computer vision inference on top of [ONNX Runtime Web](https://onnxruntime.ai/docs/get-started/with-javascript/web.html).
|
|
8
|
+
|
|
9
|
+
Mirrors the Python [`ort-vision-sdk`](https://pypi.org/project/ort-vision-sdk/) API: task-oriented classes (`Classifier`, `Detector`) that handle image loading, preprocessing, execution-provider selection and postprocessing. Output is the same typed shape as the Python version (`ClassificationResult`, `DetectionResult`, `BoundingBox`).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mauriciobenjamin700/ort-vision-sdk-web onnxruntime-web
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`onnxruntime-web` is a peer dependency — you bring your own version and ship the matching `.wasm` files yourself.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
### Image classification
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Classifier } from "@mauriciobenjamin700/ort-vision-sdk-web";
|
|
25
|
+
|
|
26
|
+
const clf = await Classifier.create("/models/resnet50.onnx", {
|
|
27
|
+
labels: ["tench", "goldfish", /* ... 1000 ImageNet labels */],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const result = await clf.predict("/images/dog.jpg", { topK: 5 });
|
|
31
|
+
|
|
32
|
+
console.log(result.className, result.confidence);
|
|
33
|
+
console.log(result.probabilities);
|
|
34
|
+
// result.image is an RGBImage (HWC RGB Uint8Array) — the original input.
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Object detection
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Detector } from "@mauriciobenjamin700/ort-vision-sdk-web";
|
|
41
|
+
|
|
42
|
+
// labels defaults to "coco" (80 classes)
|
|
43
|
+
const det = await Detector.create("/models/yolov8n.onnx");
|
|
44
|
+
|
|
45
|
+
const detections = await det.predict("/images/street.jpg", {
|
|
46
|
+
confThreshold: 0.4,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
for (const d of detections) {
|
|
50
|
+
console.log(d.className, d.confidence, d.bbox.asXyxy());
|
|
51
|
+
// d.croppedImage is an RGBImage of just that bounding box region.
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Accepted image inputs
|
|
56
|
+
|
|
57
|
+
`predict(image)` and `loadImage(image)` both accept:
|
|
58
|
+
|
|
59
|
+
- `string` — a URL fetched via `fetch()`.
|
|
60
|
+
- `Blob` / `File` — for `<input type="file">` uploads.
|
|
61
|
+
- `HTMLImageElement` — an existing `<img>` tag.
|
|
62
|
+
- `HTMLCanvasElement` / `OffscreenCanvas` — already-rendered canvas.
|
|
63
|
+
- `ImageBitmap` — from `createImageBitmap()`.
|
|
64
|
+
- `ImageData` — raw pixel buffer (RGBA from canvas `getImageData()`).
|
|
65
|
+
- `RGBImage` — the SDK's canonical HWC RGB Uint8Array wrapper.
|
|
66
|
+
|
|
67
|
+
## Execution providers
|
|
68
|
+
|
|
69
|
+
The default provider order is `["webgpu", "wasm"]` — ONNX Runtime tries WebGPU first and silently falls back to WebAssembly if WebGPU isn't available. You can override per task:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const clf = await Classifier.create(model, {
|
|
73
|
+
labels,
|
|
74
|
+
providers: ["wasm"], // force CPU
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
For WebGPU to actually engage you need a recent ORT-Web build, a Chromium-based browser with WebGPU enabled, and either secure context (`https://` or `localhost`) or the right COOP/COEP headers if you also want SharedArrayBuffer-based wasm threading.
|
|
79
|
+
|
|
80
|
+
## Status
|
|
81
|
+
|
|
82
|
+
This project is **alpha** — the public API is stable enough to build against, but minor versions may introduce breaking changes during the pre-1.0 phase. Pin the version range you build against.
|
|
83
|
+
|
|
84
|
+
- Source code & issues: <https://github.com/mauriciobenjamin700/ort-vision-sdk>
|
|
85
|
+
- Changelog: <https://github.com/mauriciobenjamin700/ort-vision-sdk/blob/main/sdk-js-web/CHANGELOG.md>
|
|
86
|
+
- Python counterpart: [`ort-vision-sdk`](https://pypi.org/project/ort-vision-sdk/)
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT — see [LICENSE](https://github.com/mauriciobenjamin700/ort-vision-sdk/blob/main/sdk-js-web/LICENSE).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas plumbing shared by `io/image.ts` and `preprocess/image.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Browsers and workers expose two canvas types (`HTMLCanvasElement` and
|
|
5
|
+
* `OffscreenCanvas`) with slightly different surfaces. These helpers pick the
|
|
6
|
+
* right one for the current environment, return a unified 2D context, and
|
|
7
|
+
* convert between RGBA `ImageData` and the SDK's canonical HWC RGB
|
|
8
|
+
* `RGBImage`.
|
|
9
|
+
*/
|
|
10
|
+
import { RGBImage } from "../types.js";
|
|
11
|
+
export type Canvas2D = HTMLCanvasElement | OffscreenCanvas;
|
|
12
|
+
export type Context2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
|
|
13
|
+
/** Allocate a `Canvas2D`, preferring `OffscreenCanvas` when available. */
|
|
14
|
+
export declare function createCanvas(width: number, height: number): Canvas2D;
|
|
15
|
+
/** Get a 2D context from a canvas, throwing a typed error if the browser refuses. */
|
|
16
|
+
export declare function get2DContext(canvas: Canvas2D): Context2D;
|
|
17
|
+
/** Convert RGBA `ImageData` into the canonical HWC RGB `RGBImage`. */
|
|
18
|
+
export declare function imageDataToRGB(imageData: ImageData): RGBImage;
|
|
19
|
+
/** Convert an `RGBImage` into RGBA `ImageData` (alpha forced to 255). */
|
|
20
|
+
export declare function rgbToImageData(image: RGBImage): ImageData;
|
|
21
|
+
//# sourceMappingURL=canvas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canvas.d.ts","sourceRoot":"","sources":["../../src/core/canvas.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,MAAM,MAAM,QAAQ,GAAG,iBAAiB,GAAG,eAAe,CAAC;AAC3D,MAAM,MAAM,SAAS,GACjB,wBAAwB,GACxB,iCAAiC,CAAC;AAEtC,0EAA0E;AAC1E,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAWpE;AAED,qFAAqF;AACrF,wBAAgB,YAAY,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAMxD;AAED,sEAAsE;AACtE,wBAAgB,cAAc,CAAC,SAAS,EAAE,SAAS,GAAG,QAAQ,CAgB7D;AAED,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CASzD"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas plumbing shared by `io/image.ts` and `preprocess/image.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Browsers and workers expose two canvas types (`HTMLCanvasElement` and
|
|
5
|
+
* `OffscreenCanvas`) with slightly different surfaces. These helpers pick the
|
|
6
|
+
* right one for the current environment, return a unified 2D context, and
|
|
7
|
+
* convert between RGBA `ImageData` and the SDK's canonical HWC RGB
|
|
8
|
+
* `RGBImage`.
|
|
9
|
+
*/
|
|
10
|
+
import { ImageLoadError } from "./exceptions.js";
|
|
11
|
+
import { RGBImage } from "../types.js";
|
|
12
|
+
/** Allocate a `Canvas2D`, preferring `OffscreenCanvas` when available. */
|
|
13
|
+
export function createCanvas(width, height) {
|
|
14
|
+
if (typeof OffscreenCanvas !== "undefined") {
|
|
15
|
+
return new OffscreenCanvas(width, height);
|
|
16
|
+
}
|
|
17
|
+
if (typeof document !== "undefined") {
|
|
18
|
+
const c = document.createElement("canvas");
|
|
19
|
+
c.width = width;
|
|
20
|
+
c.height = height;
|
|
21
|
+
return c;
|
|
22
|
+
}
|
|
23
|
+
throw new ImageLoadError("No canvas implementation available in this environment.");
|
|
24
|
+
}
|
|
25
|
+
/** Get a 2D context from a canvas, throwing a typed error if the browser refuses. */
|
|
26
|
+
export function get2DContext(canvas) {
|
|
27
|
+
const ctx = canvas.getContext("2d");
|
|
28
|
+
if (ctx === null) {
|
|
29
|
+
throw new ImageLoadError("Failed to obtain 2D rendering context.");
|
|
30
|
+
}
|
|
31
|
+
return ctx;
|
|
32
|
+
}
|
|
33
|
+
/** Convert RGBA `ImageData` into the canonical HWC RGB `RGBImage`. */
|
|
34
|
+
export function imageDataToRGB(imageData) {
|
|
35
|
+
const { data, width, height } = imageData;
|
|
36
|
+
if (data.length !== width * height * 4) {
|
|
37
|
+
throw new ImageLoadError(`Unexpected ImageData length ${data.length} for ${width}x${height} (expected ${width * height * 4}).`);
|
|
38
|
+
}
|
|
39
|
+
const rgb = new Uint8Array(width * height * 3);
|
|
40
|
+
for (let i = 0, j = 0; i < data.length; i += 4, j += 3) {
|
|
41
|
+
rgb[j] = data[i];
|
|
42
|
+
rgb[j + 1] = data[i + 1];
|
|
43
|
+
rgb[j + 2] = data[i + 2];
|
|
44
|
+
}
|
|
45
|
+
return new RGBImage(rgb, width, height);
|
|
46
|
+
}
|
|
47
|
+
/** Convert an `RGBImage` into RGBA `ImageData` (alpha forced to 255). */
|
|
48
|
+
export function rgbToImageData(image) {
|
|
49
|
+
const data = new Uint8ClampedArray(image.width * image.height * 4);
|
|
50
|
+
for (let i = 0, j = 0; i < image.data.length; i += 3, j += 4) {
|
|
51
|
+
data[j] = image.data[i];
|
|
52
|
+
data[j + 1] = image.data[i + 1];
|
|
53
|
+
data[j + 2] = image.data[i + 2];
|
|
54
|
+
data[j + 3] = 255;
|
|
55
|
+
}
|
|
56
|
+
return new ImageData(data, image.width, image.height);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=canvas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canvas.js","sourceRoot":"","sources":["../../src/core/canvas.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAOvC,0EAA0E;AAC1E,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,MAAc;IACxD,IAAI,OAAO,eAAe,KAAK,WAAW,EAAE,CAAC;QAC3C,OAAO,IAAI,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;QACpC,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAC3C,CAAC,CAAC,KAAK,GAAG,KAAK,CAAC;QAChB,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC;QAClB,OAAO,CAAC,CAAC;IACX,CAAC;IACD,MAAM,IAAI,cAAc,CAAC,yDAAyD,CAAC,CAAC;AACtF,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,YAAY,CAAC,MAAgB;IAC3C,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAqB,CAAC;IACxD,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;Q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E,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7D,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAW,CAAC;QAClC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAW,CAAC;QAC1C,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAW,CAAC;QAC1C,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC;IACpB,CAAC;IACD,OAAO,IAAI,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exceptions raised by the SDK.
|
|
3
|
+
*
|
|
4
|
+
* All exceptions inherit from {@link OrtVisionError}, so callers can catch
|
|
5
|
+
* the base class to handle any SDK-originated failure uniformly.
|
|
6
|
+
*/
|
|
7
|
+
export declare class OrtVisionError extends Error {
|
|
8
|
+
constructor(message: string, options?: ErrorOptions);
|
|
9
|
+
}
|
|
10
|
+
/** Raised when an ONNX model cannot be loaded into an inference session. */
|
|
11
|
+
export declare class ModelLoadError extends OrtVisionError {
|
|
12
|
+
}
|
|
13
|
+
/** Raised when ONNX Runtime fails while executing a model. */
|
|
14
|
+
export declare class InferenceError extends OrtVisionError {
|
|
15
|
+
}
|
|
16
|
+
/** Raised when a requested execution provider is not available. */
|
|
17
|
+
export declare class ProviderNotAvailableError extends OrtVisionError {
|
|
18
|
+
}
|
|
19
|
+
/** Raised when an input image cannot be decoded into the canonical format. */
|
|
20
|
+
export declare class ImageLoadError extends OrtVisionError {
|
|
21
|
+
}
|
|
22
|
+
/** Raised when class labels cannot be resolved from the supplied spec. */
|
|
23
|
+
export declare class LabelMapError extends OrtVisionError {
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=exceptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.d.ts","sourceRoot":"","sources":["../../src/core/exceptions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qBAAa,cAAe,SAAQ,KAAK;gBAC3B,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED,4EAA4E;AAC5E,qBAAa,cAAe,SAAQ,cAAc;CAAG;AAErD,8DAA8D;AAC9D,qBAAa,cAAe,SAAQ,cAAc;CAAG;AAErD,mEAAmE;AACnE,qBAAa,yBAA0B,SAAQ,cAAc;CAAG;AAEhE,8EAA8E;AAC9E,qBAAa,cAAe,SAAQ,cAAc;CAAG;AAErD,0EAA0E;AAC1E,qBAAa,aAAc,SAAQ,cAAc;CAAG"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exceptions raised by the SDK.
|
|
3
|
+
*
|
|
4
|
+
* All exceptions inherit from {@link OrtVisionError}, so callers can catch
|
|
5
|
+
* the base class to handle any SDK-originated failure uniformly.
|
|
6
|
+
*/
|
|
7
|
+
export class OrtVisionError extends Error {
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message, options);
|
|
10
|
+
this.name = new.target.name;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Raised when an ONNX model cannot be loaded into an inference session. */
|
|
14
|
+
export class ModelLoadError extends OrtVisionError {
|
|
15
|
+
}
|
|
16
|
+
/** Raised when ONNX Runtime fails while executing a model. */
|
|
17
|
+
export class InferenceError extends OrtVisionError {
|
|
18
|
+
}
|
|
19
|
+
/** Raised when a requested execution provider is not available. */
|
|
20
|
+
export class ProviderNotAvailableError extends OrtVisionError {
|
|
21
|
+
}
|
|
22
|
+
/** Raised when an input image cannot be decoded into the canonical format. */
|
|
23
|
+
export class ImageLoadError extends OrtVisionError {
|
|
24
|
+
}
|
|
25
|
+
/** Raised when class labels cannot be resolved from the supplied spec. */
|
|
26
|
+
export class LabelMapError extends OrtVisionError {
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=exceptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../../src/core/exceptions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,OAAO,cAAe,SAAQ,KAAK;IACvC,YAAY,OAAe,EAAE,OAAsB;QACjD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;IAC9B,CAAC;CACF;AAED,4EAA4E;AAC5E,MAAM,OAAO,cAAe,SAAQ,cAAc;CAAG;AAErD,8DAA8D;AAC9D,MAAM,OAAO,cAAe,SAAQ,cAAc;CAAG;AAErD,mEAAmE;AACnE,MAAM,OAAO,yBAA0B,SAAQ,cAAc;CAAG;AAEhE,8EAA8E;AAC9E,MAAM,OAAO,cAAe,SAAQ,cAAc;CAAG;AAErD,0EAA0E;AAC1E,MAAM,OAAO,aAAc,SAAQ,cAAc;CAAG"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execution-provider defaults for ONNX Runtime Web sessions.
|
|
3
|
+
*
|
|
4
|
+
* Unlike Node ORT, the browser ORT cannot enumerate "available providers" at
|
|
5
|
+
* runtime — `webgpu` either works or ORT silently falls back to the next
|
|
6
|
+
* provider in the list. We just expose a sensible default order.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Default execution provider preference order for browser ORT.
|
|
10
|
+
*
|
|
11
|
+
* `webgpu` is tried first when available; ORT-Web falls back to `wasm`
|
|
12
|
+
* automatically when WebGPU is not supported by the browser or device.
|
|
13
|
+
*/
|
|
14
|
+
export declare const DEFAULT_PROVIDERS: readonly string[];
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the execution providers to pass to `InferenceSession.create`.
|
|
17
|
+
*
|
|
18
|
+
* @param requested Explicit provider list in preference order; `undefined` returns the default.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveProviders(requested?: readonly string[]): string[];
|
|
21
|
+
//# sourceMappingURL=providers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../../src/core/providers.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,MAAM,EAAuB,CAAC;AAEvE;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAQxE"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execution-provider defaults for ONNX Runtime Web sessions.
|
|
3
|
+
*
|
|
4
|
+
* Unlike Node ORT, the browser ORT cannot enumerate "available providers" at
|
|
5
|
+
* runtime — `webgpu` either works or ORT silently falls back to the next
|
|
6
|
+
* provider in the list. We just expose a sensible default order.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Default execution provider preference order for browser ORT.
|
|
10
|
+
*
|
|
11
|
+
* `webgpu` is tried first when available; ORT-Web falls back to `wasm`
|
|
12
|
+
* automatically when WebGPU is not supported by the browser or device.
|
|
13
|
+
*/
|
|
14
|
+
export const DEFAULT_PROVIDERS = ["webgpu", "wasm"];
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the execution providers to pass to `InferenceSession.create`.
|
|
17
|
+
*
|
|
18
|
+
* @param requested Explicit provider list in preference order; `undefined` returns the default.
|
|
19
|
+
*/
|
|
20
|
+
export function resolveProviders(requested) {
|
|
21
|
+
if (requested === undefined) {
|
|
22
|
+
return [...DEFAULT_PROVIDERS];
|
|
23
|
+
}
|
|
24
|
+
if (requested.length === 0) {
|
|
25
|
+
return [...DEFAULT_PROVIDERS];
|
|
26
|
+
}
|
|
27
|
+
return [...requested];
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../../src/core/providers.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAsB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAEvE;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAA6B;IAC5D,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO,CAAC,GAAG,iBAAiB,CAAC,CAAC;IAChC,CAAC;IACD,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,CAAC,GAAG,iBAAiB,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.
|
|
3
|
+
*/
|
|
4
|
+
import type * as ort from "onnxruntime-web";
|
|
5
|
+
/** Anything `InferenceSession.create` accepts. */
|
|
6
|
+
export type ModelSource = string | ArrayBufferLike | Uint8Array;
|
|
7
|
+
export interface OrtSessionOptions {
|
|
8
|
+
/** Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}. */
|
|
9
|
+
readonly providers?: readonly string[];
|
|
10
|
+
/** Optional ORT session options forwarded to `InferenceSession.create`. */
|
|
11
|
+
readonly sessionOptions?: ort.InferenceSession.SessionOptions;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.
|
|
15
|
+
*
|
|
16
|
+
* The wrapper exposes input/output names, manages execution-provider
|
|
17
|
+
* selection, and provides a typed {@link OrtSession.run} method.
|
|
18
|
+
*/
|
|
19
|
+
export declare class OrtSession {
|
|
20
|
+
private readonly _session;
|
|
21
|
+
readonly providers: readonly string[];
|
|
22
|
+
private constructor();
|
|
23
|
+
/**
|
|
24
|
+
* Load an ONNX model into an ORT inference session.
|
|
25
|
+
*
|
|
26
|
+
* @param model Either a URL string fetched by ORT, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.
|
|
27
|
+
* @param options Provider list and pass-through `SessionOptions`.
|
|
28
|
+
* @throws {@link ModelLoadError} if the model cannot be loaded.
|
|
29
|
+
*/
|
|
30
|
+
static create(model: ModelSource, options?: OrtSessionOptions): Promise<OrtSession>;
|
|
31
|
+
/** Names of the model's inputs, in declaration order. */
|
|
32
|
+
get inputNames(): readonly string[];
|
|
33
|
+
/** Name of the first (and usually only) input. */
|
|
34
|
+
get inputName(): string;
|
|
35
|
+
/** Names of the model's outputs, in declaration order. */
|
|
36
|
+
get outputNames(): readonly string[];
|
|
37
|
+
/** The underlying `onnxruntime-web` session, for advanced use cases. */
|
|
38
|
+
get raw(): ort.InferenceSession;
|
|
39
|
+
/**
|
|
40
|
+
* Run inference and return all outputs.
|
|
41
|
+
*
|
|
42
|
+
* @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.
|
|
43
|
+
* @throws {@link InferenceError} if ORT raises any error during execution.
|
|
44
|
+
*/
|
|
45
|
+
run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>>;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/core/session.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,KAAK,GAAG,MAAM,iBAAiB,CAAC;AAM5C,kDAAkD;AAClD,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,eAAe,GAAG,UAAU,CAAC;AAEhE,MAAM,WAAW,iBAAiB;IAChC,2FAA2F;IAC3F,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,2EAA2E;IAC3E,QAAQ,CAAC,cAAc,CAAC,EAAE,GAAG,CAAC,gBAAgB,CAAC,cAAc,CAAC;CAC/D;AAED;;;;;GAKG;AACH,qBAAa,UAAU;IAEnB,OAAO,CAAC,QAAQ,CAAC,QAAQ;aACT,SAAS,EAAE,SAAS,MAAM,EAAE;IAF9C,OAAO;IAKP;;;;;;OAMG;WACU,MAAM,CACjB,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,UAAU,CAAC;IA6BtB,yDAAyD;IACzD,IAAI,UAAU,IAAI,SAAS,MAAM,EAAE,CAElC;IAED,kDAAkD;IAClD,IAAI,SAAS,IAAI,MAAM,CAMtB;IAED,0DAA0D;IAC1D,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE,CAEnC;IAED,wEAAwE;IACxE,IAAI,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAE9B;IAED;;;;;OAKG;IACG,GAAG,CACP,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAWvC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin wrapper around `onnxruntime-web` `InferenceSession` with typed metadata.
|
|
3
|
+
*/
|
|
4
|
+
import * as ortRuntime from "onnxruntime-web";
|
|
5
|
+
import { InferenceError, ModelLoadError } from "./exceptions.js";
|
|
6
|
+
import { resolveProviders } from "./providers.js";
|
|
7
|
+
/**
|
|
8
|
+
* Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.
|
|
9
|
+
*
|
|
10
|
+
* The wrapper exposes input/output names, manages execution-provider
|
|
11
|
+
* selection, and provides a typed {@link OrtSession.run} method.
|
|
12
|
+
*/
|
|
13
|
+
export class OrtSession {
|
|
14
|
+
_session;
|
|
15
|
+
providers;
|
|
16
|
+
constructor(_session, providers) {
|
|
17
|
+
this._session = _session;
|
|
18
|
+
this.providers = providers;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Load an ONNX model into an ORT inference session.
|
|
22
|
+
*
|
|
23
|
+
* @param model Either a URL string fetched by ORT, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.
|
|
24
|
+
* @param options Provider list and pass-through `SessionOptions`.
|
|
25
|
+
* @throws {@link ModelLoadError} if the model cannot be loaded.
|
|
26
|
+
*/
|
|
27
|
+
static async create(model, options = {}) {
|
|
28
|
+
const providers = resolveProviders(options.providers);
|
|
29
|
+
const sessionOptions = {
|
|
30
|
+
...(options.sessionOptions ?? {}),
|
|
31
|
+
executionProviders: providers,
|
|
32
|
+
};
|
|
33
|
+
let session;
|
|
34
|
+
try {
|
|
35
|
+
if (typeof model === "string") {
|
|
36
|
+
session = await ortRuntime.InferenceSession.create(model, sessionOptions);
|
|
37
|
+
}
|
|
38
|
+
else if (model instanceof Uint8Array) {
|
|
39
|
+
session = await ortRuntime.InferenceSession.create(model, sessionOptions);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
session = await ortRuntime.InferenceSession.create(model, sessionOptions);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
throw new ModelLoadError(`Failed to load ONNX model: ${err.message}`, { cause: err });
|
|
47
|
+
}
|
|
48
|
+
return new OrtSession(session, providers);
|
|
49
|
+
}
|
|
50
|
+
/** Names of the model's inputs, in declaration order. */
|
|
51
|
+
get inputNames() {
|
|
52
|
+
return this._session.inputNames;
|
|
53
|
+
}
|
|
54
|
+
/** Name of the first (and usually only) input. */
|
|
55
|
+
get inputName() {
|
|
56
|
+
const name = this._session.inputNames[0];
|
|
57
|
+
if (name === undefined) {
|
|
58
|
+
throw new InferenceError("Model has no inputs.");
|
|
59
|
+
}
|
|
60
|
+
return name;
|
|
61
|
+
}
|
|
62
|
+
/** Names of the model's outputs, in declaration order. */
|
|
63
|
+
get outputNames() {
|
|
64
|
+
return this._session.outputNames;
|
|
65
|
+
}
|
|
66
|
+
/** The underlying `onnxruntime-web` session, for advanced use cases. */
|
|
67
|
+
get raw() {
|
|
68
|
+
return this._session;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Run inference and return all outputs.
|
|
72
|
+
*
|
|
73
|
+
* @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.
|
|
74
|
+
* @throws {@link InferenceError} if ORT raises any error during execution.
|
|
75
|
+
*/
|
|
76
|
+
async run(feeds) {
|
|
77
|
+
try {
|
|
78
|
+
const result = await this._session.run(feeds);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
throw new InferenceError(`Inference failed: ${err.message}`, { cause: err });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/core/session.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAYlD;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IAEF;IACD;IAFlB,YACmB,QAA8B,EAC/B,SAA4B;QAD3B,aAAQ,GAAR,QAAQ,CAAsB;QAC/B,cAAS,GAAT,SAAS,CAAmB;IAC3C,CAAC;IAEJ;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,KAA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,IAAI,CAAC;IACd,CAAC;IAED,0DAA0D;IAC1D,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC;IACnC,CAAC;IAED,wEAAwE;IACxE,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,GAAG,CACP,KAAiC;QAEjC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAC9C,OAAO,MAAoC,CAAC;QAC9C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,cAAc,CACtB,qBAAsB,GAAa,CAAC,OAAO,EAAE,EAC7C,EAAE,KAAK,EAAE,GAAG,EAAE,CACf,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ort-vision-sdk/web — high-level TypeScript SDK for browser computer
|
|
3
|
+
* vision inference with ONNX Runtime Web.
|
|
4
|
+
*/
|
|
5
|
+
export { BoundingBox, Mask, RGBImage, type ClassProbability, type ClassificationResult, type DetectionResult, type SegmentationResult, } from "./types.js";
|
|
6
|
+
export { Boxes, ClassificationResults, DetectionResults, Masks, Probs, SegmentationResults, } from "./results.js";
|
|
7
|
+
export { COCO_CLASSES, type LabelSpec, type ResolveLabelsOptions, resolveLabels, } from "./labels.js";
|
|
8
|
+
export { ImageLoadError, InferenceError, LabelMapError, ModelLoadError, OrtVisionError, ProviderNotAvailableError, } from "./core/exceptions.js";
|
|
9
|
+
export { type ModelSource, type OrtSessionOptions, OrtSession, } from "./core/session.js";
|
|
10
|
+
export { DEFAULT_PROVIDERS, resolveProviders } from "./core/providers.js";
|
|
11
|
+
export { type ImageInput, loadImage } from "./io/image.js";
|
|
12
|
+
export { type LetterboxResult, fromCv2, letterbox, normalize, resize, toCHW, toCv2, toFloat32, toFloat32Tensor, toTensor, } from "./preprocess/image.js";
|
|
13
|
+
export { type TopKResult, softmax, topK, } from "./postprocess/classification.js";
|
|
14
|
+
export { type DecodeYoloAnchorsOptions, type DecodeYoloOptions, type DecodeYoloV8AnchorsOptions, type DecodeYoloV8Options, type DecodedAnchors, type DecodedDetection, batchedNms, decodeYolo, decodeYoloAnchors, decodeYoloV8, decodeYoloV8Anchors, nms, } from "./postprocess/detection.js";
|
|
15
|
+
export { type DecodeYoloSegOptions, type DecodeYoloV8SegOptions, type DecodedSegmentation, decodeYoloSeg, decodeYoloV8Seg, } from "./postprocess/segmentation.js";
|
|
16
|
+
export { VisionTask } from "./tasks/base.js";
|
|
17
|
+
export { type ClassifierOptions, type ClassifierPredictOptions, Classifier, } from "./tasks/classifier.js";
|
|
18
|
+
export { type DetectorHead, type DetectorOptions, type DetectorPredictOptions, Detector, } from "./tasks/detector.js";
|
|
19
|
+
export { type SegmenterHead, type SegmenterOptions, type SegmenterPredictOptions, Segmenter, } from "./tasks/segmenter.js";
|
|
20
|
+
export declare const VERSION: string;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,WAAW,EACX,IAAI,EACJ,QAAQ,EACR,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,kBAAkB,GACxB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,KAAK,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,EACL,KAAK,EACL,mBAAmB,GACpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,YAAY,EACZ,KAAK,SAAS,EACd,KAAK,oBAAoB,EACzB,aAAa,GACd,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,cAAc,EACd,cAAc,EACd,aAA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