tempest-react-sdk 0.36.0 → 0.38.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.
@@ -1,21 +1,23 @@
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 { OrtSession as i } from "../core/session.js";
5
- import { loadImage as a } from "../io/image.js";
6
- import { normalize as o, resize as s, toCHW as c, toFloat32Tensor as l } from "../preprocess/image.js";
7
- import { softmax as u, topK as d } from "../postprocess/classification.js";
8
- import { VisionTask as f } from "./base.js";
4
+ import { classificationNumClasses as i, resolveInputSize as a } from "../core/graph.js";
5
+ import { modelNames as o } from "../core/metadata.js";
6
+ import { OrtSession as s } from "../core/session.js";
7
+ import { loadImage as c } from "../io/image.js";
8
+ import { normalize as l, resize as u, toCHW as d, toFloat32Tensor as f } from "../preprocess/image.js";
9
+ import { softmax as p, topK as m } from "../postprocess/classification.js";
10
+ import { VisionTask as h } from "./base.js";
9
11
  //#region src/vision/tasks/classifier.ts
10
- var p = [
12
+ var g = [
11
13
  .485,
12
14
  .456,
13
15
  .406
14
- ], m = [
16
+ ], _ = [
15
17
  .229,
16
18
  .224,
17
19
  .225
18
- ], h = class h extends f {
20
+ ], v = class v extends h {
19
21
  _labels;
20
22
  _names;
21
23
  _inputSize;
@@ -25,10 +27,14 @@ var p = [
25
27
  constructor(e, t, n, r, i, a, o) {
26
28
  super(e), this._labels = t, this._names = n, this._inputSize = r, this._mean = i, this._std = a, this._applySoftmax = o;
27
29
  }
28
- static async create(e, t) {
29
- let n = await i.create(e, t), a = r(t.labels, { numClasses: t.numClasses }), o = {};
30
- for (let e = 0; e < a.length; e++) o[e] = a[e];
31
- return new h(n, a, o, t.inputSize ?? [224, 224], t.mean ?? p, t.std ?? m, t.applySoftmax ?? !0);
30
+ static async create(e, t = {}) {
31
+ let n = await s.create(e, t), c = t.numClasses ?? i(n.outputShape) ?? void 0, l = r(t.labels ?? o(n.metadata), { numClasses: c }), u = {};
32
+ for (let e = 0; e < l.length; e++) u[e] = l[e];
33
+ return new v(n, l, u, a({
34
+ graphShape: n.inputShape,
35
+ requested: t.inputSize,
36
+ fallback: [224, 224]
37
+ }), t.mean ?? g, t.std ?? _, t.applySoftmax ?? !0);
32
38
  }
33
39
  get labels() {
34
40
  return this._labels;
@@ -36,6 +42,9 @@ var p = [
36
42
  get names() {
37
43
  return this._names;
38
44
  }
45
+ get inputSize() {
46
+ return this._inputSize;
47
+ }
39
48
  get numClasses() {
40
49
  return this._labels.length;
41
50
  }
@@ -43,17 +52,17 @@ var p = [
43
52
  return this.predict(e, t);
44
53
  }
45
54
  async predict(r, i = {}) {
46
- let o = new e(), s = typeof r == "string" ? r : null, c = await a(r);
47
- o.stage("load");
48
- let l = this._preprocess(c);
49
- o.stage("preprocess");
55
+ let a = new e(), o = typeof r == "string" ? r : null, s = await c(r);
56
+ a.stage("load");
57
+ let l = this._preprocess(s);
58
+ a.stage("preprocess");
50
59
  let u = await this._session.run({ [this._session.inputName]: l });
51
- o.stage("inference");
52
- let f = this._session.outputNames[0];
53
- if (f === void 0) throw Error("Classifier model has no outputs.");
54
- let p = u[f];
55
- if (p === void 0) throw Error(`Classifier model output ${f} missing from run() result.`);
56
- let m = this._postprocess(p.data), { indices: h, values: g } = d(m, i.topK ?? null), _ = [];
60
+ a.stage("inference");
61
+ let d = this._session.outputNames[0];
62
+ if (d === void 0) throw Error("Classifier model has no outputs.");
63
+ let f = u[d];
64
+ if (f === void 0) throw Error(`Classifier model output ${d} missing from run() result.`);
65
+ let p = this._postprocess(f.data), { indices: h, values: g } = m(p, i.topK ?? null), _ = [];
57
66
  for (let e = 0; e < h.length; e++) {
58
67
  let t = h[e], n = this._labels[t] ?? `class_${t}`;
59
68
  _.push({
@@ -73,14 +82,14 @@ var p = [
73
82
  cls: v.classId,
74
83
  name: v.className,
75
84
  conf: v.probability,
76
- image: c,
85
+ image: s,
77
86
  probabilities: _
78
- }, b = [c.height, c.width], x = new n(m);
79
- return o.stage("postprocess"), [new t(x, y, this._names, c, b, s, o.speed())];
87
+ }, b = [s.height, s.width], x = new n(p);
88
+ return a.stage("postprocess"), [new t(x, y, this._names, s, b, o, a.speed())];
80
89
  }
81
90
  _preprocess(e) {
82
- let [t, n] = this._inputSize, r = s(e, t, n);
83
- return l(c(o(r, this._mean, this._std), r.width, r.height, 3), [
91
+ let [t, n] = this._inputSize, r = u(e, t, n);
92
+ return f(d(l(r, this._mean, this._std), r.width, r.height, 3), [
84
93
  1,
85
94
  3,
86
95
  r.height,
@@ -88,10 +97,10 @@ var p = [
88
97
  ]);
89
98
  }
90
99
  _postprocess(e) {
91
- return this._applySoftmax ? u(e) : new Float32Array(e);
100
+ return this._applySoftmax ? p(e) : new Float32Array(e);
92
101
  }
93
102
  };
94
103
  //#endregion
95
- export { h as Classifier };
104
+ export { v as Classifier };
96
105
 
97
106
  //# sourceMappingURL=classifier.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"classifier.js","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/**\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 { type LabelSpec, resolveLabels } from \"../labels\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { normalize, resize, toCHW, toFloat32Tensor } from \"../preprocess/image\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nconst IMAGENET_MEAN: readonly [number, number, number] = [0.485, 0.456, 0.406];\nconst IMAGENET_STD: readonly [number, number, number] = [0.229, 0.224, 0.225];\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /** Class label spec — see {@link resolveLabels}. */\n readonly labels: LabelSpec;\n /**\n * Number of classes the model can predict. Required when `labels` is `null`\n * or when you want to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /** Model input `[width, height]` in pixels. Defaults to `[224, 224]`. */\n readonly inputSize?: readonly [number, number];\n /** Per-channel RGB mean used for normalization. Defaults to ImageNet. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation. Defaults to ImageNet. */\n readonly std?: readonly [number, number, number];\n /**\n * If `true` (default), apply softmax to the raw model output. Set to\n * `false` for models whose final layer already produces a probability\n * distribution.\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 ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: ClassifierOptions): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels, { numClasses: options.numClasses });\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 options.inputSize ?? [224, 224],\n options.mean ?? IMAGENET_MEAN,\n options.std ?? IMAGENET_STD,\n options.applySoftmax ?? true,\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 /** 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 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 resized = resize(image, tw, th);\n const normalized = normalize(resized, this._mean, this._std);\n const chw = toCHW(normalized, resized.width, resized.height, 3);\n return toFloat32Tensor(chw, [1, 3, resized.height, resized.width]);\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;AAgBA,IAAM,IAAmD;CAAC;CAAO;CAAO;AAAK,GACvE,IAAkD;CAAC;CAAO;CAAO;AAAK,GAsD/D,IAAb,MAAa,UAAmB,EAAW;CAGlB;CACA;CACA;CACA;CACA;CACA;CAPrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GAPI,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,aAAA,GACA,KAAA,QAAA,GACA,KAAA,OAAA,GACA,KAAA,gBAAA;CAGrB;CAGA,aAAa,OAAO,GAAoB,GAAiD;EACrF,IAAM,IAAU,MAAM,EAAW,OAAO,GAAO,CAAO,GAChD,IAAS,EAAc,EAAQ,QAAQ,EAAE,YAAY,EAAQ,WAAW,CAAC,GACzE,IAAgC,CAAC;EACvC,KAAK,IAAI,IAAI,GAAG,IAAI,EAAO,QAAQ,KAC/B,EAAM,KAAK,EAAO;EAEtB,OAAO,IAAI,EACP,GACA,GACA,GACA,EAAQ,aAAa,CAAC,KAAK,GAAG,GAC9B,EAAQ,QAAQ,GA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EAAI;GACb,WAAW,EAAI;GACf,YAAY,EAAI;GAChB,KAAK,EAAI;GACT,MAAM,EAAI;GACV,MAAM,EAAI;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,IAAU,EAAO,GAAO,GAAI,CAAE;EAGpC,OAAO,EADK,EADO,EAAU,GAAS,KAAK,OAAO,KAAK,IACrC,GAAY,EAAQ,OAAO,EAAQ,QAAQ,CACtC,GAAK;GAAC;GAAG;GAAG,EAAQ;GAAQ,EAAQ;EAAK,CAAC;CACrE;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":["/**\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 { softmax, topK } from \"../postprocess/classification\";\nimport { normalize, resize, toCHW, toFloat32Tensor } from \"../preprocess/image\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nconst IMAGENET_MEAN: readonly [number, number, number] = [0.485, 0.456, 0.406];\nconst IMAGENET_STD: readonly [number, number, number] = [0.229, 0.224, 0.225];\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 /** Per-channel RGB mean used for normalization. Defaults to ImageNet. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation. Defaults to ImageNet. */\n readonly std?: readonly [number, number, number];\n /**\n * If `true` (default), apply softmax to the raw model output. Set to\n * `false` for models whose final layer already produces a probability\n * distribution.\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 ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\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 options.mean ?? IMAGENET_MEAN,\n options.std ?? IMAGENET_STD,\n options.applySoftmax ?? true,\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 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 resized = resize(image, tw, th);\n const normalized = normalize(resized, this._mean, this._std);\n const chw = toCHW(normalized, resized.width, resized.height, 3);\n return toFloat32Tensor(chw, [1, 3, resized.height, resized.width]);\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;;;AAkBA,IAAM,IAAmD;CAAC;CAAO;CAAO;AAAK,GACvE,IAAkD;CA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rC,GAAY,EAAQ,OAAO,EAAQ,QAAQ,CACtC,GAAK;GAAC;GAAG;GAAG,EAAQ;GAAQ,EAAQ;EAAK,CAAC;CACrE;CAEA,aAAqB,GAAiC;EAClD,OAAO,KAAK,gBAAgB,EAAQ,CAAG,IAAI,IAAI,aAAa,CAAG;CACnE;AACJ"}
@@ -1,2 +1,2 @@
1
- const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/session.cjs"),a=require("../io/image.cjs"),o=require("../preprocess/image.cjs"),s=require("../postprocess/detection.cjs"),c=require("./base.cjs");var l=class l extends c.VisionTask{_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;constructor(e,t,n,r,i,a,o,s){super(e),this._head=t,this._labels=n,this._names=r,this._inputSize=i,this._confThreshold=a,this._iouThreshold=o,this._maxDetections=s}static async create(e,t={}){let n=t.head??`yolo`;if(n!==`yolo`)throw Error(`Unsupported detector head '${n}'. Supported: 'yolo'.`);let a=await i.OrtSession.create(e,t),o=r.resolveLabels(t.labels??`coco`,{numClasses:t.numClasses}),s={};for(let e=0;e<o.length;e++)s[e]=o[e];return new l(a,n,o,s,t.inputSize??[640,640],t.confThreshold??.25,t.iouThreshold??.45,t.maxDetections??300)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(e,t={}){return this.predict(e,t)}async predict(e,r={}){let i=new t.SpeedTimer,o=typeof e==`string`?e:null,c=await a.loadImage(e);i.stage(`load`);let{tensor:l,scale:u,padLeft:d,padTop:f}=this._preprocess(c);i.stage(`preprocess`);let p=await this._session.run({[this._session.inputName]:l});i.stage(`inference`);let m=this._session.outputNames[0];if(m===void 0)throw Error(`Detector model has no outputs.`);let h=p[m];if(h===void 0)throw Error(`Detector model output ${m} missing from run() result.`);let g=s.decodeYolo(h.data,h.dims,{originalWidth:c.width,originalHeight:c.height,padLeft:d,padTop:f,scale:u,confThreshold:r.confThreshold??this._confThreshold,iouThreshold:r.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections}),_=(r.classes===void 0?g:(()=>{let e=new Set(r.classes);return g.filter(t=>e.has(t.classId))})()).map(e=>this._buildResult(c,e.bbox,e.classId,e.confidence)),v=[c.height,c.width],y=this._buildBoxes(_,v);return i.stage(`postprocess`),[new n.DetectionResults(y,_,this._names,c,v,o,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,r=o.letterbox(e,t,n);return{tensor:o.toFloat32Tensor(o.toCHW(o.toFloat32(r.image),r.image.width,r.image.height,3),[1,3,r.image.height,r.image.width]),scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_buildResult(t,n,r,i){let[a,o,s,c]=n.asIntXyxy(),l=Math.max(0,a),u=Math.max(0,o),d=Math.min(t.width,s),f=Math.min(t.height,c),p;if(d>l&&f>u){let n=d-l,r=f-u,i=new Uint8Array(n*r*3);for(let e=0;e<r;e++){let r=((u+e)*t.width+l)*3;i.set(t.data.subarray(r,r+n*3),e*n*3)}p=new e.RGBImage(i,n,r)}else p=new e.RGBImage(new Uint8Array,0,0);let m=this._names[r]??`class_${r}`;return{classId:r,className:m,confidence:i,bbox:n,cls:r,name:m,conf:i,box:n,croppedImage:p}}_buildBoxes(e,t){let r=e.length,i=new Float32Array(r*4),a=new Int32Array(r),o=new Float32Array(r);for(let t=0;t<r;t++){let n=e[t];i[t*4]=n.bbox.x1,i[t*4+1]=n.bbox.y1,i[t*4+2]=n.bbox.x2,i[t*4+3]=n.bbox.y2,a[t]=n.classId,o[t]=n.confidence}return new n.Boxes(i,a,o,t)}};exports.Detector=l;
1
+ const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/graph.cjs"),a=require("../core/metadata.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../preprocess/image.cjs"),l=require("../postprocess/detection.cjs"),u=require("./base.cjs");var d=class d extends u.VisionTask{_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;constructor(e,t,n,r,i,a,o,s){super(e),this._head=t,this._labels=n,this._names=r,this._inputSize=i,this._confThreshold=a,this._iouThreshold=o,this._maxDetections=s}static async create(e,t={}){let n=t.head??`yolo`;if(n!==`yolo`)throw Error(`Unsupported detector head '${n}'. Supported: 'yolo'.`);let s=await o.OrtSession.create(e,t),c=r.resolveLabels(t.labels??a.modelNames(s.metadata)??`coco`,{numClasses:t.numClasses??i.detectionNumClasses(s.outputShape)??void 0}),l={};for(let e=0;e<c.length;e++)l[e]=c[e];return new d(s,n,c,l,i.resolveInputSize({graphShape:s.inputShape,requested:t.inputSize,fallback:[640,640]}),t.confThreshold??.25,t.iouThreshold??.45,t.maxDetections??300)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get inputSize(){return this._inputSize}get numClasses(){return this._labels.length}async call(e,t={}){return this.predict(e,t)}async predict(e,r={}){let i=new t.SpeedTimer,a=typeof e==`string`?e:null,o=await s.loadImage(e);i.stage(`load`);let{tensor:c,scale:u,padLeft:d,padTop:f}=this._preprocess(o);i.stage(`preprocess`);let p=await this._session.run({[this._session.inputName]:c});i.stage(`inference`);let m=this._session.outputNames[0];if(m===void 0)throw Error(`Detector model has no outputs.`);let h=p[m];if(h===void 0)throw Error(`Detector model output ${m} missing from run() result.`);let g=l.decodeYolo(h.data,h.dims,{originalWidth:o.width,originalHeight:o.height,padLeft:d,padTop:f,scale:u,confThreshold:r.confThreshold??this._confThreshold,iouThreshold:r.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections}),_=(r.classes===void 0?g:(()=>{let e=new Set(r.classes);return g.filter(t=>e.has(t.classId))})()).map(e=>this._buildResult(o,e.bbox,e.classId,e.confidence)),v=[o.height,o.width],y=this._buildBoxes(_,v);return i.stage(`postprocess`),[new n.DetectionResults(y,_,this._names,o,v,a,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,r=c.letterbox(e,t,n);return{tensor:c.toFloat32Tensor(c.toCHW(c.toFloat32(r.image),r.image.width,r.image.height,3),[1,3,r.image.height,r.image.width]),scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_buildResult(t,n,r,i){let[a,o,s,c]=n.asIntXyxy(),l=Math.max(0,a),u=Math.max(0,o),d=Math.min(t.width,s),f=Math.min(t.height,c),p;if(d>l&&f>u){let n=d-l,r=f-u,i=new Uint8Array(n*r*3);for(let e=0;e<r;e++){let r=((u+e)*t.width+l)*3;i.set(t.data.subarray(r,r+n*3),e*n*3)}p=new e.RGBImage(i,n,r)}else p=new e.RGBImage(new Uint8Array,0,0);let m=this._names[r]??`class_${r}`;return{classId:r,className:m,confidence:i,bbox:n,cls:r,name:m,conf:i,box:n,croppedImage:p}}_buildBoxes(e,t){let r=e.length,i=new Float32Array(r*4),a=new Int32Array(r),o=new Float32Array(r);for(let t=0;t<r;t++){let n=e[t];i[t*4]=n.bbox.x1,i[t*4+1]=n.bbox.y1,i[t*4+2]=n.bbox.x2,i[t*4+3]=n.bbox.y2,a[t]=n.classId,o[t]=n.confidence}return new n.Boxes(i,a,o,t)}};exports.Detector=d;
2
2
  //# sourceMappingURL=detector.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"detector.cjs","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\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 { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /** Model input `[width, height]` for letterboxing. Defaults to `[640, 640]`. */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? \"coco\", {\n numClasses: options.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 Detector(\n session,\n head,\n labels,\n names,\n options.inputSize ?? [640, 640],\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\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 /** Number of classes the model predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":"4RA+EA,IAAa,EAAb,MAAa,UAAiB,EAAA,UAAW,CAGhB,MACA,QACA,OACA,WACA,eACA,cACA,eARrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EARI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,eAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,CAGrB,CAGA,aAAa,OAAO,EAAoB,EAA2B,CAAC,EAAsB,CACtF,IAAM,EAAqB,EAAQ,MAAQ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C,EAAM,KAAK,IAAI,EAAS,OAAQ,CAAE,EAEpC,EACJ,GAAI,EAAM,GAAO,EAAM,EAAK,CACxB,IAAM,EAAK,EAAM,EACX,EAAK,EAAM,EACX,EAAM,IAAI,WAAW,EAAK,EAAK,CAAC,EACtC,IAAK,IAAI,EAAM,EAAG,EAAM,EAAI,IAAO,CAC/B,IAAM,IAAc,EAAM,GAAO,EAAS,MAAQ,GAAO,EACzD,EAAI,IAAI,EAAS,KAAK,SAAS,EAAW,EAAY,EAAK,CAAC,EAAG,EAAM,EAAK,CAAC,CAC/E,CACA,EAAU,IAAI,EAAA,SAAS,EAAK,EAAI,CAAE,CACtC,KACI,GAAU,IAAI,EAAA,SAAS,IAAI,WAAe,EAAG,CAAC,EAGlD,IAAM,EAAY,KAAK,OAAO,IAAY,SAAS,IAEnD,MAAO,CACH,UACA,YACA,aACA,OACA,IAAK,EACL,KAAM,EACN,KAAM,EACN,IAAK,EACL,aAAc,CAClB,CACJ,CAEA,YACI,EACA,EACK,CACL,IAAM,EAAI,EAAW,OACf,EAAO,IAAI,aAAa,EAAI,CAAC,EAC7B,EAAM,IAAI,WAAW,CAAC,EACtB,EAAO,IAAI,aAAa,CAAC,EAC/B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,EAAW,GACrB,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAI,GAAK,EAAE,QACX,EAAK,GAAK,EAAE,UAChB,CACA,OAAO,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C,CACJ"}
1
+ {"version":3,"file":"detector.cjs","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\n\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { detectionNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels for letterboxing.\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 `[640, 640]`.\n */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata) ?? \"coco\", {\n numClasses: options.numClasses ?? detectionNumClasses(session.outputShape) ?? undefined,\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 Detector(\n session,\n head,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [640, 640],\n }),\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\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 predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":"6VAwFA,IAAa,EAAb,MAAa,UAAiB,EAAA,UAAW,CAGhB,MACA,QACA,OACA,WACA,eACA,cACA,eARrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EARI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,eAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,CAGrB,CAGA,aAAa,OAAO,EAAoB,EAA2B,CAAC,EAAsB,CACtF,IAAM,EAAqB,EAAQ,MAAQ,OAC3C,GAAI,IAAS,OACT,MAAU,MAAM,8BAA8B,EAAK,sBAAsB,EAE7E,IAAM,EAAU,MAAM,EAAA,WAAW,OAAO,EAAO,CAAO,EAChD,EAAS,EAAA,cAAc,EAAQ,QAAU,EAAA,WAAW,EAAQ,QAAQ,GAAK,OAAQ,CACnF,WAAY,EAAQ,YAAc,EAAA,oBAAoB,EAAQ,WAAW,GAAK,IAAA,EAClF,CAAC,EACK,EAAgC,CAAC,EACvC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,OAAQ,IAC/B,EAAM,GAAK,EAAO,GAEtB,OAAO,IAAI,EACP,EACA,EACA,EACA,EACA,EAAA,iBAAiB,CACb,WAAY,EAAQ,WACpB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,EACzD,EAAI,IAAI,EAAS,KAAK,SAAS,EAAW,EAAY,EAAK,CAAC,EAAG,EAAM,EAAK,CAAC,CAC/E,CACA,EAAU,IAAI,EAAA,SAAS,EAAK,EAAI,CAAE,CACtC,KACI,GAAU,IAAI,EAAA,SAAS,IAAI,WAAe,EAAG,CAAC,EAGlD,IAAM,EAAY,KAAK,OAAO,IAAY,SAAS,IAEnD,MAAO,CACH,UACA,YACA,aACA,OACA,IAAK,EACL,KAAM,EACN,KAAM,EACN,IAAK,EACL,aAAc,CAClB,CACJ,CAEA,YACI,EACA,EACK,CACL,IAAM,EAAI,EAAW,OACf,EAAO,IAAI,aAAa,EAAI,CAAC,EAC7B,EAAM,IAAI,WAAW,CAAC,EACtB,EAAO,IAAI,aAAa,CAAC,EAC/B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,EAAW,GACrB,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAI,GAAK,EAAE,QACX,EAAK,GAAK,EAAE,UAChB,CACA,OAAO,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C,CACJ"}
@@ -2,13 +2,15 @@ import { RGBImage as e } from "../types.js";
2
2
  import { SpeedTimer as t } from "../core/timing.js";
3
3
  import { Boxes as n, DetectionResults as r } from "../results.js";
4
4
  import { resolveLabels as i } from "../labels.js";
5
- import { OrtSession as a } from "../core/session.js";
6
- import { loadImage as o } from "../io/image.js";
7
- import { letterbox as s, toCHW as c, toFloat32 as l, toFloat32Tensor as u } from "../preprocess/image.js";
8
- import { decodeYolo as d } from "../postprocess/detection.js";
9
- import { VisionTask as f } from "./base.js";
5
+ import { detectionNumClasses 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 { loadImage as l } from "../io/image.js";
9
+ import { letterbox as u, toCHW as d, toFloat32 as f, toFloat32Tensor as p } from "../preprocess/image.js";
10
+ import { decodeYolo as m } from "../postprocess/detection.js";
11
+ import { VisionTask as h } from "./base.js";
10
12
  //#region src/vision/tasks/detector.ts
11
- var p = class p extends f {
13
+ var g = class g extends h {
12
14
  _head;
13
15
  _labels;
14
16
  _names;
@@ -22,9 +24,13 @@ var p = class p extends f {
22
24
  static async create(e, t = {}) {
23
25
  let n = t.head ?? "yolo";
24
26
  if (n !== "yolo") throw Error(`Unsupported detector head '${n}'. Supported: 'yolo'.`);
25
- let r = await a.create(e, t), o = i(t.labels ?? "coco", { numClasses: t.numClasses }), s = {};
26
- for (let e = 0; e < o.length; e++) s[e] = o[e];
27
- return new p(r, n, o, s, t.inputSize ?? [640, 640], t.confThreshold ?? .25, t.iouThreshold ?? .45, t.maxDetections ?? 300);
27
+ let r = await c.create(e, t), l = i(t.labels ?? s(r.metadata) ?? "coco", { numClasses: t.numClasses ?? a(r.outputShape) ?? void 0 }), u = {};
28
+ for (let e = 0; e < l.length; e++) u[e] = l[e];
29
+ return new g(r, n, l, u, o({
30
+ graphShape: r.inputShape,
31
+ requested: t.inputSize,
32
+ fallback: [640, 640]
33
+ }), t.confThreshold ?? .25, t.iouThreshold ?? .45, t.maxDetections ?? 300);
28
34
  }
29
35
  get head() {
30
36
  return this._head;
@@ -35,6 +41,9 @@ var p = class p extends f {
35
41
  get names() {
36
42
  return this._names;
37
43
  }
44
+ get inputSize() {
45
+ return this._inputSize;
46
+ }
38
47
  get numClasses() {
39
48
  return this._labels.length;
40
49
  }
@@ -42,35 +51,35 @@ var p = class p extends f {
42
51
  return this.predict(e, t);
43
52
  }
44
53
  async predict(e, n = {}) {
45
- let i = new t(), a = typeof e == "string" ? e : null, s = await o(e);
54
+ let i = new t(), a = typeof e == "string" ? e : null, o = await l(e);
46
55
  i.stage("load");
47
- let { tensor: c, scale: l, padLeft: u, padTop: f } = this._preprocess(s);
56
+ let { tensor: s, scale: c, padLeft: u, padTop: d } = this._preprocess(o);
48
57
  i.stage("preprocess");
49
- let p = await this._session.run({ [this._session.inputName]: c });
58
+ let f = await this._session.run({ [this._session.inputName]: s });
50
59
  i.stage("inference");
51
- let m = this._session.outputNames[0];
52
- if (m === void 0) throw Error("Detector model has no outputs.");
53
- let h = p[m];
54
- if (h === void 0) throw Error(`Detector model output ${m} missing from run() result.`);
55
- let g = d(h.data, h.dims, {
56
- originalWidth: s.width,
57
- originalHeight: s.height,
60
+ let p = this._session.outputNames[0];
61
+ if (p === void 0) throw Error("Detector model has no outputs.");
62
+ let h = f[p];
63
+ if (h === void 0) throw Error(`Detector model output ${p} missing from run() result.`);
64
+ let g = m(h.data, h.dims, {
65
+ originalWidth: o.width,
66
+ originalHeight: o.height,
58
67
  padLeft: u,
59
- padTop: f,
60
- scale: l,
68
+ padTop: d,
69
+ scale: c,
61
70
  confThreshold: n.confThreshold ?? this._confThreshold,
62
71
  iouThreshold: n.iouThreshold ?? this._iouThreshold,
63
72
  maxDetections: this._maxDetections
64
73
  }), _ = (n.classes === void 0 ? g : (() => {
65
74
  let e = new Set(n.classes);
66
75
  return g.filter((t) => e.has(t.classId));
67
- })()).map((e) => this._buildResult(s, e.bbox, e.classId, e.confidence)), v = [s.height, s.width], y = this._buildBoxes(_, v);
68
- return i.stage("postprocess"), [new r(y, _, this._names, s, v, a, i.speed())];
76
+ })()).map((e) => this._buildResult(o, e.bbox, e.classId, e.confidence)), v = [o.height, o.width], y = this._buildBoxes(_, v);
77
+ return i.stage("postprocess"), [new r(y, _, this._names, o, v, a, i.speed())];
69
78
  }
70
79
  _preprocess(e) {
71
- let [t, n] = this._inputSize, r = s(e, t, n);
80
+ let [t, n] = this._inputSize, r = u(e, t, n);
72
81
  return {
73
- tensor: u(c(l(r.image), r.image.width, r.image.height, 3), [
82
+ tensor: p(d(f(r.image), r.image.width, r.image.height, 3), [
74
83
  1,
75
84
  3,
76
85
  r.image.height,
@@ -114,6 +123,6 @@ var p = class p extends f {
114
123
  }
115
124
  };
116
125
  //#endregion
117
- export { p as Detector };
126
+ export { g as Detector };
118
127
 
119
128
  //# sourceMappingURL=detector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"detector.js","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\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 { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /** Model input `[width, height]` for letterboxing. Defaults to `[640, 640]`. */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? \"coco\", {\n numClasses: options.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 Detector(\n session,\n head,\n labels,\n names,\n options.inputSize ?? [640, 640],\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\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 /** Number of classes the model predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":";;;;;;;;;;AA+EA,IAAa,IAAb,MAAa,UAAiB,EAAW;CAGhB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GARI,KAAA,QAAA,GACA,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,aAAA,GACA,KAAA,iBAAA,GACA,KAAA,gBAAA,GACA,KAAA,iBAAA;CAGrB;CAGA,aAAa,OAAO,GAAoB,IAA2B,CAAC,GAAsB;EACtF,IAAM,IAAqB,EAAQ,QAAQ;EAC3C,IAAI,MAAS,QACT,MAAU,MAAM,8BAA8B,EAAK,sBAAsB;EAE7E,IAAM,IAAU,MAAM,EAAW,OAAO,GAAO,CAAO,GAChD,IAAS,EAAc,EAAQ,UAAU,QAAQ,EACnD,YAAY,EAAQ,WACxB,CAAC,GACK,IAAgC,CAAC;EACvC,KAAK,IAAI,IAAI,GAAG,IAAI,EAAO,QAAQ,KAC/B,EAAM,KAAK,EAAO;EAEtB,OAAO,IAAI,EACP,GACA,GACA,GACA,GACA,EAAQ,aAAa,CAAC,KAAK,GAAG,GAC9B,EAAQ,iBAAiB,KACzB,EAAQ,gBAAgB,KACxB,EAAQ,iBAAiB,GAC7B;CACJ;CAGA,IAAI,OAAqB;EACrB,OAAO,KAAK;CAChB;CAGA,IAAI,SAA4B;EAC5B,OAAO,KAAK;CAChB;CAGA,IAAI,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GACL,cAAc;EAClB;CACJ;CAEA,YACI,GACA,GACK;EACL,IAAM,IAAI,EAAW,QACf,IAAO,IAAI,aAAa,IAAI,CAAC,GAC7B,IAAM,IAAI,WAAW,CAAC,GACtB,IAAO,IAAI,aAAa,CAAC;EAC/B,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;GACxB,IAAM,IAAI,EAAW;GAMrB,AALA,EAAK,IAAI,KAAK,EAAE,KAAK,IACrB,EAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IACzB,EAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IACzB,EAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IACzB,EAAI,KAAK,EAAE,SACX,EAAK,KAAK,EAAE;EAChB;EACA,OAAO,IAAI,EAAM,GAAM,GAAK,GAAM,CAAS;CAC/C;AACJ"}
1
+ {"version":3,"file":"detector.js","names":[],"sources":["../../../src/vision/tasks/detector.ts"],"sourcesContent":["/**\n * Object detection task using anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\n\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { detectionNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYolo } from \"../postprocess/detection\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, DetectionResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type DetectionResult, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the model's detection head.\n *\n * - `\"yolo\"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —\n * covers YOLOv8, v9, v10, v11, v12, v26 detect exports.\n *\n * The SDK does **not** auto-detect the head from the model — the caller is\n * responsible for picking a head that matches their export. Future families\n * (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.\n */\nexport type DetectorHead = \"yolo\";\n\nexport interface DetectorOptions extends OrtSessionOptions {\n /**\n * Decoder family for the detection head. Default `\"yolo\"` covers\n * YOLOv8/v9/v10/v11/v12/v26.\n */\n readonly head?: DetectorHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels for letterboxing.\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 `[640, 640]`.\n */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of detections per image. */\n readonly maxDetections?: number;\n}\n\nexport interface DetectorPredictOptions {\n /** Override the default confidence threshold. */\n readonly confThreshold?: number;\n /** Override the default IoU threshold. */\n readonly iouThreshold?: number;\n /**\n * If set, keep only detections whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).\n *\n * `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single\n * image), mirroring Ultralytics' `YOLO(\"img.jpg\")`. Iterate the envelope for\n * per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,\n * `.xyxyn`, `.xywhn`, `.cls`, `.conf`).\n *\n * @example\n * ```typescript\n * const det = await Detector.create(\"/models/yolov8n.onnx\");\n * const results = await det.predict(\"/images/street.jpg\");\n * const r = results[0];\n * console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);\n * for (const d of r) {\n * console.log(d.cls, d.conf, d.box.xyxy);\n * }\n * ```\n */\nexport class Detector extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: DetectorHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: DetectorOptions = {}): Promise<Detector> {\n const head: DetectorHead = options.head ?? \"yolo\";\n if (head !== \"yolo\") {\n throw new Error(`Unsupported detector head '${head}'. Supported: 'yolo'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata) ?? \"coco\", {\n numClasses: options.numClasses ?? detectionNumClasses(session.outputShape) ?? undefined,\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 Detector(\n session,\n head,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [640, 640],\n }),\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): DetectorHead {\n return this._head;\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 predicts. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /**\n * Alias for {@link predict} — call the detector like a torch `nn.Module`.\n *\n * Use as `det.call(img)` since JavaScript class instances are not callable;\n * for direct invocation, prefer `det.predict(img)`. The full\n * {@link DetectorPredictOptions} (including `classes`) is supported.\n */\n async call(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\n return this.predict(image, options);\n }\n\n /**\n * Run detection on a single image.\n *\n * The returned envelope carries a {@link Speed} breakdown in `speed`,\n * mirroring Ultralytics' `results[0].speed`.\n */\n async predict(\n image: ImageInput,\n options: DetectorPredictOptions = {},\n ): Promise<DetectionResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Detector model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(`Detector model output ${firstOutputName} missing from run() result.`);\n }\n\n const decodedAll = decodeYolo(raw.data as Float32Array, raw.dims, {\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n });\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new DetectionResults(\n boxes,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n ): DetectionResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let cropped: RGBImage;\n if (cx2 > cx1 && cy2 > cy1) {\n const cw = cx2 - cx1;\n const ch = cy2 - cy1;\n const out = new Uint8Array(cw * ch * 3);\n for (let row = 0; row < ch; row++) {\n const srcOffset = ((cy1 + row) * original.width + cx1) * 3;\n out.set(original.data.subarray(srcOffset, srcOffset + cw * 3), row * cw * 3);\n }\n cropped = new RGBImage(out, cw, ch);\n } else {\n cropped = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n croppedImage: cropped,\n };\n }\n\n private _buildBoxes(\n detections: readonly DetectionResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as DetectionResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n}\n"],"mappings":";;;;;;;;;;;;AAwFA,IAAa,IAAb,MAAa,UAAiB,EAAW;CAGhB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,A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ACzB,EAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IACzB,EAAK,IAAI,IAAI,KAAK,EAAE,KAAK,IACzB,EAAI,KAAK,EAAE,SACX,EAAK,KAAK,EAAE;EAChB;EACA,OAAO,IAAI,EAAM,GAAM,GAAK,GAAM,CAAS;CAC/C;AACJ"}
@@ -1,2 +1,2 @@
1
- const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/session.cjs"),a=require("../io/image.cjs"),o=require("../preprocess/image.cjs"),s=require("../postprocess/segmentation.cjs"),c=require("./base.cjs");var l=class l extends c.VisionTask{_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;_maskThreshold;constructor(e,t,n,r,i,a,o,s,c){super(e),this._head=t,this._labels=n,this._names=r,this._inputSize=i,this._confThreshold=a,this._iouThreshold=o,this._maxDetections=s,this._maskThreshold=c}static async create(e,t={}){let n=t.head??`yolo-seg`;if(n!==`yolo-seg`)throw Error(`Unsupported segmenter head '${n}'. Supported: 'yolo-seg'.`);let a=await i.OrtSession.create(e,t),o=r.resolveLabels(t.labels??`coco`,{numClasses:t.numClasses}),s={};for(let e=0;e<o.length;e++)s[e]=o[e];return new l(a,n,o,s,t.inputSize??[640,640],t.confThreshold??.25,t.iouThreshold??.45,t.maxDetections??300,t.maskThreshold??.5)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(e,t={}){return this.predict(e,t)}async predict(e,r={}){let i=new t.SpeedTimer,o=typeof e==`string`?e:null,c=await a.loadImage(e);i.stage(`load`);let{tensor:l,scale:u,padLeft:d,padTop:f}=this._preprocess(c);i.stage(`preprocess`);let p=await this._session.run({[this._session.inputName]:l});i.stage(`inference`);let{perAnchor:m,prototypes:h}=this._splitOutputs(p),g=s.decodeYoloSeg(m.data,m.dims,h.data,h.dims,{numClasses:this._labels.length,inputWidth:this._inputSize[0],inputHeight:this._inputSize[1],originalWidth:c.width,originalHeight:c.height,padLeft:d,padTop:f,scale:u,confThreshold:r.confThreshold??this._confThreshold,iouThreshold:r.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections,maskThreshold:this._maskThreshold}),_=(r.classes===void 0?g:(()=>{let e=new Set(r.classes);return g.filter(t=>e.has(t.classId))})()).map(e=>this._buildResult(c,e.bbox,e.classId,e.confidence,e.mask)),v=[c.height,c.width],y=this._buildBoxes(_,v),b=this._buildMasks(_,v);return i.stage(`postprocess`),[new n.SegmentationResults(y,b,_,this._names,c,v,o,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,r=o.letterbox(e,t,n);return{tensor:o.toFloat32Tensor(o.toCHW(o.toFloat32(r.image),r.image.width,r.image.height,3),[1,3,r.image.height,r.image.width]),scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_splitOutputs(e){let t,n;for(let r of this._session.outputNames){let i=e[r];i!==void 0&&(i.dims.length===3&&t===void 0?t=i:i.dims.length===4&&n===void 0&&(n=i))}if(t===void 0||n===void 0){let t=this._session.outputNames.map(t=>`${t}: ${JSON.stringify(e[t]?.dims??[])}`);throw Error(`Segmenter expected one 3-D and one 4-D output, got [${t.join(`, `)}].`)}return{perAnchor:t,prototypes:n}}_buildResult(t,n,r,i,a){let[o,s,c,l]=n.asIntXyxy(),u=Math.max(0,o),d=Math.max(0,s),f=Math.min(t.width,c),p=Math.min(t.height,l),m,h=a;if(f>u&&p>d&&a.data.length>0){let n=f-u,r=p-d,i=Math.min(a.width,n),o=Math.min(a.height,r),s=new Uint8Array(i*o*3);for(let e=0;e<o;e++){let n=((d+e)*t.width+u)*3,r=e*i*3,o=e*a.width;for(let e=0;e<i;e++)if(a.data[o+e]!==0){let i=n+e*3,a=r+e*3;s[a]=t.data[i],s[a+1]=t.data[i+1],s[a+2]=t.data[i+2]}}if(m=new e.RGBImage(s,i,o),i!==a.width||o!==a.height){let t=new Uint8Array(i*o);for(let e=0;e<o;e++)t.set(a.data.subarray(e*a.width,e*a.width+i),e*i);h=new e.Mask(t,i,o)}}else h=new e.Mask(new Uint8Array,0,0),m=new e.RGBImage(new Uint8Array,0,0);let g=this._names[r]??`class_${r}`;return{classId:r,className:g,confidence:i,bbox:n,cls:r,name:g,conf:i,box:n,mask:h,segmentedImage:m}}_buildBoxes(e,t){let r=e.length,i=new Float32Array(r*4),a=new Int32Array(r),o=new Float32Array(r);for(let t=0;t<r;t++){let n=e[t];i[t*4]=n.bbox.x1,i[t*4+1]=n.bbox.y1,i[t*4+2]=n.bbox.x2,i[t*4+3]=n.bbox.y2,a[t]=n.classId,o[t]=n.confidence}return new n.Boxes(i,a,o,t)}_buildMasks(e,t){let r=new Float32Array(e.length*4);for(let t=0;t<e.length;t++){let n=e[t];r[t*4]=n.bbox.x1,r[t*4+1]=n.bbox.y1,r[t*4+2]=n.bbox.x2,r[t*4+3]=n.bbox.y2}return new n.Masks(e.map(e=>e.mask),r,t)}};exports.Segmenter=l;
1
+ const e=require("../types.cjs"),t=require("../core/timing.cjs"),n=require("../results.cjs"),r=require("../labels.cjs"),i=require("../core/graph.cjs"),a=require("../core/metadata.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../preprocess/image.cjs"),l=require("../postprocess/segmentation.cjs"),u=require("./base.cjs");var d=class d extends u.VisionTask{_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;_maskThreshold;constructor(e,t,n,r,i,a,o,s,c){super(e),this._head=t,this._labels=n,this._names=r,this._inputSize=i,this._confThreshold=a,this._iouThreshold=o,this._maxDetections=s,this._maskThreshold=c}static async create(e,t={}){let n=t.head??`yolo-seg`;if(n!==`yolo-seg`)throw Error(`Unsupported segmenter head '${n}'. Supported: 'yolo-seg'.`);let s=await o.OrtSession.create(e,t),c=r.resolveLabels(t.labels??a.modelNames(s.metadata)??`coco`,{numClasses:t.numClasses??i.detectionNumClasses(s.outputShape)??void 0}),l={};for(let e=0;e<c.length;e++)l[e]=c[e];return new d(s,n,c,l,i.resolveInputSize({graphShape:s.inputShape,requested:t.inputSize,fallback:[640,640]}),t.confThreshold??.25,t.iouThreshold??.45,t.maxDetections??300,t.maskThreshold??.5)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get inputSize(){return this._inputSize}get numClasses(){return this._labels.length}async call(e,t={}){return this.predict(e,t)}async predict(e,r={}){let i=new t.SpeedTimer,a=typeof e==`string`?e:null,o=await s.loadImage(e);i.stage(`load`);let{tensor:c,scale:u,padLeft:d,padTop:f}=this._preprocess(o);i.stage(`preprocess`);let p=await this._session.run({[this._session.inputName]:c});i.stage(`inference`);let{perAnchor:m,prototypes:h}=this._splitOutputs(p),g=l.decodeYoloSeg(m.data,m.dims,h.data,h.dims,{numClasses:this._labels.length,inputWidth:this._inputSize[0],inputHeight:this._inputSize[1],originalWidth:o.width,originalHeight:o.height,padLeft:d,padTop:f,scale:u,confThreshold:r.confThreshold??this._confThreshold,iouThreshold:r.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections,maskThreshold:this._maskThreshold}),_=(r.classes===void 0?g:(()=>{let e=new Set(r.classes);return g.filter(t=>e.has(t.classId))})()).map(e=>this._buildResult(o,e.bbox,e.classId,e.confidence,e.mask)),v=[o.height,o.width],y=this._buildBoxes(_,v),b=this._buildMasks(_,v);return i.stage(`postprocess`),[new n.SegmentationResults(y,b,_,this._names,o,v,a,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,r=c.letterbox(e,t,n);return{tensor:c.toFloat32Tensor(c.toCHW(c.toFloat32(r.image),r.image.width,r.image.height,3),[1,3,r.image.height,r.image.width]),scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_splitOutputs(e){let t,n;for(let r of this._session.outputNames){let i=e[r];i!==void 0&&(i.dims.length===3&&t===void 0?t=i:i.dims.length===4&&n===void 0&&(n=i))}if(t===void 0||n===void 0){let t=this._session.outputNames.map(t=>`${t}: ${JSON.stringify(e[t]?.dims??[])}`);throw Error(`Segmenter expected one 3-D and one 4-D output, got [${t.join(`, `)}].`)}return{perAnchor:t,prototypes:n}}_buildResult(t,n,r,i,a){let[o,s,c,l]=n.asIntXyxy(),u=Math.max(0,o),d=Math.max(0,s),f=Math.min(t.width,c),p=Math.min(t.height,l),m,h=a;if(f>u&&p>d&&a.data.length>0){let n=f-u,r=p-d,i=Math.min(a.width,n),o=Math.min(a.height,r),s=new Uint8Array(i*o*3);for(let e=0;e<o;e++){let n=((d+e)*t.width+u)*3,r=e*i*3,o=e*a.width;for(let e=0;e<i;e++)if(a.data[o+e]!==0){let i=n+e*3,a=r+e*3;s[a]=t.data[i],s[a+1]=t.data[i+1],s[a+2]=t.data[i+2]}}if(m=new e.RGBImage(s,i,o),i!==a.width||o!==a.height){let t=new Uint8Array(i*o);for(let e=0;e<o;e++)t.set(a.data.subarray(e*a.width,e*a.width+i),e*i);h=new e.Mask(t,i,o)}}else h=new e.Mask(new Uint8Array,0,0),m=new e.RGBImage(new Uint8Array,0,0);let g=this._names[r]??`class_${r}`;return{classId:r,className:g,confidence:i,bbox:n,cls:r,name:g,conf:i,box:n,mask:h,segmentedImage:m}}_buildBoxes(e,t){let r=e.length,i=new Float32Array(r*4),a=new Int32Array(r),o=new Float32Array(r);for(let t=0;t<r;t++){let n=e[t];i[t*4]=n.bbox.x1,i[t*4+1]=n.bbox.y1,i[t*4+2]=n.bbox.x2,i[t*4+3]=n.bbox.y2,a[t]=n.classId,o[t]=n.confidence}return new n.Boxes(i,a,o,t)}_buildMasks(e,t){let r=new Float32Array(e.length*4);for(let t=0;t<e.length;t++){let n=e[t];r[t*4]=n.bbox.x1,r[t*4+1]=n.bbox.y1,r[t*4+2]=n.bbox.x2,r[t*4+3]=n.bbox.y2}return new n.Masks(e.map(e=>e.mask),r,t)}};exports.Segmenter=d;
2
2
  //# sourceMappingURL=segmenter.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"segmenter.cjs","names":[],"sources":["../../../src/vision/tasks/segmenter.ts"],"sourcesContent":["/**\n * Instance-segmentation task using YOLO seg ONNX models (v8-seg / v11-seg / ...).\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 { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYoloSeg } from \"../postprocess/segmentation\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, Masks, SegmentationResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type SegmentationResult, Mask, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the segmentation head.\n *\n * - `\"yolo-seg\"`: YOLO instance-segmentation head with two outputs —\n * `[1, 4 + nc + nm, N]` per-anchor predictions plus `[1, nm, mh, mw]`\n * prototype masks. Covers YOLOv8-seg, v11-seg, v26-seg.\n *\n * The SDK does **not** auto-detect this — the caller is responsible for\n * picking a head that matches their export.\n */\nexport type SegmenterHead = \"yolo-seg\";\n\nexport interface SegmenterOptions extends OrtSessionOptions {\n /**\n * Decoder family for the segmentation head. Default `\"yolo-seg\"` covers\n * YOLOv8-seg/v11-seg/v26-seg.\n */\n readonly head?: SegmenterHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /** Model input `[width, height]` for letterboxing. Defaults to `[640, 640]`. */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of instances per image. */\n readonly maxDetections?: number;\n /** Probability cutoff applied to soft masks. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface SegmenterPredictOptions {\n readonly confThreshold?: number;\n readonly iouThreshold?: number;\n /**\n * If set, keep only instances whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Instance segmenter for YOLO seg ONNX models (v8-seg / v11-seg / ...).\n *\n * The model is expected to expose two outputs:\n *\n * 1. `output0`: `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — per-anchor\n * predictions (boxes, class scores, mask coefficients).\n * 2. `output1`: `(1, numMaskCoefs, maskH, maskW)` — prototype masks.\n *\n * `predict()` returns `Promise<SegmentationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes:\n *\n * - `boxes`: bulk numpy view (`xyxy`, `xywh`, `xyxyn`, `xywhn`, `cls`, `conf`).\n * - `masks`: per-instance binary masks cropped to each box.\n * - per-instance {@link SegmentationResult} via iteration.\n *\n * @example\n * ```typescript\n * const seg = await Segmenter.create(\"/models/yolov8n-seg.onnx\");\n * const r = (await seg.predict(\"/images/street.jpg\"))[0];\n * for (const inst of r) {\n * console.log(inst.cls, inst.conf, inst.box.xyxy);\n * }\n * ```\n */\nexport class Segmenter extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: SegmenterHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n private readonly _maskThreshold: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: SegmenterOptions = {}): Promise<Segmenter> {\n const head: SegmenterHead = options.head ?? \"yolo-seg\";\n if (head !== \"yolo-seg\") {\n throw new Error(`Unsupported segmenter head '${head}'. Supported: 'yolo-seg'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? \"coco\", {\n numClasses: options.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 Segmenter(\n session,\n head,\n labels,\n names,\n options.inputSize ?? [640, 640],\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n options.maskThreshold ?? 0.5,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): SegmenterHead {\n return this._head;\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 /** Number of classes the model predicts. */\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: SegmenterPredictOptions = {},\n ): Promise<SegmentationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run instance segmentation on a single image. */\n async predict(\n image: ImageInput,\n options: SegmenterPredictOptions = {},\n ): Promise<SegmentationResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const { perAnchor, prototypes } = this._splitOutputs(outputs);\n\n const decodedAll = decodeYoloSeg(\n perAnchor.data as Float32Array,\n perAnchor.dims,\n prototypes.data as Float32Array,\n prototypes.dims,\n {\n numClasses: this._labels.length,\n inputWidth: this._inputSize[0],\n inputHeight: this._inputSize[1],\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n maskThreshold: this._maskThreshold,\n },\n );\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence, d.mask),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n const masks = this._buildMasks(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new SegmentationResults(\n boxes,\n masks,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _splitOutputs(outputs: Record<string, ort.Tensor>): {\n perAnchor: ort.Tensor;\n prototypes: ort.Tensor;\n } {\n let perAnchor: ort.Tensor | undefined;\n let prototypes: ort.Tensor | undefined;\n for (const name of this._session.outputNames) {\n const t = outputs[name];\n if (t === undefined) continue;\n if (t.dims.length === 3 && perAnchor === undefined) {\n perAnchor = t;\n } else if (t.dims.length === 4 && prototypes === undefined) {\n prototypes = t;\n }\n }\n if (perAnchor === undefined || prototypes === undefined) {\n const shapes = this._session.outputNames.map(\n (n) => `${n}: ${JSON.stringify(outputs[n]?.dims ?? [])}`,\n );\n throw new Error(\n `Segmenter expected one 3-D and one 4-D output, got [${shapes.join(\", \")}].`,\n );\n }\n return { perAnchor, prototypes };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n mask: Mask,\n ): SegmentationResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let segmentedImage: RGBImage;\n let finalMask = mask;\n if (cx2 > cx1 && cy2 > cy1 && mask.data.length > 0) {\n const cropW = cx2 - cx1;\n const cropH = cy2 - cy1;\n const mw = Math.min(mask.width, cropW);\n const mh = Math.min(mask.height, cropH);\n const segData = new Uint8Array(mw * mh * 3);\n for (let row = 0; row < mh; row++) {\n const srcRowOffset = ((cy1 + row) * original.width + cx1) * 3;\n const dstRowOffset = row * mw * 3;\n const maskRowOffset = row * mask.width;\n for (let col = 0; col < mw; col++) {\n const m = mask.data[maskRowOffset + col];\n if (m !== 0) {\n const s = srcRowOffset + col * 3;\n const d = dstRowOffset + col * 3;\n segData[d] = original.data[s];\n segData[d + 1] = original.data[s + 1];\n segData[d + 2] = original.data[s + 2];\n }\n }\n }\n segmentedImage = new RGBImage(segData, mw, mh);\n if (mw !== mask.width || mh !== mask.height) {\n const trimmed = new Uint8Array(mw * mh);\n for (let row = 0; row < mh; row++) {\n trimmed.set(\n mask.data.subarray(row * mask.width, row * mask.width + mw),\n row * mw,\n );\n }\n finalMask = new Mask(trimmed, mw, mh);\n }\n } else {\n finalMask = new Mask(new Uint8Array(0), 0, 0);\n segmentedImage = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n mask: finalMask,\n segmentedImage,\n };\n }\n\n private _buildBoxes(\n detections: readonly SegmentationResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as SegmentationResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n\n private _buildMasks(\n detections: readonly SegmentationResult[],\n origShape: readonly [number, number],\n ): Masks {\n const xyxy = new Float32Array(detections.length * 4);\n for (let i = 0; i < detections.length; i++) {\n const d = detections[i] as SegmentationResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n }\n return new Masks(\n detections.map((d) => d.mask),\n xyxy,\n origShape,\n );\n }\n}\n"],"mappings":"+RAqFA,IAAa,EAAb,MAAa,UAAkB,EAAA,UAAW,CAGjB,MACA,QACA,OACA,WACA,eACA,cACA,eACA,eATrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EATI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,eAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,EACA,KAAA,eAAA,CAGrB,CAGA,aAAa,OAAO,EAAoB,EAA4B,CAAC,EAAuB,CACxF,IAAM,EAAsB,EAAQ,MAAQ,WAC5C,GAAI,IAAS,WACT,MAAU,MAAM,+BAA+B,EAAK,0BAA0B,EAElF,IAAM,EAAU,MAAM,EAAA,WAAW,OAAO,EAAO,CAAO,EAChD,EAAS,EAAA,cAAc,EAAQ,QAAU,OAAQ,CACnD,WAAY,EAAQ,UACxB,CAAC,EACK,EAAgC,CAAC,EACvC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,OAAQ,IAC/B,EAAM,GAAK,EAAO,GAEtB,OAAO,IAAI,EACP,EACA,EACA,EACA,EACA,EAAQ,WAAa,CAAC,IAAK,GAAG,EAC9B,EAAQ,eAAiB,IACzB,EAAQ,cAAgB,IACxB,EAAQ,eAAiB,IACzB,EAAQ,eAAiB,EAC7B,CACJ,CAGA,IAAI,MAAsB,CACtB,OAAO,KAAK,KAChB,CAGA,IAAI,QAA4B,CAC5B,OAAO,KAAK,OAChB,CAGA,IAAI,OAA0C,CAC1C,OAAO,KAAK,MAChB,CAGA,IAAI,YAAqB,CACrB,OAAO,KAAK,QAAQ,MACxB,CAGA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A,EACK,CACL,IAAM,EAAI,EAAW,OACf,EAAO,IAAI,aAAa,EAAI,CAAC,EAC7B,EAAM,IAAI,WAAW,CAAC,EACtB,EAAO,IAAI,aAAa,CAAC,EAC/B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAG,IAAK,CACxB,IAAM,EAAI,EAAW,GACrB,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAI,GAAK,EAAE,QACX,EAAK,GAAK,EAAE,UAChB,CACA,OAAO,IAAI,EAAA,MAAM,EAAM,EAAK,EAAM,CAAS,CAC/C,CAEA,YACI,EACA,EACK,CACL,IAAM,EAAO,IAAI,aAAa,EAAW,OAAS,CAAC,EACnD,IAAK,IAAI,EAAI,EAAG,EAAI,EAAW,OAAQ,IAAK,CACxC,IAAM,EAAI,EAAW,GACrB,EAAK,EAAI,GAAK,EAAE,KAAK,GACrB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,GACzB,EAAK,EAAI,EAAI,GAAK,EAAE,KAAK,EAC7B,CACA,OAAO,IAAI,EAAA,MACP,EAAW,IAAK,GAAM,EAAE,IAAI,EAC5B,EACA,CACJ,CACJ,CACJ"}
1
+ {"version":3,"file":"segmenter.cjs","names":[],"sources":["../../../src/vision/tasks/segmenter.ts"],"sourcesContent":["/**\n * Instance-segmentation task using YOLO seg ONNX models (v8-seg / v11-seg / ...).\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\n\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { detectionNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport { decodeYoloSeg } from \"../postprocess/segmentation\";\nimport { letterbox, toCHW, toFloat32, toFloat32Tensor } from \"../preprocess/image\";\nimport { Boxes, Masks, SegmentationResults } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type BoundingBox, type SegmentationResult, Mask, RGBImage } from \"../types\";\n\n/**\n * Decoder family for the segmentation head.\n *\n * - `\"yolo-seg\"`: YOLO instance-segmentation head with two outputs —\n * `[1, 4 + nc + nm, N]` per-anchor predictions plus `[1, nm, mh, mw]`\n * prototype masks. Covers YOLOv8-seg, v11-seg, v26-seg.\n *\n * The SDK does **not** auto-detect this — the caller is responsible for\n * picking a head that matches their export.\n */\nexport type SegmenterHead = \"yolo-seg\";\n\nexport interface SegmenterOptions extends OrtSessionOptions {\n /**\n * Decoder family for the segmentation head. Default `\"yolo-seg\"` covers\n * YOLOv8-seg/v11-seg/v26-seg.\n */\n readonly head?: SegmenterHead;\n /** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */\n readonly labels?: LabelSpec;\n /** Number of classes — used to validate the supplied labels. */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels for letterboxing.\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 `[640, 640]`.\n */\n readonly inputSize?: readonly [number, number];\n /** Default minimum class score to keep a candidate. */\n readonly confThreshold?: number;\n /** Default IoU threshold for non-maximum suppression. */\n readonly iouThreshold?: number;\n /** Maximum number of instances per image. */\n readonly maxDetections?: number;\n /** Probability cutoff applied to soft masks. Defaults to `0.5`. */\n readonly maskThreshold?: number;\n}\n\nexport interface SegmenterPredictOptions {\n readonly confThreshold?: number;\n readonly iouThreshold?: number;\n /**\n * If set, keep only instances whose `classId` is in this list.\n * Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.\n */\n readonly classes?: readonly number[];\n}\n\n/**\n * Instance segmenter for YOLO seg ONNX models (v8-seg / v11-seg / ...).\n *\n * The model is expected to expose two outputs:\n *\n * 1. `output0`: `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — per-anchor\n * predictions (boxes, class scores, mask coefficients).\n * 2. `output1`: `(1, numMaskCoefs, maskH, maskW)` — prototype masks.\n *\n * `predict()` returns `Promise<SegmentationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes:\n *\n * - `boxes`: bulk numpy view (`xyxy`, `xywh`, `xyxyn`, `xywhn`, `cls`, `conf`).\n * - `masks`: per-instance binary masks cropped to each box.\n * - per-instance {@link SegmentationResult} via iteration.\n *\n * @example\n * ```typescript\n * const seg = await Segmenter.create(\"/models/yolov8n-seg.onnx\");\n * const r = (await seg.predict(\"/images/street.jpg\"))[0];\n * for (const inst of r) {\n * console.log(inst.cls, inst.conf, inst.box.xyxy);\n * }\n * ```\n */\nexport class Segmenter extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _head: SegmenterHead,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _confThreshold: number,\n private readonly _iouThreshold: number,\n private readonly _maxDetections: number,\n private readonly _maskThreshold: number,\n ) {\n super(session);\n }\n\n /** Load the model and resolve labels. */\n static async create(model: ModelSource, options: SegmenterOptions = {}): Promise<Segmenter> {\n const head: SegmenterHead = options.head ?? \"yolo-seg\";\n if (head !== \"yolo-seg\") {\n throw new Error(`Unsupported segmenter head '${head}'. Supported: 'yolo-seg'.`);\n }\n const session = await OrtSession.create(model, options);\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata) ?? \"coco\", {\n numClasses: options.numClasses ?? detectionNumClasses(session.outputShape) ?? undefined,\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 Segmenter(\n session,\n head,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [640, 640],\n }),\n options.confThreshold ?? 0.25,\n options.iouThreshold ?? 0.45,\n options.maxDetections ?? 300,\n options.maskThreshold ?? 0.5,\n );\n }\n\n /** The decoder family used to interpret the model's output. */\n get head(): SegmenterHead {\n return this._head;\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 predicts. */\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: SegmenterPredictOptions = {},\n ): Promise<SegmentationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run instance segmentation on a single image. */\n async predict(\n image: ImageInput,\n options: SegmenterPredictOptions = {},\n ): Promise<SegmentationResults[]> {\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, scale, padLeft, padTop } = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n timer.stage(\"inference\");\n\n const { perAnchor, prototypes } = this._splitOutputs(outputs);\n\n const decodedAll = decodeYoloSeg(\n perAnchor.data as Float32Array,\n perAnchor.dims,\n prototypes.data as Float32Array,\n prototypes.dims,\n {\n numClasses: this._labels.length,\n inputWidth: this._inputSize[0],\n inputHeight: this._inputSize[1],\n originalWidth: original.width,\n originalHeight: original.height,\n padLeft,\n padTop,\n scale,\n confThreshold: options.confThreshold ?? this._confThreshold,\n iouThreshold: options.iouThreshold ?? this._iouThreshold,\n maxDetections: this._maxDetections,\n maskThreshold: this._maskThreshold,\n },\n );\n\n const decoded =\n options.classes !== undefined\n ? (() => {\n const allowed = new Set(options.classes);\n return decodedAll.filter((d) => allowed.has(d.classId));\n })()\n : decodedAll;\n\n const detections = decoded.map((d) =>\n this._buildResult(original, d.bbox, d.classId, d.confidence, d.mask),\n );\n\n const orig: readonly [number, number] = [original.height, original.width];\n const boxes = this._buildBoxes(detections, orig);\n const masks = this._buildMasks(detections, orig);\n timer.stage(\"postprocess\");\n return [\n new SegmentationResults(\n boxes,\n masks,\n detections,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): {\n tensor: ort.Tensor;\n scale: number;\n padLeft: number;\n padTop: number;\n } {\n const [tw, th] = this._inputSize;\n const lb = letterbox(image, tw, th);\n const f32 = toFloat32(lb.image);\n const chw = toCHW(f32, lb.image.width, lb.image.height, 3);\n return {\n tensor: toFloat32Tensor(chw, [1, 3, lb.image.height, lb.image.width]),\n scale: lb.scale,\n padLeft: lb.padLeft,\n padTop: lb.padTop,\n };\n }\n\n private _splitOutputs(outputs: Record<string, ort.Tensor>): {\n perAnchor: ort.Tensor;\n prototypes: ort.Tensor;\n } {\n let perAnchor: ort.Tensor | undefined;\n let prototypes: ort.Tensor | undefined;\n for (const name of this._session.outputNames) {\n const t = outputs[name];\n if (t === undefined) continue;\n if (t.dims.length === 3 && perAnchor === undefined) {\n perAnchor = t;\n } else if (t.dims.length === 4 && prototypes === undefined) {\n prototypes = t;\n }\n }\n if (perAnchor === undefined || prototypes === undefined) {\n const shapes = this._session.outputNames.map(\n (n) => `${n}: ${JSON.stringify(outputs[n]?.dims ?? [])}`,\n );\n throw new Error(\n `Segmenter expected one 3-D and one 4-D output, got [${shapes.join(\", \")}].`,\n );\n }\n return { perAnchor, prototypes };\n }\n\n private _buildResult(\n original: RGBImage,\n bbox: BoundingBox,\n classId: number,\n confidence: number,\n mask: Mask,\n ): SegmentationResult {\n const [x1, y1, x2, y2] = bbox.asIntXyxy();\n const cx1 = Math.max(0, x1);\n const cy1 = Math.max(0, y1);\n const cx2 = Math.min(original.width, x2);\n const cy2 = Math.min(original.height, y2);\n\n let segmentedImage: RGBImage;\n let finalMask = mask;\n if (cx2 > cx1 && cy2 > cy1 && mask.data.length > 0) {\n const cropW = cx2 - cx1;\n const cropH = cy2 - cy1;\n const mw = Math.min(mask.width, cropW);\n const mh = Math.min(mask.height, cropH);\n const segData = new Uint8Array(mw * mh * 3);\n for (let row = 0; row < mh; row++) {\n const srcRowOffset = ((cy1 + row) * original.width + cx1) * 3;\n const dstRowOffset = row * mw * 3;\n const maskRowOffset = row * mask.width;\n for (let col = 0; col < mw; col++) {\n const m = mask.data[maskRowOffset + col];\n if (m !== 0) {\n const s = srcRowOffset + col * 3;\n const d = dstRowOffset + col * 3;\n segData[d] = original.data[s];\n segData[d + 1] = original.data[s + 1];\n segData[d + 2] = original.data[s + 2];\n }\n }\n }\n segmentedImage = new RGBImage(segData, mw, mh);\n if (mw !== mask.width || mh !== mask.height) {\n const trimmed = new Uint8Array(mw * mh);\n for (let row = 0; row < mh; row++) {\n trimmed.set(\n mask.data.subarray(row * mask.width, row * mask.width + mw),\n row * mw,\n );\n }\n finalMask = new Mask(trimmed, mw, mh);\n }\n } else {\n finalMask = new Mask(new Uint8Array(0), 0, 0);\n segmentedImage = new RGBImage(new Uint8Array(0), 0, 0);\n }\n\n const className = this._names[classId] ?? `class_${classId}`;\n\n return {\n classId,\n className,\n confidence,\n bbox,\n cls: classId,\n name: className,\n conf: confidence,\n box: bbox,\n mask: finalMask,\n segmentedImage,\n };\n }\n\n private _buildBoxes(\n detections: readonly SegmentationResult[],\n origShape: readonly [number, number],\n ): Boxes {\n const n = detections.length;\n const xyxy = new Float32Array(n * 4);\n const cls = new Int32Array(n);\n const conf = new Float32Array(n);\n for (let i = 0; i < n; i++) {\n const d = detections[i] as SegmentationResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n cls[i] = d.classId;\n conf[i] = d.confidence;\n }\n return new Boxes(xyxy, cls, conf, origShape);\n }\n\n private _buildMasks(\n detections: readonly SegmentationResult[],\n origShape: readonly [number, number],\n ): Masks {\n const xyxy = new Float32Array(detections.length * 4);\n for (let i = 0; i < detections.length; i++) {\n const d = detections[i] as SegmentationResult;\n xyxy[i * 4] = d.bbox.x1;\n xyxy[i * 4 + 1] = d.bbox.y1;\n xyxy[i * 4 + 2] = d.bbox.x2;\n xyxy[i * 4 + 3] = d.bbox.y2;\n }\n return new Masks(\n detections.map((d) => d.mask),\n xyxy,\n origShape,\n );\n }\n}\n"],"mappings":"gWA8FA,IAAa,EAAb,MAAa,UAAkB,EAAA,UAAW,CAGjB,MACA,QACA,OACA,WACA,eACA,cACA,eACA,eATrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EATI,KAAA,MAAA,EACA,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,eAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,EACA,KAAA,eAAA,CAGrB,CAGA,aAAa,OAAO,EAAoB,EAA4B,CAAC,EAAuB,CACxF,IAAM,EAAsB,EAAQ,MAAQ,WAC5C,GAAI,IAAS,WACT,MAAU,MAAM,+BAA+B,EAAK,0BAA0B,EAElF,IAAM,EAAU,MAAM,EAAA,WAAW,OAAO,EAAO,CAAO,EAChD,EAAS,EAAA,cAAc,EAAQ,QAAU,EAAA,WAAW,EAAQ,QAAQ,GAAK,OAAQ,CACnF,WAAY,EAAQ,YAAc,EAAA,oBAAoB,EAAQ,WAAW,GAAK,IAAA,EAClF,CAAC,EACK,EAAgC,CAAC,EACvC,IAAK,IAAI,EAAI,EAAG,EAAI,EAAO,OAAQ,IAC/B,EAAM,GAAK,EAAO,GAEtB,OAAO,IAAI,EACP,EACA,EACA,EACA,EACA,EAAA,iBAAiB,CACb,WAAY,EAAQ,WACpB,UAAW,EAAQ,UACnB,SA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