tempest-react-sdk 0.69.0 → 0.71.0
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 +3 -3
- package/dist/telemetry/app-error-adapter.cjs +2 -0
- package/dist/telemetry/app-error-adapter.cjs.map +1 -0
- package/dist/telemetry/app-error-adapter.js +18 -0
- package/dist/telemetry/app-error-adapter.js.map +1 -0
- package/dist/telemetry/app-error-queue.cjs +2 -0
- package/dist/telemetry/app-error-queue.cjs.map +1 -0
- package/dist/telemetry/app-error-queue.js +42 -0
- package/dist/telemetry/app-error-queue.js.map +1 -0
- package/dist/telemetry/app-error-reporter.cjs +2 -0
- package/dist/telemetry/app-error-reporter.cjs.map +1 -0
- package/dist/telemetry/app-error-reporter.js +64 -0
- package/dist/telemetry/app-error-reporter.js.map +1 -0
- package/dist/telemetry/app-error-schema.cjs +2 -0
- package/dist/telemetry/app-error-schema.cjs.map +1 -0
- package/dist/telemetry/app-error-schema.js +28 -0
- package/dist/telemetry/app-error-schema.js.map +1 -0
- package/dist/telemetry/app-error-transport.cjs +2 -0
- package/dist/telemetry/app-error-transport.cjs.map +1 -0
- package/dist/telemetry/app-error-transport.js +44 -0
- package/dist/telemetry/app-error-transport.js.map +1 -0
- package/dist/telemetry/describe-app-error.cjs +3 -0
- package/dist/telemetry/describe-app-error.cjs.map +1 -0
- package/dist/telemetry/describe-app-error.js +54 -0
- package/dist/telemetry/describe-app-error.js.map +1 -0
- package/dist/tempest-react-sdk.cjs +1 -1
- package/dist/tempest-react-sdk.d.ts +214 -0
- package/dist/tempest-react-sdk.js +13 -10
- package/dist/vision/core/canvas.cjs +1 -1
- package/dist/vision/core/canvas.cjs.map +1 -1
- package/dist/vision/core/canvas.js +37 -13
- package/dist/vision/core/canvas.js.map +1 -1
- package/dist/vision/core/dtypes.cjs +2 -0
- package/dist/vision/core/dtypes.cjs.map +1 -0
- package/dist/vision/core/dtypes.js +33 -0
- package/dist/vision/core/dtypes.js.map +1 -0
- package/dist/vision/core/graph.cjs +1 -1
- package/dist/vision/core/graph.cjs.map +1 -1
- package/dist/vision/core/graph.js +10 -2
- package/dist/vision/core/graph.js.map +1 -1
- package/dist/vision/core/metadata.cjs +2 -2
- package/dist/vision/core/metadata.cjs.map +1 -1
- package/dist/vision/core/metadata.js +103 -13
- package/dist/vision/core/metadata.js.map +1 -1
- package/dist/vision/core/providers.cjs +1 -1
- package/dist/vision/core/providers.cjs.map +1 -1
- package/dist/vision/core/providers.js +9 -6
- package/dist/vision/core/providers.js.map +1 -1
- package/dist/vision/core/session.cjs +1 -1
- package/dist/vision/core/session.cjs.map +1 -1
- package/dist/vision/core/session.js +115 -27
- package/dist/vision/core/session.js.map +1 -1
- package/dist/vision/fusion.cjs +1 -1
- package/dist/vision/fusion.cjs.map +1 -1
- package/dist/vision/fusion.js +28 -23
- package/dist/vision/fusion.js.map +1 -1
- package/dist/vision/index.cjs +1 -1
- package/dist/vision/index.cjs.map +1 -1
- package/dist/vision/index.js +20 -19
- package/dist/vision/index.js.map +1 -1
- package/dist/vision/io/image.cjs +1 -1
- package/dist/vision/io/image.cjs.map +1 -1
- package/dist/vision/io/image.js +55 -20
- package/dist/vision/io/image.js.map +1 -1
- package/dist/vision/postprocess/detection.cjs +1 -1
- package/dist/vision/postprocess/detection.cjs.map +1 -1
- package/dist/vision/postprocess/detection.js +42 -51
- package/dist/vision/postprocess/detection.js.map +1 -1
- package/dist/vision/postprocess/segmentation.cjs +1 -1
- package/dist/vision/postprocess/segmentation.cjs.map +1 -1
- package/dist/vision/postprocess/segmentation.js +45 -31
- package/dist/vision/postprocess/segmentation.js.map +1 -1
- package/dist/vision/preprocess/pipeline.cjs +1 -1
- package/dist/vision/preprocess/pipeline.cjs.map +1 -1
- package/dist/vision/preprocess/pipeline.js +24 -25
- package/dist/vision/preprocess/pipeline.js.map +1 -1
- package/dist/vision/tasks/base.cjs +1 -1
- package/dist/vision/tasks/base.cjs.map +1 -1
- package/dist/vision/tasks/base.js +22 -7
- package/dist/vision/tasks/base.js.map +1 -1
- package/dist/vision/tasks/classifier.cjs +1 -1
- package/dist/vision/tasks/classifier.cjs.map +1 -1
- package/dist/vision/tasks/classifier.js +64 -55
- package/dist/vision/tasks/classifier.js.map +1 -1
- package/dist/vision/tasks/detectClassify.cjs +1 -1
- package/dist/vision/tasks/detectClassify.cjs.map +1 -1
- package/dist/vision/tasks/detectClassify.js +80 -69
- package/dist/vision/tasks/detectClassify.js.map +1 -1
- package/dist/vision/tasks/detector.cjs +1 -1
- package/dist/vision/tasks/detector.cjs.map +1 -1
- package/dist/vision/tasks/detector.js +74 -68
- package/dist/vision/tasks/detector.js.map +1 -1
- package/dist/vision/tasks/segmenter.cjs +1 -1
- package/dist/vision/tasks/segmenter.cjs.map +1 -1
- package/dist/vision/tasks/segmenter.js +85 -62
- package/dist/vision/tasks/segmenter.js.map +1 -1
- package/dist/vision/types.cjs +1 -1
- package/dist/vision/types.cjs.map +1 -1
- package/dist/vision/types.js +24 -4
- package/dist/vision/types.js.map +1 -1
- package/dist/vision.cjs +1 -1
- package/dist/vision.d.ts +1153 -777
- package/dist/vision.js +22 -21
- package/package.json +1 -1
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { SpeedTimer as e } from "../core/timing.js";
|
|
2
2
|
import { ClassificationResults as t, Probs as n } from "../results.js";
|
|
3
3
|
import { resolveLabels as r } from "../labels.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
4
|
+
import { asFloat32Array as i } from "../core/dtypes.js";
|
|
5
|
+
import { classificationNumClasses as a, resolveInputSize as o } from "../core/graph.js";
|
|
6
|
+
import { modelNames as s } from "../core/metadata.js";
|
|
7
|
+
import { OrtSession as c } from "../core/session.js";
|
|
8
|
+
import { isUltralyticsClassifier as l, resolveNormalization as u } from "../normalization.js";
|
|
9
|
+
import { loadImageSource as d } from "../io/image.js";
|
|
10
|
+
import { toFloat32Tensor as f } from "../preprocess/image.js";
|
|
11
|
+
import { ResizePipeline as p, zeroTensorData as m } from "../preprocess/pipeline.js";
|
|
12
|
+
import { softmax as h, topK as g } from "../postprocess/classification.js";
|
|
13
|
+
import { VisionTask as _ } from "./base.js";
|
|
13
14
|
//#region src/vision/tasks/classifier.ts
|
|
14
|
-
var
|
|
15
|
+
var v = class v extends _ {
|
|
15
16
|
_labels;
|
|
16
17
|
_names;
|
|
17
18
|
_inputSize;
|
|
@@ -32,7 +33,7 @@ var _ = class _ extends g {
|
|
|
32
33
|
async warmup(e = 1) {
|
|
33
34
|
let [t, n] = this._inputSize;
|
|
34
35
|
for (let r = 0; r < e; r++) {
|
|
35
|
-
let e =
|
|
36
|
+
let e = f(m(t, n), [
|
|
36
37
|
1,
|
|
37
38
|
3,
|
|
38
39
|
n,
|
|
@@ -44,22 +45,22 @@ var _ = class _ extends g {
|
|
|
44
45
|
get _pipeline() {
|
|
45
46
|
if (this._pipelineCache === null) {
|
|
46
47
|
let [e, t] = this._inputSize;
|
|
47
|
-
this._pipelineCache = new
|
|
48
|
+
this._pipelineCache = new p(e, t, this._mean, this._std);
|
|
48
49
|
}
|
|
49
50
|
return this._pipelineCache;
|
|
50
51
|
}
|
|
51
52
|
static async create(e, t = {}) {
|
|
52
|
-
let n = await
|
|
53
|
+
let n = await c.create(e, t), i = u(n.metadata, {
|
|
53
54
|
normalization: t.normalization,
|
|
54
55
|
mean: t.mean,
|
|
55
56
|
std: t.std
|
|
56
|
-
}), d = t.numClasses ??
|
|
57
|
+
}), d = t.numClasses ?? a(n.outputShape) ?? void 0, f = r(t.labels ?? s(n.metadata), { numClasses: d }), p = {};
|
|
57
58
|
for (let e = 0; e < f.length; e++) p[e] = f[e];
|
|
58
|
-
return new
|
|
59
|
+
return new v(n, f, p, o({
|
|
59
60
|
graphShape: n.inputShape,
|
|
60
61
|
requested: t.inputSize,
|
|
61
62
|
fallback: [224, 224]
|
|
62
|
-
}),
|
|
63
|
+
}), i.mean, i.std, t.applySoftmax ?? !l(n.metadata), i.name);
|
|
63
64
|
}
|
|
64
65
|
get labels() {
|
|
65
66
|
return this._labels;
|
|
@@ -76,56 +77,64 @@ var _ = class _ extends g {
|
|
|
76
77
|
async call(e, t = {}) {
|
|
77
78
|
return this.predict(e, t);
|
|
78
79
|
}
|
|
79
|
-
async predict(r,
|
|
80
|
-
let
|
|
81
|
-
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
let
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
async predict(r, a = {}) {
|
|
81
|
+
let o = new e(), s = typeof r == "string" ? r : null, { image: c, canvas: l } = await d(r);
|
|
82
|
+
o.stage("load");
|
|
83
|
+
let { tensor: u, reused: f } = this._preprocess(c, l);
|
|
84
|
+
o.stage("preprocess");
|
|
85
|
+
let p;
|
|
86
|
+
try {
|
|
87
|
+
p = await this._session.run({ [this._session.inputName]: u });
|
|
88
|
+
} finally {
|
|
89
|
+
this._pipeline.release({ reused: f });
|
|
90
|
+
}
|
|
91
|
+
o.stage("inference");
|
|
92
|
+
let m = this._session.outputNames[0];
|
|
93
|
+
if (m === void 0) throw Error("Classifier model has no outputs.");
|
|
94
|
+
let h = p[m];
|
|
95
|
+
if (h === void 0) throw Error(`Classifier model output ${m} missing from run() result.`);
|
|
96
|
+
let _ = this._postprocess(i(h.data)), { indices: v, values: y } = g(_, a.topK ?? null), b = [];
|
|
97
|
+
for (let e = 0; e < v.length; e++) {
|
|
98
|
+
let t = v[e], n = this._labels[t] ?? `class_${t}`;
|
|
99
|
+
b.push({
|
|
94
100
|
classId: t,
|
|
95
101
|
className: n,
|
|
96
|
-
probability:
|
|
102
|
+
probability: y[e],
|
|
97
103
|
cls: t,
|
|
98
104
|
name: n,
|
|
99
|
-
conf:
|
|
105
|
+
conf: y[e]
|
|
100
106
|
});
|
|
101
107
|
}
|
|
102
|
-
if (
|
|
103
|
-
let
|
|
104
|
-
classId:
|
|
105
|
-
className:
|
|
106
|
-
confidence:
|
|
107
|
-
cls:
|
|
108
|
-
name:
|
|
109
|
-
conf:
|
|
110
|
-
image:
|
|
111
|
-
probabilities:
|
|
112
|
-
},
|
|
113
|
-
return
|
|
108
|
+
if (b.length === 0) throw Error("Classifier produced no probabilities (empty output).");
|
|
109
|
+
let x = b[0], S = {
|
|
110
|
+
classId: x.classId,
|
|
111
|
+
className: x.className,
|
|
112
|
+
confidence: x.probability,
|
|
113
|
+
cls: x.classId,
|
|
114
|
+
name: x.className,
|
|
115
|
+
conf: x.probability,
|
|
116
|
+
image: c,
|
|
117
|
+
probabilities: b
|
|
118
|
+
}, C = [c.height, c.width], w = new n(_);
|
|
119
|
+
return o.stage("postprocess"), [new t(w, S, this._names, c, C, s, o.speed())];
|
|
114
120
|
}
|
|
115
|
-
_preprocess(e) {
|
|
116
|
-
let [
|
|
117
|
-
return
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
_preprocess(e, t) {
|
|
122
|
+
let [n, r] = this._inputSize, { data: i, reused: a } = this._pipeline.run(e, t);
|
|
123
|
+
return {
|
|
124
|
+
tensor: f(i, [
|
|
125
|
+
1,
|
|
126
|
+
3,
|
|
127
|
+
r,
|
|
128
|
+
n
|
|
129
|
+
]),
|
|
130
|
+
reused: a
|
|
131
|
+
};
|
|
123
132
|
}
|
|
124
133
|
_postprocess(e) {
|
|
125
|
-
return this._applySoftmax ?
|
|
134
|
+
return this._applySoftmax ? h(e) : new Float32Array(e);
|
|
126
135
|
}
|
|
127
136
|
};
|
|
128
137
|
//#endregion
|
|
129
|
-
export {
|
|
138
|
+
export { v as Classifier };
|
|
130
139
|
|
|
131
140
|
//# sourceMappingURL=classifier.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classifier.js","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const tensor = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n this._pipeline.release();\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(raw.data as Float32Array);\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): ort.Tensor {\n const [tw, th] = this._inputSize;\n const { data } = this._pipeline.run(image);\n return toFloat32Tensor(data, [1, 3, th, tw]);\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAyGA,IAAa,IAAb,MAAa,UAAmB,EAAW;CAGlB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GARI,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,aAAA,GACA,KAAA,QAAA,GACA,KAAA,OAAA,GACA,KAAA,gBAAA,GACA,KAAA,iBAAA;CAGrB;CAUA,IAAI,iBAA0B;EAC1B,OAAO,KAAK;CAChB;CAWA,IAAI,gBAAwB;EACxB,OAAO,KAAK;CAChB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AAI;GACV,OAAO;GACP;EACJ,GAEM,IAAkC,CAAC,EAAS,QAAQ,EAAS,KAAK,GAClE,IAAQ,IAAI,EAAM,CAAS;EAEjC,OADA,EAAM,MAAM,aAAa,GAClB,CACH,IAAI,EACA,GACA,GACA,KAAK,QACL,GACA,GACA,GACA,EAAM,MAAM,CAChB,CACJ;CACJ;CAEA,YAAoB,GAA6B;EAC7C,IAAM,CAAC,GAAI,KAAM,KAAK,YAChB,EAAE,YAAS,KAAK,UAAU,IAAI,CAAK;EACzC,OAAO,EAAgB,GAAM;GAAC;GAAG;GAAG;GAAI;EAAE,CAAC;CAC/C;CAEA,aAAqB,GAAiC;EAClD,OAAO,KAAK,gBAAgB,EAAQ,CAAG,IAAI,IAAI,aAAa,CAAG;CACnE;AACJ"}
|
|
1
|
+
{"version":3,"file":"classifier.js","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImageSource } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { asFloat32Array } from \"../core/dtypes\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const { image: original, canvas } = await loadImageSource(image);\n timer.stage(\"load\");\n const { tensor, reused } = this._preprocess(original, canvas);\n timer.stage(\"preprocess\");\n let outputs: Record<string, ort.Tensor>;\n try {\n outputs = await this._session.run({ [this._session.inputName]: tensor });\n } finally {\n this._pipeline.release({ reused });\n }\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(asFloat32Array(raw.data));\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Resize and pack the image into the tensor the model expects.\n *\n * @param image The decoded input.\n * @param canvas The opaque canvas it was decoded on, when there is one — see\n * {@link ResizePipeline.run}.\n */\n private _preprocess(\n image: RGBImage,\n canvas: CanvasImageSource | null,\n ): { tensor: ort.Tensor; reused: boolean } {\n const [tw, th] = this._inputSize;\n const { data, reused } = this._pipeline.run(image, canvas);\n return { tensor: toFloat32Tensor(data, [1, 3, th, tw]), reused };\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA0GA,IAAa,IAAb,MAAa,UAAmB,EAAW;CAGlB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GARI,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,aAAA,GACA,KAAA,QAAA,GACA,KAAA,OAAA,GACA,KAAA,gBAAA,GACA,KAAA,iBAAA;CAGrB;CAUA,IAAI,iBAA0B;EAC1B,OAAO,KAAK;CAChB;CAWA,IAAI,gBAAwB;EACxB,OAAO,KAAK;CAChB;CAEA,iBAAgD;CAchD,MAAM,OAAO,IAAe,GAAkB;EAC1C,IAAM,CAAC,GAAI,KAAM,KAAK;EACtB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAM,KAAK;GAC3B,IAAM,IAAS,EAAgB,EAAe,GAAI,CAAE,GAAG;IAAC;IAAG;IAAG;IAAI;GAAE,CAAC;GACrE,MAAM,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,YAAY,EAAO,CAAC;EACjE;CACJ;CASA,IAAY,YAA4B;EACpC,IAAI,KAAK,mBAAmB,MAAM;GAC9B,IAAM,CAAC,GAAI,KAAM,KAAK;GACtB,KAAK,iBAAiB,IAAI,EAAe,GAAI,GAAI,KAAK,OAAO,KAAK,IAAI;E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gB,GAAM;IAAC;IAAG;IAAG;IAAI;GAAE,CAAC;GAAG;EAAO;CACnE;CAEA,aAAqB,GAAiC;EAClD,OAAO,KAAK,gBAAgB,EAAQ,CAAG,IAAI,IAAI,aAAa,CAAG;CACnE;AACJ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../core/exceptions.cjs"),t=require("../types.cjs"),n=require("../core/timing.cjs"),r=require("../results.cjs"),i=require("../labels.cjs"),a=require("../core/
|
|
1
|
+
const e=require("../core/exceptions.cjs"),t=require("../types.cjs"),n=require("../core/timing.cjs"),r=require("../results.cjs"),i=require("../labels.cjs"),a=require("../core/dtypes.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../fusion.cjs"),l=require("../preprocess/image.cjs"),u=require("../preprocess/pipeline.cjs"),d=require("../postprocess/classification.cjs"),f=require("./base.cjs");var p=class p extends f.VisionTask{_spec;_labels;_names;_classifierLabels;_classifierNames;_raiseOnEmpty;constructor(e,t,n,r,i,a,o){super(e),this._spec=t,this._labels=n,this._names=r,this._classifierLabels=i,this._classifierNames=a,this._raiseOnEmpty=o}_pipelineCache=null;async warmup(e=1){let[t,n]=this._spec.inputSize;for(let r=0;r<e;r++){let e={[c.INPUT_IMAGE]:l.toFloat32Tensor(u.zeroTensorData(t,n),[1,3,n,t])};this._spec.needsSourceImage&&(e[c.INPUT_SOURCE]=l.toFloat32Tensor(u.zeroTensorData(t,n),[1,3,n,t]),e[c.INPUT_SCALE]=l.toFloat32Tensor(new Float32Array([1]),[1]),e[c.INPUT_PAD]=l.toFloat32Tensor(new Float32Array([0,0]),[2])),await this._session.run(e)}}get _pipeline(){if(this._pipelineCache===null){let[e,t]=this._spec.inputSize;this._pipelineCache=new u.LetterboxPipeline(e,t)}return this._pipelineCache}static async create(t,n={}){let r=await o.OrtSession.create(t,n),a=c.readFusionSpec(r.metadata);if(a===null)throw new e.FusionError(`This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a plain model with Detector/Classifier instead.`);let s=i.resolveLabels(n.labels??a.detectorNames??`coco`),l=i.resolveLabels(n.classifierLabels??a.classifierNames,{numClasses:m(r)??void 0});return new p(r,a,s,h(s),l,h(l),n.raiseOnEmpty??!1)}get spec(){return this._spec}get inputSize(){return this._spec.inputSize}get labels(){return this._labels}get names(){return this._names}get classifierLabels(){return this._classifierLabels}get classifierNames(){return this._classifierNames}async call(e,t={}){return this.predict(e,t)}async predict(e,t={}){let i=new n.SpeedTimer,o=typeof e==`string`?e:null,{image:l,canvas:u}=await s.loadImageSource(e);i.stage(`load`);let{feeds:d,scale:p,padLeft:m,padTop:h,reused:y}=this._preprocess(l,u);i.stage(`preprocess`);let C;try{C=await this._session.run(d)}finally{this._pipeline.release({reused:y})}i.stage(`inference`);let w=g(C,c.OUTPUT_PROBS),T=_(C,c.OUTPUT_BOXES),E=_(C,c.OUTPUT_SCORES),D=v(C,c.OUTPUT_CLASSES),O=a.asFloat32Array(w.data),k=v(C,`num_detections`)[0]??0,A=this._spec.hasMasks?_(C,c.OUTPUT_MASKS):null,[j,M]=this._spec.cropSize,N=Math.min(k,Math.floor(T.length/4)),P=w.dims[w.dims.length-1]??0,F=t.classes===void 0?null:new Set(t.classes),I=t.confThreshold??0,L=[];for(let e=0;e<N;e++){let n=D[e]??0,r=E[e]??0;if(r<I||F!==null&&!F.has(n))continue;let i=this._toOriginal(T,e,{scale:p,padLeft:m,padTop:h,original:l}),a=f.cropToBox(l,i);L.push(b(n,this._names[n]??`class_${n}`,r,i,a,this._classify(O.subarray(e*P,(e+1)*P),a,t.topK),A===null?null:x(A,e,j,M,a)))}f.requireDetections(L.length,{raiseOnEmpty:t.raiseOnEmpty??this._raiseOnEmpty,confThreshold:Math.max(I,this._spec.confThreshold),classes:t.classes,path:o});let R=[l.height,l.width];return i.stage(`postprocess`),[new r.DetectClassifyResults(S(L,R),L,this._names,this._classifierNames,l,R,o,i.speed())]}_preprocess(e,t){let[n,r]=this._spec.inputSize,i=this._pipeline.run(e,t),a={[c.INPUT_IMAGE]:l.toFloat32Tensor(i.data,[1,3,r,n])};return this._spec.needsSourceImage&&(a[c.INPUT_SOURCE]=y(e),a[c.INPUT_SCALE]=l.toFloat32Tensor(new Float32Array([i.scale]),[1]),a[c.INPUT_PAD]=l.toFloat32Tensor(new Float32Array([i.padLeft,i.padTop]),[2])),{feeds:a,scale:i.scale,padLeft:i.padLeft,padTop:i.padTop,reused:i.reused}}_toOriginal(e,n,r){let{scale:i,padLeft:a,padTop:o,original:s}=r,c=t=>e[n*4+t]??0,l=e=>Math.min(Math.max(e,0),s.width),u=e=>Math.min(Math.max(e,0),s.height);return new t.BoundingBox(l((c(0)-a)/i),u((c(1)-o)/i),l((c(2)-a)/i),u((c(3)-o)/i))}_classify(e,t,n){let r=this._spec.applySoftmax?d.softmax(e):e,{indices:i,values:a}=d.topK(r,n??null),o=[];for(let e=0;e<i.length;e++){let t=i[e]??0,n=a[e]??0,r=this._classifierLabels[t]??`class_${t}`;o.push({classId:t,className:r,probability:n,cls:t,name:r,conf:n})}let s=o[0]??{classId:0,className:`class_0`,probability:0,cls:0,name:`class_0`,conf:0};return{classId:s.classId,className:s.className,confidence:s.probability,cls:s.classId,name:s.className,conf:s.probability,image:t,probabilities:o}}};function m(e){let t=e.outputNames.indexOf(c.OUTPUT_PROBS);if(t<0)return null;let n=e.outputShapes[t];return n===void 0||n.length===0?null:n[n.length-1]??null}function h(e){let t={};for(let n=0;n<e.length;n++)t[n]=e[n];return t}function g(t,n){let r=t[n];if(r===void 0)throw new e.FusionError(`The fused pipeline is missing its '${n}' output.`);return r}function _(e,t){return a.asFloat32Array(g(e,t).data)}function v(e,t){let n=g(e,t).data,r=[];for(let e=0;e<n.length;e++)r.push(Number(n[e]));return r}function y(e){let t=l.toCHW(l.toFloat32(e),e.width,e.height,3);return l.toFloat32Tensor(t,[1,3,e.height,e.width])}function b(e,t,n,r,i,a,o){return{classId:e,className:t,confidence:n,bbox:r,cls:e,name:t,conf:n,box:r,croppedImage:i,classification:a,mask:o}}function x(e,n,r,i,a){let{width:o,height:s}=a;if(o<1||s<1)return null;let c=r*i,l=e.subarray(n*c,(n+1)*c),u=new Uint8Array(o*s);for(let e=0;e<s;e++){let t=Math.min(Math.floor(e*i/s),i-1);for(let n=0;n<o;n++){let i=Math.min(Math.floor(n*r/o),r-1);u[e*o+n]=(l[t*r+i]??0)>.5?255:0}}return new t.Mask(u,o,s)}function S(e,t){let n=new Float32Array(e.length*4),i=new Int32Array(e.length),a=new Float32Array(e.length);return e.forEach((e,t)=>{let[r,o,s,c]=e.bbox.xyxy;n[t*4]=r,n[t*4+1]=o,n[t*4+2]=s,n[t*4+3]=c,i[t]=e.classId,a[t]=e.confidence}),new r.Boxes(n,i,a,t)}exports.DetectClassify=p,exports.maskToBox=x;
|
|
2
2
|
//# sourceMappingURL=detectClassify.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detectClassify.cjs","names":[],"sources":["../../../src/vision/tasks/detectClassify.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Run a fused detect→classify pipeline in the browser.\n *\n * The file this loads was built by the Python SDK's `ort_vision_sdk.compose`,\n * and it already contains both models plus the crop-and-resize bridge between\n * them. That matters far more in a tab than on a server: two models mean two\n * `.onnx` downloads, two WASM/WebGPU session initializations, and a per-crop\n * round trip through JavaScript to slice, resize and restack the regions before\n * the second model can see them. A fused pipeline has one download, one session\n * and no round trip — the crops are produced and consumed inside the graph.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { FusionError } from \"../core/exceptions\";\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport {\n INPUT_IMAGE,\n INPUT_PAD,\n INPUT_SCALE,\n INPUT_SOURCE,\n OUTPUT_BOXES,\n OUTPUT_CLASSES,\n OUTPUT_NUM_DETECTIONS,\n OUTPUT_PROBS,\n OUTPUT_SCORES,\n type FusionSpec,\n readFusionSpec,\n} from \"../fusion\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { LetterboxPipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { Boxes, DetectClassifyResults } from \"../results\";\nimport {\n BoundingBox,\n RGBImage,\n type ClassProbability,\n type ClassificationResult,\n type DetectionResult,\n} from \"../types\";\nimport { VisionTask, requireDetections } from \"./base\";\n\nexport interface DetectClassifyOptions extends OrtSessionOptions {\n /**\n * Class label spec for the **detection** stage — see {@link resolveLabels}.\n * Defaults to the names recorded at fusion time, falling back to the COCO\n * 80-class preset when the fusion recorded none.\n */\n readonly labels?: LabelSpec;\n /**\n * Class label spec for the **classification** stage. Defaults to the recorded\n * names, falling back to generated `class_<id>` names.\n */\n readonly classifierLabels?: LabelSpec;\n /**\n * If `true`, a run that finds nothing throws {@link NoDetectionsError}\n * instead of returning an empty envelope. Default `false`, because looking\n * and finding nothing is a successful inference. Turn it on when an empty\n * result means the surrounding pipeline should stop rather than carry on with\n * zero rows. Can be overridden per `predict` call.\n */\n readonly raiseOnEmpty?: boolean;\n}\n\nexport interface DetectClassifyPredictOptions {\n /**\n * Drop detections scoring below this. The graph's own NMS threshold was fixed\n * at fusion time and cannot be lowered here — this only filters further.\n */\n readonly confThreshold?: number;\n /** If set, keep only detections whose detector `classId` is in this list. */\n readonly classes?: readonly number[];\n /** Truncate each detection's `classification.probabilities` to its top-k entries. */\n readonly topK?: number;\n /** Override the constructor's `raiseOnEmpty` setting for this call. */\n readonly raiseOnEmpty?: boolean;\n}\n\n/**\n * Detector and classifier running as a single ONNX model.\n *\n * Everything the pipeline needs to know about itself — the resolution to\n * letterbox to, whether it wants the full-resolution image as well, whether its\n * classifier output still needs a softmax, the class names of both stages — was\n * written into the file at fusion time and is read back here. Nothing is\n * restated on the JavaScript side, so nothing can drift out of step with the\n * Python side that built it.\n *\n * @example\n * ```typescript\n * const pipeline = await DetectClassify.create(\"/models/pipeline.onnx\");\n * const result = (await pipeline.predict(\"/images/flock.jpg\"))[0];\n * for (const detection of result) {\n * console.log(detection.name, detection.conf, detection.classification?.name);\n * }\n * ```\n */\nexport class DetectClassify extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _spec: FusionSpec,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _classifierLabels: readonly string[],\n private readonly _classifierNames: Readonly<Record<number, string>>,\n private readonly _raiseOnEmpty: boolean,\n ) {\n super(session);\n }\n\n private _pipelineCache: LetterboxPipeline | null = null;\n\n /**\n * Run the model once on zero-filled inputs, paying one-time costs up front.\n *\n * Worth more here than on a single-stage task: a fused pipeline is two models\n * plus the bridge in one graph, so the first inference compiles shaders for\n * all of it. Calling this while a loading spinner is still up moves that cost\n * somewhere the user is already waiting.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [width, height] = this._spec.inputSize;\n for (let i = 0; i < runs; i++) {\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]),\n };\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([1]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(new Float32Array([0, 0]), [2]);\n }\n await this._session.run(feeds);\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it allocates canvases: a pipeline built in an\n * environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): LetterboxPipeline {\n if (this._pipelineCache === null) {\n const [width, height] = this._spec.inputSize;\n this._pipelineCache = new LetterboxPipeline(width, height);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load a fused pipeline and resolve both label spaces.\n *\n * @param model The fused `.onnx` — a URL, an `ArrayBuffer`, or bytes.\n * @param options Label overrides plus the usual session options.\n * @throws {@link FusionError} when the model carries no pipeline metadata,\n * i.e. it is a plain detector or classifier rather than something\n * `ort_vision_sdk.compose` produced.\n */\n static async create(\n model: ModelSource,\n options: DetectClassifyOptions = {},\n ): Promise<DetectClassify> {\n const session = await OrtSession.create(model, options);\n const spec = readFusionSpec(session.metadata);\n if (spec === null) {\n throw new FusionError(\n \"This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to \" +\n \"drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a \" +\n \"plain model with Detector/Classifier instead.\",\n );\n }\n\n const labels = resolveLabels(options.labels ?? spec.detectorNames ?? \"coco\");\n const classifierLabels = resolveLabels(options.classifierLabels ?? spec.classifierNames, {\n numClasses: classifierClasses(session) ?? undefined,\n });\n return new DetectClassify(\n session,\n spec,\n labels,\n indexNames(labels),\n classifierLabels,\n indexNames(classifierLabels),\n options.raiseOnEmpty ?? false,\n );\n }\n\n /** The pipeline configuration recorded in the model at fusion time. */\n get spec(): FusionSpec {\n return this._spec;\n }\n\n /** The `[width, height]` the detection stage runs at. */\n get inputSize(): readonly [number, number] {\n return this._spec.inputSize;\n }\n\n /** Detector class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Detector class id → class name (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /** Classifier class labels indexed by class id. */\n get classifierLabels(): readonly string[] {\n return this._classifierLabels;\n }\n\n /** Classifier class id → class name. */\n get classifierNames(): Readonly<Record<number, string>> {\n return this._classifierNames;\n }\n\n /**\n * Alias for {@link predict} — call the pipeline like a torch `nn.Module`.\n *\n * Use as `pipeline.call(img)` since JavaScript class instances are not\n * callable; for direct invocation, prefer `pipeline.predict(img)`.\n */\n async call(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run the pipeline on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`. Its\n * `inference` figure covers detection *and* classification, since the\n * pipeline runs them as one graph and no boundary between them is observable\n * from outside.\n */\n async predict(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const { feeds, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run(feeds);\n this._pipeline.release();\n timer.stage(\"inference\");\n\n const probsTensor = output(outputs, OUTPUT_PROBS);\n const boxes = floats(outputs, OUTPUT_BOXES);\n const scores = floats(outputs, OUTPUT_SCORES);\n const classes = integers(outputs, OUTPUT_CLASSES);\n const probs = probsTensor.data as Float32Array;\n const reported = integers(outputs, OUTPUT_NUM_DETECTIONS)[0] ?? 0;\n const rows = Math.min(reported, Math.floor(boxes.length / 4));\n const classCount = probsTensor.dims[probsTensor.dims.length - 1] ?? 0;\n\n const allowed = options.classes === undefined ? null : new Set(options.classes);\n const floor = options.confThreshold ?? 0;\n const detections: DetectionResult[] = [];\n for (let row = 0; row < rows; row++) {\n const classId = classes[row] ?? 0;\n const confidence = scores[row] ?? 0;\n if (confidence < floor || (allowed !== null && !allowed.has(classId))) continue;\n\n const bbox = this._toOriginal(boxes, row, { scale, padLeft, padTop, original });\n const cropped = crop(original, bbox);\n detections.push(\n detection(\n classId,\n this._names[classId] ?? `class_${classId}`,\n confidence,\n bbox,\n cropped,\n this._classify(\n probs.subarray(row * classCount, (row + 1) * classCount),\n cropped,\n options.topK,\n ),\n ),\n );\n }\n\n requireDetections(detections.length, {\n raiseOnEmpty: options.raiseOnEmpty ?? this._raiseOnEmpty,\n confThreshold: Math.max(floor, this._spec.confThreshold),\n classes: options.classes,\n path,\n });\n\n const origShape: readonly [number, number] = [original.height, original.width];\n timer.stage(\"postprocess\");\n return [\n new DetectClassifyResults(\n bulkBoxes(detections, origShape),\n detections,\n this._names,\n this._classifierNames,\n original,\n origShape,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Letterbox the image and build the graph's feeds.\n *\n * The detector input runs through {@link LetterboxPipeline}, which fuses the\n * resize, the padding and the HWC-to-CHW float conversion into one\n * `drawImage` plus one readback, and reuses its output buffer between frames.\n * That buffer goes straight to ONNX Runtime, so `_pipeline.release()` must not\n * be called until the run resolves.\n *\n * A pipeline fused with `cropSource: \"original\"` also takes the untouched\n * image as a second input, plus the scale and padding of the letterbox — that\n * is what lets the graph undo the letterbox transform internally and crop at\n * native resolution instead of from the downscaled copy. That one is **not**\n * letterboxed by definition, so it does not go through the fused path.\n */\n private _preprocess(image: RGBImage): {\n feeds: Record<string, ort.Tensor>;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [width, height] = this._spec.inputSize;\n const boxed = this._pipeline.run(image);\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(boxed.data, [1, 3, height, width]),\n };\n\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = tensorOf(image);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([boxed.scale]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(\n new Float32Array([boxed.padLeft, boxed.padTop]),\n [2],\n );\n }\n return { feeds, scale: boxed.scale, padLeft: boxed.padLeft, padTop: boxed.padTop };\n }\n\n /**\n * Map one letterboxed xyxy row back onto the original image.\n *\n * The graph always reports boxes in the detector's letterboxed pixel space,\n * whichever crop source it was fused with, so both sources agree here.\n */\n private _toOriginal(\n boxes: Float32Array,\n row: number,\n context: {\n scale: number;\n padLeft: number;\n padTop: number;\n original: RGBImage;\n },\n ): BoundingBox {\n const { scale, padLeft, padTop, original } = context;\n const at = (offset: number): number => boxes[row * 4 + offset] ?? 0;\n const clampX = (value: number): number => Math.min(Math.max(value, 0), original.width);\n const clampY = (value: number): number => Math.min(Math.max(value, 0), original.height);\n return new BoundingBox(\n clampX((at(0) - padLeft) / scale),\n clampY((at(1) - padTop) / scale),\n clampX((at(2) - padLeft) / scale),\n clampY((at(3) - padTop) / scale),\n );\n }\n\n /**\n * Turn one row of the classifier output into a result object.\n *\n * @param row The output row for this detection.\n * @param image The crop the row describes, carried so callers can display\n * what was classified.\n * @param k Optional truncation of the probability list.\n */\n private _classify(\n row: Float32Array,\n image: RGBImage,\n k: number | undefined,\n ): ClassificationResult {\n const scores = this._spec.applySoftmax ? softmax(row) : row;\n const { indices, values } = topK(scores, k ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const classId = indices[i] ?? 0;\n const probability = values[i] ?? 0;\n const className = this._classifierLabels[classId] ?? `class_${classId}`;\n probabilities.push({\n classId,\n className,\n probability,\n cls: classId,\n name: className,\n conf: probability,\n });\n }\n const top = probabilities[0] ?? {\n classId: 0,\n className: \"class_0\",\n probability: 0,\n cls: 0,\n name: \"class_0\",\n conf: 0,\n };\n return {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image,\n probabilities,\n };\n }\n}\n\n/**\n * Read the classifier stage's class count off the `probs` output shape.\n *\n * @param session The loaded pipeline session.\n * @returns The class count, or `null` when the graph leaves that axis dynamic\n * or declares no `probs` output — in which case label resolution falls back\n * to whatever the fusion recorded.\n */\nfunction classifierClasses(session: OrtSession): number | null {\n const index = session.outputNames.indexOf(OUTPUT_PROBS);\n if (index < 0) return null;\n const shape = session.outputShapes[index];\n if (shape === undefined || shape.length === 0) return null;\n return shape[shape.length - 1] ?? null;\n}\n\n/**\n * Build a class id → name record from an ordered label list.\n *\n * @param labels Labels indexed by class id.\n * @returns The equivalent record.\n */\nfunction indexNames(labels: readonly string[]): Readonly<Record<number, string>> {\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) names[i] = labels[i] as string;\n return names;\n}\n\n/**\n * Fetch an output tensor by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The tensor.\n * @throws {@link FusionError} when the graph does not carry that output, which\n * means the file is not a pipeline this version can drive.\n */\nfunction output(outputs: Record<string, ort.Tensor>, name: string): ort.Tensor {\n const tensor = outputs[name];\n if (tensor === undefined) {\n throw new FusionError(`The fused pipeline is missing its '${name}' output.`);\n }\n return tensor;\n}\n\n/**\n * Fetch a float output by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's data.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction floats(outputs: Record<string, ort.Tensor>, name: string): Float32Array {\n return output(outputs, name).data as Float32Array;\n}\n\n/**\n * Fetch an integer output by name, normalizing ORT's 64-bit representation.\n *\n * ONNX Runtime Web returns `int64` tensors as `BigInt64Array`, whose values do\n * not compare or index like numbers. Class ids and detection counts are always\n * small, so widening them to `number` here is lossless and keeps every caller\n * free of `BigInt` handling.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's values as plain numbers.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction integers(outputs: Record<string, ort.Tensor>, name: string): number[] {\n const data = output(outputs, name).data as BigInt64Array | Int32Array | Float32Array;\n const values: number[] = [];\n for (let i = 0; i < data.length; i++) values.push(Number(data[i]));\n return values;\n}\n\n/**\n * Convert an image to the `[1, 3, H, W]` float32 tensor the graph expects.\n *\n * @param image The image to convert.\n * @returns The batched CHW tensor, scaled to `[0, 1]`.\n */\nfunction tensorOf(image: RGBImage): ort.Tensor {\n const chw = toCHW(toFloat32(image), image.width, image.height, 3);\n return toFloat32Tensor(chw, [1, 3, image.height, image.width]);\n}\n\n/**\n * Cut the box region out of the original image.\n *\n * @param image The source image.\n * @param bbox The box, in original-image pixel coordinates.\n * @returns The cropped region, or a zero-sized image for a box with no area.\n */\nfunction crop(image: RGBImage, bbox: BoundingBox): RGBImage {\n const [rawX1, rawY1, rawX2, rawY2] = bbox.asIntXyxy();\n const x1 = Math.max(0, rawX1);\n const y1 = Math.max(0, rawY1);\n const x2 = Math.min(image.width, rawX2);\n const y2 = Math.min(image.height, rawY2);\n if (x2 <= x1 || y2 <= y1) return new RGBImage(new Uint8Array(0), 0, 0);\n\n const width = x2 - x1;\n const height = y2 - y1;\n const out = new Uint8Array(width * height * 3);\n for (let row = 0; row < height; row++) {\n const offset = ((y1 + row) * image.width + x1) * 3;\n out.set(image.data.subarray(offset, offset + width * 3), row * width * 3);\n }\n return new RGBImage(out, width, height);\n}\n\n/**\n * Assemble one detection, filling the Ultralytics-style aliases.\n *\n * @param classId Detector class index.\n * @param className Detector class name.\n * @param confidence Detection score.\n * @param bbox Box in original-image coordinates.\n * @param croppedImage The region the box covers.\n * @param classification What the classification stage said about that region.\n * @returns The detection object.\n */\nfunction detection(\n classId: number,\n className: string,\n confidence: number,\n bbox: BoundingBox,\n croppedImage: RGBImage,\n classification: ClassificationResult,\n): DetectionResult {\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage,\n classification,\n };\n}\n\n/**\n * Assemble the bulk-array `Boxes` view from per-instance detections.\n *\n * @param detections The surviving detections.\n * @param origShape `[height, width]` of the original image.\n * @returns The bulk view, empty when nothing survived.\n */\nfunction bulkBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n): Boxes {\n const xyxy = new Float32Array(detections.length * 4);\n const cls = new Int32Array(detections.length);\n const conf = new Float32Array(detections.length);\n detections.forEach((entry, index) => {\n const [x1, y1, x2, y2] = entry.bbox.xyxy;\n xyxy[index * 4] = x1;\n xyxy[index * 4 + 1] = y1;\n xyxy[index * 4 + 2] = x2;\n xyxy[index * 4 + 3] = y2;\n cls[index] = entry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,EACtB,EAAI,GAAS,EAAM,QACnB,EAAK,GAAS,EAAM,UACxB,CAAC,EACM,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C"}
|
|
1
|
+
{"version":3,"file":"detectClassify.cjs","names":[],"sources":["../../../src/vision/tasks/detectClassify.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Run a fused detect→classify pipeline in the browser.\n *\n * The file this loads was built by the Python SDK's `ort_vision_sdk.compose`,\n * and it already contains both models plus the crop-and-resize bridge between\n * them. That matters far more in a tab than on a server: two models mean two\n * `.onnx` downloads, two WASM/WebGPU session initializations, and a per-crop\n * round trip through JavaScript to slice, resize and restack the regions before\n * the second model can see them. A fused pipeline has one download, one session\n * and no round trip — the crops are produced and consumed inside the graph.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { FusionError } from \"../core/exceptions\";\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport {\n INPUT_IMAGE,\n INPUT_PAD,\n INPUT_SCALE,\n INPUT_SOURCE,\n OUTPUT_BOXES,\n OUTPUT_CLASSES,\n OUTPUT_NUM_DETECTIONS,\n OUTPUT_MASKS,\n OUTPUT_PROBS,\n OUTPUT_SCORES,\n type FusionSpec,\n readFusionSpec,\n} from \"../fusion\";\nimport { type ImageInput, loadImageSource } from \"../io/image\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { asFloat32Array } from \"../core/dtypes\";\nimport { toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { LetterboxPipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { Boxes, DetectClassifyResults } from \"../results\";\nimport type { RGBImage } from \"../types\";\nimport {\n BoundingBox,\n Mask,\n type ClassProbability,\n type ClassificationResult,\n type DetectionResult,\n} from \"../types\";\nimport { VisionTask, cropToBox, requireDetections } from \"./base\";\n\nexport interface DetectClassifyOptions extends OrtSessionOptions {\n /**\n * Class label spec for the **detection** stage — see {@link resolveLabels}.\n * Defaults to the names recorded at fusion time, falling back to the COCO\n * 80-class preset when the fusion recorded none.\n */\n readonly labels?: LabelSpec;\n /**\n * Class label spec for the **classification** stage. Defaults to the recorded\n * names, falling back to generated `class_<id>` names.\n */\n readonly classifierLabels?: LabelSpec;\n /**\n * If `true`, a run that finds nothing throws {@link NoDetectionsError}\n * instead of returning an empty envelope. Default `false`, because looking\n * and finding nothing is a successful inference. Turn it on when an empty\n * result means the surrounding pipeline should stop rather than carry on with\n * zero rows. Can be overridden per `predict` call.\n */\n readonly raiseOnEmpty?: boolean;\n}\n\nexport interface DetectClassifyPredictOptions {\n /**\n * Drop detections scoring below this. The graph's own NMS threshold was fixed\n * at fusion time and cannot be lowered here — this only filters further.\n */\n readonly confThreshold?: number;\n /** If set, keep only detections whose detector `classId` is in this list. */\n readonly classes?: readonly number[];\n /** Truncate each detection's `classification.probabilities` to its top-k entries. */\n readonly topK?: number;\n /** Override the constructor's `raiseOnEmpty` setting for this call. */\n readonly raiseOnEmpty?: boolean;\n}\n\n/**\n * Detector and classifier running as a single ONNX model.\n *\n * Everything the pipeline needs to know about itself — the resolution to\n * letterbox to, whether it wants the full-resolution image as well, whether its\n * classifier output still needs a softmax, the class names of both stages — was\n * written into the file at fusion time and is read back here. Nothing is\n * restated on the JavaScript side, so nothing can drift out of step with the\n * Python side that built it.\n *\n * @example\n * ```typescript\n * const pipeline = await DetectClassify.create(\"/models/pipeline.onnx\");\n * const result = (await pipeline.predict(\"/images/flock.jpg\"))[0];\n * for (const detection of result) {\n * console.log(detection.name, detection.conf, detection.classification?.name);\n * }\n * ```\n */\nexport class DetectClassify extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _spec: FusionSpec,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _classifierLabels: readonly string[],\n private readonly _classifierNames: Readonly<Record<number, string>>,\n private readonly _raiseOnEmpty: boolean,\n ) {\n super(session);\n }\n\n private _pipelineCache: LetterboxPipeline | null = null;\n\n /**\n * Run the model once on zero-filled inputs, paying one-time costs up front.\n *\n * Worth more here than on a single-stage task: a fused pipeline is two models\n * plus the bridge in one graph, so the first inference compiles shaders for\n * all of it. Calling this while a loading spinner is still up moves that cost\n * somewhere the user is already waiting.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [width, height] = this._spec.inputSize;\n for (let i = 0; i < runs; i++) {\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]),\n };\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = toFloat32Tensor(zeroTensorData(width, height), [\n 1,\n 3,\n height,\n width,\n ]);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([1]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(new Float32Array([0, 0]), [2]);\n }\n await this._session.run(feeds);\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it allocates canvases: a pipeline built in an\n * environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): LetterboxPipeline {\n if (this._pipelineCache === null) {\n const [width, height] = this._spec.inputSize;\n this._pipelineCache = new LetterboxPipeline(width, height);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load a fused pipeline and resolve both label spaces.\n *\n * @param model The fused `.onnx` — a URL, an `ArrayBuffer`, or bytes.\n * @param options Label overrides plus the usual session options.\n * @throws {@link FusionError} when the model carries no pipeline metadata,\n * i.e. it is a plain detector or classifier rather than something\n * `ort_vision_sdk.compose` produced.\n */\n static async create(\n model: ModelSource,\n options: DetectClassifyOptions = {},\n ): Promise<DetectClassify> {\n const session = await OrtSession.create(model, options);\n const spec = readFusionSpec(session.metadata);\n if (spec === null) {\n throw new FusionError(\n \"This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to \" +\n \"drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a \" +\n \"plain model with Detector/Classifier instead.\",\n );\n }\n\n const labels = resolveLabels(options.labels ?? spec.detectorNames ?? \"coco\");\n const classifierLabels = resolveLabels(options.classifierLabels ?? spec.classifierNames, {\n numClasses: classifierClasses(session) ?? undefined,\n });\n return new DetectClassify(\n session,\n spec,\n labels,\n indexNames(labels),\n classifierLabels,\n indexNames(classifierLabels),\n options.raiseOnEmpty ?? false,\n );\n }\n\n /** The pipeline configuration recorded in the model at fusion time. */\n get spec(): FusionSpec {\n return this._spec;\n }\n\n /** The `[width, height]` the detection stage runs at. */\n get inputSize(): readonly [number, number] {\n return this._spec.inputSize;\n }\n\n /** Detector class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Detector class id → class name (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /** Classifier class labels indexed by class id. */\n get classifierLabels(): readonly string[] {\n return this._classifierLabels;\n }\n\n /** Classifier class id → class name. */\n get classifierNames(): Readonly<Record<number, string>> {\n return this._classifierNames;\n }\n\n /**\n * Alias for {@link predict} — call the pipeline like a torch `nn.Module`.\n *\n * Use as `pipeline.call(img)` since JavaScript class instances are not\n * callable; for direct invocation, prefer `pipeline.predict(img)`.\n */\n async call(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run the pipeline on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`. Its\n * `inference` figure covers detection *and* classification, since the\n * pipeline runs them as one graph and no boundary between them is observable\n * from outside.\n */\n async predict(\n image: ImageInput,\n options: DetectClassifyPredictOptions = {},\n ): Promise<DetectClassifyResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const { image: original, canvas } = await loadImageSource(image);\n timer.stage(\"load\");\n const { feeds, scale, padLeft, padTop, reused } = this._preprocess(original, canvas);\n timer.stage(\"preprocess\");\n let outputs: Record<string, ort.Tensor>;\n try {\n outputs = await this._session.run(feeds);\n } finally {\n this._pipeline.release({ reused });\n }\n timer.stage(\"inference\");\n\n const probsTensor = output(outputs, OUTPUT_PROBS);\n const boxes = floats(outputs, OUTPUT_BOXES);\n const scores = floats(outputs, OUTPUT_SCORES);\n const classes = integers(outputs, OUTPUT_CLASSES);\n const probs = asFloat32Array(probsTensor.data);\n const reported = integers(outputs, OUTPUT_NUM_DETECTIONS)[0] ?? 0;\n const masks = this._spec.hasMasks ? floats(outputs, OUTPUT_MASKS) : null;\n const [cropWidth, cropHeight] = this._spec.cropSize;\n const rows = Math.min(reported, Math.floor(boxes.length / 4));\n const classCount = probsTensor.dims[probsTensor.dims.length - 1] ?? 0;\n\n const allowed = options.classes === undefined ? null : new Set(options.classes);\n const floor = options.confThreshold ?? 0;\n const detections: DetectionResult[] = [];\n for (let row = 0; row < rows; row++) {\n const classId = classes[row] ?? 0;\n const confidence = scores[row] ?? 0;\n if (confidence < floor || (allowed !== null && !allowed.has(classId))) continue;\n\n const bbox = this._toOriginal(boxes, row, { scale, padLeft, padTop, original });\n const cropped = cropToBox(original, bbox);\n detections.push(\n detection(\n classId,\n this._names[classId] ?? `class_${classId}`,\n confidence,\n bbox,\n cropped,\n this._classify(\n probs.subarray(row * classCount, (row + 1) * classCount),\n cropped,\n options.topK,\n ),\n masks === null ? null : maskToBox(masks, row, cropWidth, cropHeight, cropped),\n ),\n );\n }\n\n requireDetections(detections.length, {\n raiseOnEmpty: options.raiseOnEmpty ?? this._raiseOnEmpty,\n confThreshold: Math.max(floor, this._spec.confThreshold),\n classes: options.classes,\n path,\n });\n\n const origShape: readonly [number, number] = [original.height, original.width];\n timer.stage(\"postprocess\");\n return [\n new DetectClassifyResults(\n bulkBoxes(detections, origShape),\n detections,\n this._names,\n this._classifierNames,\n original,\n origShape,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Letterbox the image and build the graph's feeds.\n *\n * The detector input runs through {@link LetterboxPipeline}, which fuses the\n * resize, the padding and the HWC-to-CHW float conversion into one\n * `drawImage` plus one readback, and reuses its output buffer between frames.\n * That buffer goes straight to ONNX Runtime, so `_pipeline.release()` must not\n * be called until the run resolves.\n *\n * A pipeline fused with `cropSource: \"original\"` also takes the untouched\n * image as a second input, plus the scale and padding of the letterbox — that\n * is what lets the graph undo the letterbox transform internally and crop at\n * native resolution instead of from the downscaled copy. That one is **not**\n * letterboxed by definition, so it does not go through the fused path.\n *\n * @param image The decoded input.\n * @param canvas The opaque canvas it was decoded on, when there is one — see\n * {@link LetterboxPipeline.run}.\n */\n private _preprocess(\n image: RGBImage,\n canvas: CanvasImageSource | null,\n ): {\n feeds: Record<string, ort.Tensor>;\n scale: number;\n padLeft: number;\n padTop: number;\n reused: boolean;\n } {\n const [width, height] = this._spec.inputSize;\n const boxed = this._pipeline.run(image, canvas);\n const feeds: Record<string, ort.Tensor> = {\n [INPUT_IMAGE]: toFloat32Tensor(boxed.data, [1, 3, height, width]),\n };\n\n if (this._spec.needsSourceImage) {\n feeds[INPUT_SOURCE] = tensorOf(image);\n feeds[INPUT_SCALE] = toFloat32Tensor(new Float32Array([boxed.scale]), [1]);\n feeds[INPUT_PAD] = toFloat32Tensor(\n new Float32Array([boxed.padLeft, boxed.padTop]),\n [2],\n );\n }\n return {\n feeds,\n scale: boxed.scale,\n padLeft: boxed.padLeft,\n padTop: boxed.padTop,\n reused: boxed.reused,\n };\n }\n\n /**\n * Map one letterboxed xyxy row back onto the original image.\n *\n * The graph always reports boxes in the detector's letterboxed pixel space,\n * whichever crop source it was fused with, so both sources agree here.\n */\n private _toOriginal(\n boxes: Float32Array,\n row: number,\n context: {\n scale: number;\n padLeft: number;\n padTop: number;\n original: RGBImage;\n },\n ): BoundingBox {\n const { scale, padLeft, padTop, original } = context;\n const at = (offset: number): number => boxes[row * 4 + offset] ?? 0;\n const clampX = (value: number): number => Math.min(Math.max(value, 0), original.width);\n const clampY = (value: number): number => Math.min(Math.max(value, 0), original.height);\n return new BoundingBox(\n clampX((at(0) - padLeft) / scale),\n clampY((at(1) - padTop) / scale),\n clampX((at(2) - padLeft) / scale),\n clampY((at(3) - padTop) / scale),\n );\n }\n\n /**\n * Turn one row of the classifier output into a result object.\n *\n * @param row The output row for this detection.\n * @param image The crop the row describes, carried so callers can display\n * what was classified.\n * @param k Optional truncation of the probability list.\n */\n private _classify(\n row: Float32Array,\n image: RGBImage,\n k: number | undefined,\n ): ClassificationResult {\n const scores = this._spec.applySoftmax ? softmax(row) : row;\n const { indices, values } = topK(scores, k ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const classId = indices[i] ?? 0;\n const probability = values[i] ?? 0;\n const className = this._classifierLabels[classId] ?? `class_${classId}`;\n probabilities.push({\n classId,\n className,\n probability,\n cls: classId,\n name: className,\n conf: probability,\n });\n }\n const top = probabilities[0] ?? {\n classId: 0,\n className: \"class_0\",\n probability: 0,\n cls: 0,\n name: \"class_0\",\n conf: 0,\n };\n return {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image,\n probabilities,\n };\n }\n}\n\n/**\n * Read the classifier stage's class count off the `probs` output shape.\n *\n * @param session The loaded pipeline session.\n * @returns The class count, or `null` when the graph leaves that axis dynamic\n * or declares no `probs` output — in which case label resolution falls back\n * to whatever the fusion recorded.\n */\nfunction classifierClasses(session: OrtSession): number | null {\n const index = session.outputNames.indexOf(OUTPUT_PROBS);\n if (index < 0) return null;\n const shape = session.outputShapes[index];\n if (shape === undefined || shape.length === 0) return null;\n return shape[shape.length - 1] ?? null;\n}\n\n/**\n * Build a class id → name record from an ordered label list.\n *\n * @param labels Labels indexed by class id.\n * @returns The equivalent record.\n */\nfunction indexNames(labels: readonly string[]): Readonly<Record<number, string>> {\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) names[i] = labels[i] as string;\n return names;\n}\n\n/**\n * Fetch an output tensor by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The tensor.\n * @throws {@link FusionError} when the graph does not carry that output, which\n * means the file is not a pipeline this version can drive.\n */\nfunction output(outputs: Record<string, ort.Tensor>, name: string): ort.Tensor {\n const tensor = outputs[name];\n if (tensor === undefined) {\n throw new FusionError(`The fused pipeline is missing its '${name}' output.`);\n }\n return tensor;\n}\n\n/**\n * Fetch a float output by name.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's data.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction floats(outputs: Record<string, ort.Tensor>, name: string): Float32Array {\n return asFloat32Array(output(outputs, name).data);\n}\n\n/**\n * Fetch an integer output by name, normalizing ORT's 64-bit representation.\n *\n * ONNX Runtime Web returns `int64` tensors as `BigInt64Array`, whose values do\n * not compare or index like numbers. Class ids and detection counts are always\n * small, so widening them to `number` here is lossless and keeps every caller\n * free of `BigInt` handling.\n *\n * @param outputs The run's results.\n * @param name The output's name in the pipeline contract.\n * @returns The output's values as plain numbers.\n * @throws {@link FusionError} when the graph does not carry that output.\n */\nfunction integers(outputs: Record<string, ort.Tensor>, name: string): number[] {\n const data = output(outputs, name).data as BigInt64Array | Int32Array | Float32Array;\n const values: number[] = [];\n for (let i = 0; i < data.length; i++) values.push(Number(data[i]));\n return values;\n}\n\n/**\n * Convert an image to the `[1, 3, H, W]` float32 tensor the graph expects.\n *\n * @param image The image to convert.\n * @returns The batched CHW tensor, scaled to `[0, 1]`.\n */\nfunction tensorOf(image: RGBImage): ort.Tensor {\n const chw = toCHW(toFloat32(image), image.width, image.height, 3);\n return toFloat32Tensor(chw, [1, 3, image.height, image.width]);\n}\n\n/**\n * Assemble one detection, filling the Ultralytics-style aliases.\n *\n * @param classId Detector class index.\n * @param className Detector class name.\n * @param confidence Detection score.\n * @param bbox Box in original-image coordinates.\n * @param croppedImage The region the box covers.\n * @param classification What the classification stage said about that region.\n * @returns The detection object.\n */\nfunction detection(\n classId: number,\n className: string,\n confidence: number,\n bbox: BoundingBox,\n croppedImage: RGBImage,\n classification: ClassificationResult,\n mask: Mask | null,\n): DetectionResult {\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage,\n classification,\n mask,\n };\n}\n\n/**\n * Resample a crop-space mask onto the cropped image's own pixel grid.\n *\n * Exported for the test suite rather than for callers: it is not re-exported\n * from the package root, and a mask reaches user code already resampled, on\n * {@link DetectionResult.mask}.\n *\n * The graph computes masks at the resolution the segmenter was exported at,\n * which is rarely the size of the box in the original image. Resampling here is\n * what lets {@link DetectionResult.mask} carry the contract `Segmenter` already\n * produces, so a caller that handles one handles the other.\n *\n * Nearest-neighbour on purpose: the values are already thresholded, and\n * interpolating between them would invent edge pixels that are neither.\n *\n * @param masks The graph's `masks` output.\n * @param row Which detection row to read.\n * @param cropWidth Width of one mask in the graph's output.\n * @param cropHeight Height of one mask in the graph's output.\n * @param target The cropped image the mask must line up with.\n * @returns The mask on the crop's grid, or `null` when the box has no area.\n */\nexport function maskToBox(\n masks: Float32Array,\n row: number,\n cropWidth: number,\n cropHeight: number,\n target: RGBImage,\n): Mask | null {\n const { width, height } = target;\n if (width < 1 || height < 1) return null;\n const stride = cropWidth * cropHeight;\n const source = masks.subarray(row * stride, (row + 1) * stride);\n const data = new Uint8Array(width * height);\n for (let y = 0; y < height; y++) {\n const sourceRow = Math.min(Math.floor((y * cropHeight) / height), cropHeight - 1);\n for (let x = 0; x < width; x++) {\n const sourceColumn = Math.min(Math.floor((x * cropWidth) / width), cropWidth - 1);\n data[y * width + x] =\n (source[sourceRow * cropWidth + sourceColumn] ?? 0) > 0.5 ? 255 : 0;\n }\n }\n return new Mask(data, width, height);\n}\n\n/**\n * Assemble the bulk-array `Boxes` view from per-instance detections.\n *\n * @param detections The surviving detections.\n * @param origShape `[height, width]` of the original image.\n * @returns The bulk view, empty when nothing survived.\n */\nfunction bulkBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n): Boxes {\n const xyxy = new Float32Array(detections.length * 4);\n const cls = new Int32Array(detections.length);\n const conf = new Float32Array(detections.length);\n detections.forEach((entry, index) => {\n const [x1, y1, x2, y2] = entry.bbox.xyxy;\n xyxy[index * 4] = x1;\n xyxy[index * 4 + 1] = y1;\n xyxy[index * 4 + 2] = x2;\n xyxy[index * 4 + 3] = y2;\n cls[index] = entry.classId;\n conf[index] = entry.confidence;\n });\n return new Boxes(xyxy, cls, conf, origShape);\n}\n"],"mappings":"waAwGA,IAAa,EAAb,MAAa,UAAuB,EAAA,UAAW,CAGtB,MACA,QACA,OACA,kBACA,iBACA,cAPrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EAPI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,kBAAA,EACA,KAAA,iBAAA,EACA,KAAA,cAAA,CAGrB,CAEA,eAAmD,KAanD,MAAM,OAAO,EAAe,EAAkB,CAC1C,GAAM,CAAC,EAAO,GAAU,KAAK,MAAM,UACnC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,IAAK,CAC3B,IAAM,EAAoC,EACrC,EAAA,aAAc,EAAA,gBAAgB,EAAA,eAAe,EAAO,CAAM,EAAG,CAC1D,EACA,EACA,EACA,CACJ,CAAC,CACL,EACI,KAAK,MAAM,mBACX,EAAM,EAAA,cAAgB,EAAA,gBAAgB,EAAA,eAAe,EAAO,CAAM,EAAG,CACjE,EACA,EACA,EACA,CACJ,CAAC,EACD,EAAM,EAAA,aAAe,EAAA,gBAAgB,IAAI,aAAa,CAAC,CAAC,CAAC,EAAG,CAAC,CAAC,CAAC,EAC/D,EAAM,EAAA,WAAa,EAAA,gBAAgB,IAAI,aAAa,CAAC,EAAG,CAAC,CAAC,EAAG,C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