tempest-react-sdk 0.69.0 → 0.70.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.
Files changed (77) hide show
  1. package/README.md +1 -1
  2. package/dist/vision/core/canvas.cjs +1 -1
  3. package/dist/vision/core/canvas.cjs.map +1 -1
  4. package/dist/vision/core/canvas.js +37 -13
  5. package/dist/vision/core/canvas.js.map +1 -1
  6. package/dist/vision/core/dtypes.cjs +2 -0
  7. package/dist/vision/core/dtypes.cjs.map +1 -0
  8. package/dist/vision/core/dtypes.js +33 -0
  9. package/dist/vision/core/dtypes.js.map +1 -0
  10. package/dist/vision/core/graph.cjs +1 -1
  11. package/dist/vision/core/graph.cjs.map +1 -1
  12. package/dist/vision/core/graph.js +10 -2
  13. package/dist/vision/core/graph.js.map +1 -1
  14. package/dist/vision/core/metadata.cjs +2 -2
  15. package/dist/vision/core/metadata.cjs.map +1 -1
  16. package/dist/vision/core/metadata.js +103 -13
  17. package/dist/vision/core/metadata.js.map +1 -1
  18. package/dist/vision/core/providers.cjs +1 -1
  19. package/dist/vision/core/providers.cjs.map +1 -1
  20. package/dist/vision/core/providers.js +9 -6
  21. package/dist/vision/core/providers.js.map +1 -1
  22. package/dist/vision/core/session.cjs +1 -1
  23. package/dist/vision/core/session.cjs.map +1 -1
  24. package/dist/vision/core/session.js +115 -27
  25. package/dist/vision/core/session.js.map +1 -1
  26. package/dist/vision/fusion.cjs +1 -1
  27. package/dist/vision/fusion.cjs.map +1 -1
  28. package/dist/vision/fusion.js +28 -23
  29. package/dist/vision/fusion.js.map +1 -1
  30. package/dist/vision/index.cjs +1 -1
  31. package/dist/vision/index.cjs.map +1 -1
  32. package/dist/vision/index.js +20 -19
  33. package/dist/vision/index.js.map +1 -1
  34. package/dist/vision/io/image.cjs +1 -1
  35. package/dist/vision/io/image.cjs.map +1 -1
  36. package/dist/vision/io/image.js +55 -20
  37. package/dist/vision/io/image.js.map +1 -1
  38. package/dist/vision/postprocess/detection.cjs +1 -1
  39. package/dist/vision/postprocess/detection.cjs.map +1 -1
  40. package/dist/vision/postprocess/detection.js +42 -51
  41. package/dist/vision/postprocess/detection.js.map +1 -1
  42. package/dist/vision/postprocess/segmentation.cjs +1 -1
  43. package/dist/vision/postprocess/segmentation.cjs.map +1 -1
  44. package/dist/vision/postprocess/segmentation.js +45 -31
  45. package/dist/vision/postprocess/segmentation.js.map +1 -1
  46. package/dist/vision/preprocess/pipeline.cjs +1 -1
  47. package/dist/vision/preprocess/pipeline.cjs.map +1 -1
  48. package/dist/vision/preprocess/pipeline.js +24 -25
  49. package/dist/vision/preprocess/pipeline.js.map +1 -1
  50. package/dist/vision/tasks/base.cjs +1 -1
  51. package/dist/vision/tasks/base.cjs.map +1 -1
  52. package/dist/vision/tasks/base.js +22 -7
  53. package/dist/vision/tasks/base.js.map +1 -1
  54. package/dist/vision/tasks/classifier.cjs +1 -1
  55. package/dist/vision/tasks/classifier.cjs.map +1 -1
  56. package/dist/vision/tasks/classifier.js +64 -55
  57. package/dist/vision/tasks/classifier.js.map +1 -1
  58. package/dist/vision/tasks/detectClassify.cjs +1 -1
  59. package/dist/vision/tasks/detectClassify.cjs.map +1 -1
  60. package/dist/vision/tasks/detectClassify.js +80 -69
  61. package/dist/vision/tasks/detectClassify.js.map +1 -1
  62. package/dist/vision/tasks/detector.cjs +1 -1
  63. package/dist/vision/tasks/detector.cjs.map +1 -1
  64. package/dist/vision/tasks/detector.js +74 -68
  65. package/dist/vision/tasks/detector.js.map +1 -1
  66. package/dist/vision/tasks/segmenter.cjs +1 -1
  67. package/dist/vision/tasks/segmenter.cjs.map +1 -1
  68. package/dist/vision/tasks/segmenter.js +85 -62
  69. package/dist/vision/tasks/segmenter.js.map +1 -1
  70. package/dist/vision/types.cjs +1 -1
  71. package/dist/vision/types.cjs.map +1 -1
  72. package/dist/vision/types.js +24 -4
  73. package/dist/vision/types.js.map +1 -1
  74. package/dist/vision.cjs +1 -1
  75. package/dist/vision.d.ts +1153 -777
  76. package/dist/vision.js +22 -21
  77. package/package.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { NoDetectionsError as e } from "../core/exceptions.js";
2
+ import { RGBImage as t } from "../types.js";
2
3
  //#region src/vision/tasks/base.ts
3
- var t = class {
4
+ var n = class {
4
5
  _session;
5
6
  constructor(e) {
6
7
  this._session = e;
@@ -9,15 +10,29 @@ var t = class {
9
10
  return this._session;
10
11
  }
11
12
  };
12
- function n(e) {
13
+ function r(e) {
13
14
  return e.toFixed(6).replace(/0+$/, "").replace(/\.$/, "");
14
15
  }
15
- function r(t, r) {
16
- if (!r.raiseOnEmpty || t > 0) return;
17
- let i = r.path ? ` in ${r.path}` : "", a = r.classes === void 0 ? "" : ` among classes [${[...r.classes].sort((e, t) => e - t).join(", ")}]`;
18
- throw new e(`No detections${i}${a}: nothing cleared confThreshold=${n(r.confThreshold)}.`);
16
+ function i(t, n) {
17
+ if (!n.raiseOnEmpty || t > 0) return;
18
+ let i = n.path ? ` in ${n.path}` : "", a = n.classes === void 0 ? "" : ` among classes [${[...n.classes].sort((e, t) => e - t).join(", ")}]`;
19
+ throw new e(`No detections${i}${a}: nothing cleared confThreshold=${r(n.confThreshold)}.`);
20
+ }
21
+ function a(e, n) {
22
+ let [r, i, a, o] = n.asIntXyxy(), s = Math.max(0, r), c = Math.max(0, i), l = Math.min(e.width, a), u = Math.min(e.height, o);
23
+ if (l <= s || u <= c) return new t(/* @__PURE__ */ new Uint8Array(), 0, 0);
24
+ let d = l - s, f = u - c, p = new Uint8Array(d * f * 3);
25
+ for (let t = 0; t < f; t++) {
26
+ let n = ((c + t) * e.width + s) * 3;
27
+ p.set(e.data.subarray(n, n + d * 3), t * d * 3);
28
+ }
29
+ return new t(p, d, f);
30
+ }
31
+ function o(e) {
32
+ let t = !1, n;
33
+ return () => (t ||= (n = e(), !0), n);
19
34
  }
20
35
  //#endregion
21
- export { t as VisionTask, r as requireDetections };
36
+ export { n as VisionTask, a as cropToBox, o as memoize, i as requireDetections };
22
37
 
23
38
  //# sourceMappingURL=base.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","names":[],"sources":["../../../src/vision/tasks/base.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Common foundation for task-oriented vision SDK objects.\n *\n * The base only owns the {@link OrtSession} — label resolution lives in each\n * subclass because how `numClasses` is read from the model differs per task.\n */\n\nimport { NoDetectionsError } from \"../core/exceptions\";\nimport type { OrtSession } from \"../core/session\";\n\nexport abstract class VisionTask {\n protected constructor(protected readonly _session: OrtSession) {}\n\n /** The underlying {@link OrtSession} used to run inference. */\n get session(): OrtSession {\n return this._session;\n }\n}\n\n/**\n * Render a confidence threshold the way the Python SDK renders it.\n *\n * JavaScript and Python disagree on how a number becomes text. A whole\n * threshold is `1` here and `1.0` there; JavaScript holds off on exponent\n * notation until `1e-7` while Python switches at `1e-5`. A fused pipeline is\n * built once and runs under both runtimes from the same file, so a message that\n * quotes the threshold has to quote it identically — otherwise the two SDKs\n * describe the same run with two different numbers.\n *\n * Six decimals with the trailing zeros trimmed covers every threshold a caller\n * can meaningfully set and agrees byte for byte with `_format_threshold` in the\n * Python SDK's `tasks/base.py`. The pairing is fixed by a shared table in both\n * test suites, so a change on one side that is not mirrored on the other fails.\n *\n * @param value The threshold to render.\n * @returns The threshold as text, without trailing zeros or a trailing dot.\n */\nfunction formatThreshold(value: number): string {\n return value.toFixed(6).replace(/0+$/, \"\").replace(/\\.$/, \"\");\n}\n\n/**\n * Turn an empty result into an error, when the caller asked for that.\n *\n * Shared by every task that can come back with nothing — {@link Detector},\n * {@link Segmenter} and {@link DetectClassify} — so the three agree on when\n * they throw and on what the message says. The message names the two settings\n * that decide the outcome, because \"no detections\" on its own leaves the reader\n * unable to tell a blank image from a threshold set too high.\n *\n * @param count How many detections survived every filter.\n * @param options The flag for this call, the threshold actually applied (after\n * any per-call override), the class allowlist if one narrowed the search, and\n * the source path when the input was one.\n * @throws {@link NoDetectionsError} when the flag is set and `count` is zero.\n */\nexport function requireDetections(\n count: number,\n options: {\n readonly raiseOnEmpty: boolean;\n readonly confThreshold: number;\n readonly classes: readonly number[] | undefined;\n readonly path: string | null;\n },\n): void {\n if (!options.raiseOnEmpty || count > 0) return;\n const where = options.path ? ` in ${options.path}` : \"\";\n const narrowed =\n options.classes === undefined\n ? \"\"\n : ` among classes [${[...options.classes].sort((a, b) => a - b).join(\", \")}]`;\n throw new NoDetectionsError(\n `No detections${where}${narrowed}: nothing cleared confThreshold=${formatThreshold(options.confThreshold)}.`,\n );\n}\n"],"mappings":";;AAWA,IAAsB,IAAtB,MAAiC;CACY;CAAzC,YAAsB,GAAyC;EAAtB,KAAA,WAAA;CAAuB;CAGhE,IAAI,UAAsB;EACtB,OAAO,KAAK;CAChB;AACJ;AAoBA,SAAS,EAAgB,GAAuB;CAC5C,OAAO,EAAM,QAAQ,CAAC,CAAC,CAAC,QAAQ,OAAO,EAAE,CAAC,CAAC,QAAQ,OAAO,EAAE;AAChE;AAiBA,SAAgB,EACZ,GACA,GAMI;CACJ,IAAI,CAAC,EAAQ,gBAAgB,IAAQ,GAAG;CACxC,IAAM,IAAQ,EAAQ,OAAO,OAAO,EAAQ,SAAS,IAC/C,IACF,EAAQ,YAAY,KAAA,IACd,KACA,mBAAmB,CAAC,GAAG,EAAQ,OAAO,CAAC,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;CACnF,MAAM,IAAI,EACN,gBAAgB,IAAQ,EAAS,kCAAkC,EAAgB,EAAQ,aAAa,EAAE,EAC9G;AACJ"}
1
+ {"version":3,"file":"base.js","names":[],"sources":["../../../src/vision/tasks/base.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Common foundation for task-oriented vision SDK objects.\n *\n * The base only owns the {@link OrtSession} — label resolution lives in each\n * subclass because how `numClasses` is read from the model differs per task.\n */\n\nimport { NoDetectionsError } from \"../core/exceptions\";\nimport type { OrtSession } from \"../core/session\";\nimport { type BoundingBox, RGBImage } from \"../types\";\n\nexport abstract class VisionTask {\n protected constructor(protected readonly _session: OrtSession) {}\n\n /** The underlying {@link OrtSession} used to run inference. */\n get session(): OrtSession {\n return this._session;\n }\n}\n\n/**\n * Render a confidence threshold the way the Python SDK renders it.\n *\n * JavaScript and Python disagree on how a number becomes text. A whole\n * threshold is `1` here and `1.0` there; JavaScript holds off on exponent\n * notation until `1e-7` while Python switches at `1e-5`. A fused pipeline is\n * built once and runs under both runtimes from the same file, so a message that\n * quotes the threshold has to quote it identically — otherwise the two SDKs\n * describe the same run with two different numbers.\n *\n * Six decimals with the trailing zeros trimmed covers every threshold a caller\n * can meaningfully set and agrees byte for byte with `_format_threshold` in the\n * Python SDK's `tasks/base.py`. The pairing is fixed by a shared table in both\n * test suites, so a change on one side that is not mirrored on the other fails.\n *\n * @param value The threshold to render.\n * @returns The threshold as text, without trailing zeros or a trailing dot.\n */\nfunction formatThreshold(value: number): string {\n return value.toFixed(6).replace(/0+$/, \"\").replace(/\\.$/, \"\");\n}\n\n/**\n * Turn an empty result into an error, when the caller asked for that.\n *\n * Shared by every task that can come back with nothing — {@link Detector},\n * {@link Segmenter} and {@link DetectClassify} — so the three agree on when\n * they throw and on what the message says. The message names the two settings\n * that decide the outcome, because \"no detections\" on its own leaves the reader\n * unable to tell a blank image from a threshold set too high.\n *\n * @param count How many detections survived every filter.\n * @param options The flag for this call, the threshold actually applied (after\n * any per-call override), the class allowlist if one narrowed the search, and\n * the source path when the input was one.\n * @throws {@link NoDetectionsError} when the flag is set and `count` is zero.\n */\nexport function requireDetections(\n count: number,\n options: {\n readonly raiseOnEmpty: boolean;\n readonly confThreshold: number;\n readonly classes: readonly number[] | undefined;\n readonly path: string | null;\n },\n): void {\n if (!options.raiseOnEmpty || count > 0) return;\n const where = options.path ? ` in ${options.path}` : \"\";\n const narrowed =\n options.classes === undefined\n ? \"\"\n : ` among classes [${[...options.classes].sort((a, b) => a - b).join(\", \")}]`;\n throw new NoDetectionsError(\n `No detections${where}${narrowed}: nothing cleared confThreshold=${formatThreshold(options.confThreshold)}.`,\n );\n}\n\n/**\n * Cut the box region out of an image.\n *\n * The box is truncated to whole pixels and clamped to the image, the same\n * rounding every task applies, so a crop always matches `bbox.asIntXyxy()`\n * within the frame.\n *\n * @param image The source image.\n * @param bbox The box, in the image's pixel coordinates.\n * @returns The cropped region, or a zero-sized image for a box with no area.\n */\nexport function cropToBox(image: RGBImage, bbox: BoundingBox): RGBImage {\n const [rawX1, rawY1, rawX2, rawY2] = bbox.asIntXyxy();\n const x1 = Math.max(0, rawX1);\n const y1 = Math.max(0, rawY1);\n const x2 = Math.min(image.width, rawX2);\n const y2 = Math.min(image.height, rawY2);\n if (x2 <= x1 || y2 <= y1) return new RGBImage(new Uint8Array(0), 0, 0);\n\n const width = x2 - x1;\n const height = y2 - y1;\n const out = new Uint8Array(width * height * 3);\n for (let row = 0; row < height; row++) {\n const offset = ((y1 + row) * image.width + x1) * 3;\n out.set(image.data.subarray(offset, offset + width * 3), row * width * 3);\n }\n return new RGBImage(out, width, height);\n}\n\n/**\n * Wrap a computation so it runs on first call and is remembered after.\n *\n * Backs the per-detection images (`croppedImage`, `segmentedImage`): most\n * callers read a box and a class and never look at the pixels, yet building\n * them eagerly copied every box region out of the frame on every\n * `predict()` — up to `maxDetections` allocations nobody asked for.\n *\n * @param compute Produces the value; called at most once.\n * @returns A getter returning the value, computing it on the first call.\n */\nexport function memoize<T>(compute: () => T): () => T {\n let done = false;\n let value: T;\n return () => {\n if (!done) {\n value = compute();\n done = true;\n }\n return value;\n };\n}\n"],"mappings":";;;AAYA,IAAsB,IAAtB,MAAiC;CACY;CAAzC,YAAsB,GAAyC;EAAtB,KAAA,WAAA;CAAuB;CAGhE,IAAI,UAAsB;EACtB,OAAO,KAAK;CAChB;AACJ;AAoBA,SAAS,EAAg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EJ;AAEf"}
@@ -1,2 +1,2 @@
1
- const e=require("../core/timing.cjs"),t=require("../results.cjs"),n=require("../labels.cjs"),r=require("../core/graph.cjs"),i=require("../core/metadata.cjs"),a=require("../core/session.cjs"),o=require("../normalization.cjs"),s=require("../io/image.cjs"),c=require("../preprocess/image.cjs"),l=require("../preprocess/pipeline.cjs"),u=require("../postprocess/classification.cjs"),d=require("./base.cjs");var f=class f extends d.VisionTask{_labels;_names;_inputSize;_mean;_std;_applySoftmax;_normalization;constructor(e,t,n,r,i,a,o,s){super(e),this._labels=t,this._names=n,this._inputSize=r,this._mean=i,this._std=a,this._applySoftmax=o,this._normalization=s}get appliesSoftmax(){return this._applySoftmax}get normalization(){return this._normalization}_pipelineCache=null;async warmup(e=1){let[t,n]=this._inputSize;for(let r=0;r<e;r++){let e=c.toFloat32Tensor(l.zeroTensorData(t,n),[1,3,n,t]);await this._session.run({[this._session.inputName]:e})}}get _pipeline(){if(this._pipelineCache===null){let[e,t]=this._inputSize;this._pipelineCache=new l.ResizePipeline(e,t,this._mean,this._std)}return this._pipelineCache}static async create(e,t={}){let s=await a.OrtSession.create(e,t),c=o.resolveNormalization(s.metadata,{normalization:t.normalization,mean:t.mean,std:t.std}),l=t.numClasses??r.classificationNumClasses(s.outputShape)??void 0,u=n.resolveLabels(t.labels??i.modelNames(s.metadata),{numClasses:l}),d={};for(let e=0;e<u.length;e++)d[e]=u[e];return new f(s,u,d,r.resolveInputSize({graphShape:s.inputShape,requested:t.inputSize,fallback:[224,224]}),c.mean,c.std,t.applySoftmax??!o.isUltralyticsClassifier(s.metadata),c.name)}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(n,r={}){let i=new e.SpeedTimer,a=typeof n==`string`?n:null,o=await s.loadImage(n);i.stage(`load`);let c=this._preprocess(o);i.stage(`preprocess`);let l=await this._session.run({[this._session.inputName]:c});this._pipeline.release(),i.stage(`inference`);let d=this._session.outputNames[0];if(d===void 0)throw Error(`Classifier model has no outputs.`);let f=l[d];if(f===void 0)throw Error(`Classifier model output ${d} missing from run() result.`);let p=this._postprocess(f.data),{indices:m,values:h}=u.topK(p,r.topK??null),g=[];for(let e=0;e<m.length;e++){let t=m[e],n=this._labels[t]??`class_${t}`;g.push({classId:t,className:n,probability:h[e],cls:t,name:n,conf:h[e]})}if(g.length===0)throw Error(`Classifier produced no probabilities (empty output).`);let _=g[0],v={classId:_.classId,className:_.className,confidence:_.probability,cls:_.classId,name:_.className,conf:_.probability,image:o,probabilities:g},y=[o.height,o.width],b=new t.Probs(p);return i.stage(`postprocess`),[new t.ClassificationResults(b,v,this._names,o,y,a,i.speed())]}_preprocess(e){let[t,n]=this._inputSize,{data:r}=this._pipeline.run(e);return c.toFloat32Tensor(r,[1,3,n,t])}_postprocess(e){return this._applySoftmax?u.softmax(e):new Float32Array(e)}};exports.Classifier=f;
1
+ const e=require("../core/timing.cjs"),t=require("../results.cjs"),n=require("../labels.cjs"),r=require("../core/dtypes.cjs"),i=require("../core/graph.cjs"),a=require("../core/metadata.cjs"),o=require("../core/session.cjs"),s=require("../normalization.cjs"),c=require("../io/image.cjs"),l=require("../preprocess/image.cjs"),u=require("../preprocess/pipeline.cjs"),d=require("../postprocess/classification.cjs"),f=require("./base.cjs");var p=class p extends f.VisionTask{_labels;_names;_inputSize;_mean;_std;_applySoftmax;_normalization;constructor(e,t,n,r,i,a,o,s){super(e),this._labels=t,this._names=n,this._inputSize=r,this._mean=i,this._std=a,this._applySoftmax=o,this._normalization=s}get appliesSoftmax(){return this._applySoftmax}get normalization(){return this._normalization}_pipelineCache=null;async warmup(e=1){let[t,n]=this._inputSize;for(let r=0;r<e;r++){let e=l.toFloat32Tensor(u.zeroTensorData(t,n),[1,3,n,t]);await this._session.run({[this._session.inputName]:e})}}get _pipeline(){if(this._pipelineCache===null){let[e,t]=this._inputSize;this._pipelineCache=new u.ResizePipeline(e,t,this._mean,this._std)}return this._pipelineCache}static async create(e,t={}){let r=await o.OrtSession.create(e,t),c=s.resolveNormalization(r.metadata,{normalization:t.normalization,mean:t.mean,std:t.std}),l=t.numClasses??i.classificationNumClasses(r.outputShape)??void 0,u=n.resolveLabels(t.labels??a.modelNames(r.metadata),{numClasses:l}),d={};for(let e=0;e<u.length;e++)d[e]=u[e];return new p(r,u,d,i.resolveInputSize({graphShape:r.inputShape,requested:t.inputSize,fallback:[224,224]}),c.mean,c.std,t.applySoftmax??!s.isUltralyticsClassifier(r.metadata),c.name)}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(n,i={}){let a=new e.SpeedTimer,o=typeof n==`string`?n:null,{image:s,canvas:l}=await c.loadImageSource(n);a.stage(`load`);let{tensor:u,reused:f}=this._preprocess(s,l);a.stage(`preprocess`);let p;try{p=await this._session.run({[this._session.inputName]:u})}finally{this._pipeline.release({reused:f})}a.stage(`inference`);let m=this._session.outputNames[0];if(m===void 0)throw Error(`Classifier model has no outputs.`);let h=p[m];if(h===void 0)throw Error(`Classifier model output ${m} missing from run() result.`);let g=this._postprocess(r.asFloat32Array(h.data)),{indices:_,values:v}=d.topK(g,i.topK??null),y=[];for(let e=0;e<_.length;e++){let t=_[e],n=this._labels[t]??`class_${t}`;y.push({classId:t,className:n,probability:v[e],cls:t,name:n,conf:v[e]})}if(y.length===0)throw Error(`Classifier produced no probabilities (empty output).`);let b=y[0],x={classId:b.classId,className:b.className,confidence:b.probability,cls:b.classId,name:b.className,conf:b.probability,image:s,probabilities:y},S=[s.height,s.width],C=new t.Probs(g);return a.stage(`postprocess`),[new t.ClassificationResults(C,x,this._names,s,S,o,a.speed())]}_preprocess(e,t){let[n,r]=this._inputSize,{data:i,reused:a}=this._pipeline.run(e,t);return{tensor:l.toFloat32Tensor(i,[1,3,r,n]),reused:a}}_postprocess(e){return this._applySoftmax?d.softmax(e):new Float32Array(e)}};exports.Classifier=p;
2
2
  //# sourceMappingURL=classifier.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"classifier.cjs","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const tensor = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n this._pipeline.release();\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(raw.data as Float32Array);\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): ort.Tensor {\n const [tw, th] = this._inputSize;\n const { data } = this._pipeline.run(image);\n return toFloat32Tensor(data, [1, 3, th, tw]);\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":"kZAyGA,IAAa,EAAb,MAAa,UAAmB,EAAA,UAAW,CAGlB,QACA,OACA,WACA,MACA,KACA,cACA,eARrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EARI,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,MAAA,EACA,KAAA,KAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,CAGrB,CAUA,IAAI,gBAA0B,CAC1B,OAAO,KAAK,aAChB,CAWA,IAAI,eAAwB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,QAAS,EAAI,QACb,UAAW,EAAI,UACf,WAAY,EAAI,YAChB,IAAK,EAAI,QACT,KAAM,EAAI,UACV,KAAM,EAAI,YACV,MAAO,EACP,eACJ,EAEM,EAAkC,CAAC,EAAS,OAAQ,EAAS,KAAK,EAClE,EAAQ,IAAI,EAAA,MAAM,CAAS,EAEjC,OADA,EAAM,MAAM,aAAa,EAClB,CACH,IAAI,EAAA,sBACA,EACA,EACA,KAAK,OACL,EACA,EACA,EACA,EAAM,MAAM,CAChB,CACJ,CACJ,CAEA,YAAoB,EAA6B,CAC7C,GAAM,CAAC,EAAI,GAAM,KAAK,WAChB,CAAE,QAAS,KAAK,UAAU,IAAI,CAAK,EACzC,OAAO,EAAA,gBAAgB,EAAM,CAAC,EAAG,EAAG,EAAI,CAAE,CAAC,CAC/C,CAEA,aAAqB,EAAiC,CAClD,OAAO,KAAK,cAAgB,EAAA,QAAQ,CAAG,EAAI,IAAI,aAAa,CAAG,CACnE,CACJ"}
1
+ {"version":3,"file":"classifier.cjs","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImageSource } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { asFloat32Array } from \"../core/dtypes\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const { image: original, canvas } = await loadImageSource(image);\n timer.stage(\"load\");\n const { tensor, reused } = this._preprocess(original, canvas);\n timer.stage(\"preprocess\");\n let outputs: Record<string, ort.Tensor>;\n try {\n outputs = await this._session.run({ [this._session.inputName]: tensor });\n } finally {\n this._pipeline.release({ reused });\n }\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(asFloat32Array(raw.data));\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Resize and pack the image into the tensor the model expects.\n *\n * @param image The decoded input.\n * @param canvas The opaque canvas it was decoded on, when there is one — see\n * {@link ResizePipeline.run}.\n */\n private _preprocess(\n image: RGBImage,\n canvas: CanvasImageSource | null,\n ): { tensor: ort.Tensor; reused: boolean } {\n const [tw, th] = this._inputSize;\n const { data, reused } = this._pipeline.run(image, canvas);\n return { tensor: toFloat32Tensor(data, [1, 3, th, tw]), reused };\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":"kbA0GA,IAAa,EAAb,MAAa,UAAmB,EAAA,UAAW,CAGlB,QACA,OACA,WACA,MACA,KACA,cACA,eARrB,YACI,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACF,CACE,MAAM,CAAO,EARI,KAAA,QAAA,EACA,KAAA,OAAA,EACA,KAAA,WAAA,EACA,KAAA,MAAA,EACA,KAAA,KAAA,EACA,KAAA,cAAA,EACA,KAAA,eAAA,CAGrB,CAUA,IAAI,gBAA0B,CAC1B,OAAO,KAAK,aAChB,CAWA,IAAI,eAAwB,CACxB,OAAO,KAAK,cAChB,CAEA,eAAgD,KAchD,MAAM,OAAO,EAAe,EAAkB,CAC1C,GAAM,CAAC,EAAI,GAAM,KAAK,WACtB,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,IAAK,CAC3B,IAAM,EAAS,EAAA,gBAAgB,EAAA,eAAe,EAAI,CAAE,EAAG,CAAC,EAAG,EAAG,EAAI,CAAE,CAAC,EACrE,MAAM,KAAK,SAAS,IAAI,EAAG,KAAK,SAAS,WAAY,CAAO,CAAC,CACjE,CACJ,CASA,IAAY,WAA4B,CACpC,GAAI,KAAK,iBAAmB,KAAM,C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OACL,EACA,EACA,EACA,EAAM,MAAM,CAChB,CACJ,CACJ,CASA,YACI,EACA,EACuC,CACvC,GAAM,CAAC,EAAI,GAAM,KAAK,WAChB,CAAE,OAAM,UAAW,KAAK,UAAU,IAAI,EAAO,CAAM,EACzD,MAAO,CAAE,OAAQ,EAAA,gBAAgB,EAAM,CAAC,EAAG,EAAG,EAAI,CAAE,CAAC,EAAG,QAAO,CACnE,CAEA,aAAqB,EAAiC,CAClD,OAAO,KAAK,cAAgB,EAAA,QAAQ,CAAG,EAAI,IAAI,aAAa,CAAG,CACnE,CACJ"}
@@ -1,17 +1,18 @@
1
1
  import { SpeedTimer as e } from "../core/timing.js";
2
2
  import { ClassificationResults as t, Probs as n } from "../results.js";
3
3
  import { resolveLabels as r } from "../labels.js";
4
- import { 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 { isUltralyticsClassifier as c, resolveNormalization as l } from "../normalization.js";
8
- import { loadImage as u } from "../io/image.js";
9
- import { toFloat32Tensor as d } from "../preprocess/image.js";
10
- import { ResizePipeline as f, zeroTensorData as p } from "../preprocess/pipeline.js";
11
- import { softmax as m, topK as h } from "../postprocess/classification.js";
12
- import { VisionTask as g } from "./base.js";
4
+ import { asFloat32Array as i } from "../core/dtypes.js";
5
+ import { classificationNumClasses as a, resolveInputSize as o } from "../core/graph.js";
6
+ import { modelNames as s } from "../core/metadata.js";
7
+ import { OrtSession as c } from "../core/session.js";
8
+ import { isUltralyticsClassifier as l, resolveNormalization as u } from "../normalization.js";
9
+ import { loadImageSource as d } from "../io/image.js";
10
+ import { toFloat32Tensor as f } from "../preprocess/image.js";
11
+ import { ResizePipeline as p, zeroTensorData as m } from "../preprocess/pipeline.js";
12
+ import { softmax as h, topK as g } from "../postprocess/classification.js";
13
+ import { VisionTask as _ } from "./base.js";
13
14
  //#region src/vision/tasks/classifier.ts
14
- var _ = class _ extends g {
15
+ var v = class v extends _ {
15
16
  _labels;
16
17
  _names;
17
18
  _inputSize;
@@ -32,7 +33,7 @@ var _ = class _ extends g {
32
33
  async warmup(e = 1) {
33
34
  let [t, n] = this._inputSize;
34
35
  for (let r = 0; r < e; r++) {
35
- let e = d(p(t, n), [
36
+ let e = f(m(t, n), [
36
37
  1,
37
38
  3,
38
39
  n,
@@ -44,22 +45,22 @@ var _ = class _ extends g {
44
45
  get _pipeline() {
45
46
  if (this._pipelineCache === null) {
46
47
  let [e, t] = this._inputSize;
47
- this._pipelineCache = new f(e, t, this._mean, this._std);
48
+ this._pipelineCache = new p(e, t, this._mean, this._std);
48
49
  }
49
50
  return this._pipelineCache;
50
51
  }
51
52
  static async create(e, t = {}) {
52
- let n = await s.create(e, t), u = l(n.metadata, {
53
+ let n = await c.create(e, t), i = u(n.metadata, {
53
54
  normalization: t.normalization,
54
55
  mean: t.mean,
55
56
  std: t.std
56
- }), d = t.numClasses ?? i(n.outputShape) ?? void 0, f = r(t.labels ?? o(n.metadata), { numClasses: d }), p = {};
57
+ }), d = t.numClasses ?? a(n.outputShape) ?? void 0, f = r(t.labels ?? s(n.metadata), { numClasses: d }), p = {};
57
58
  for (let e = 0; e < f.length; e++) p[e] = f[e];
58
- return new _(n, f, p, a({
59
+ return new v(n, f, p, o({
59
60
  graphShape: n.inputShape,
60
61
  requested: t.inputSize,
61
62
  fallback: [224, 224]
62
- }), u.mean, u.std, t.applySoftmax ?? !c(n.metadata), u.name);
63
+ }), i.mean, i.std, t.applySoftmax ?? !l(n.metadata), i.name);
63
64
  }
64
65
  get labels() {
65
66
  return this._labels;
@@ -76,56 +77,64 @@ var _ = class _ extends g {
76
77
  async call(e, t = {}) {
77
78
  return this.predict(e, t);
78
79
  }
79
- async predict(r, i = {}) {
80
- let a = new e(), o = typeof r == "string" ? r : null, s = await u(r);
81
- a.stage("load");
82
- let c = this._preprocess(s);
83
- a.stage("preprocess");
84
- let l = await this._session.run({ [this._session.inputName]: c });
85
- this._pipeline.release(), a.stage("inference");
86
- let d = this._session.outputNames[0];
87
- if (d === void 0) throw Error("Classifier model has no outputs.");
88
- let f = l[d];
89
- if (f === void 0) throw Error(`Classifier model output ${d} missing from run() result.`);
90
- let p = this._postprocess(f.data), { indices: m, values: g } = h(p, i.topK ?? null), _ = [];
91
- for (let e = 0; e < m.length; e++) {
92
- let t = m[e], n = this._labels[t] ?? `class_${t}`;
93
- _.push({
80
+ async predict(r, a = {}) {
81
+ let o = new e(), s = typeof r == "string" ? r : null, { image: c, canvas: l } = await d(r);
82
+ o.stage("load");
83
+ let { tensor: u, reused: f } = this._preprocess(c, l);
84
+ o.stage("preprocess");
85
+ let p;
86
+ try {
87
+ p = await this._session.run({ [this._session.inputName]: u });
88
+ } finally {
89
+ this._pipeline.release({ reused: f });
90
+ }
91
+ o.stage("inference");
92
+ let m = this._session.outputNames[0];
93
+ if (m === void 0) throw Error("Classifier model has no outputs.");
94
+ let h = p[m];
95
+ if (h === void 0) throw Error(`Classifier model output ${m} missing from run() result.`);
96
+ let _ = this._postprocess(i(h.data)), { indices: v, values: y } = g(_, a.topK ?? null), b = [];
97
+ for (let e = 0; e < v.length; e++) {
98
+ let t = v[e], n = this._labels[t] ?? `class_${t}`;
99
+ b.push({
94
100
  classId: t,
95
101
  className: n,
96
- probability: g[e],
102
+ probability: y[e],
97
103
  cls: t,
98
104
  name: n,
99
- conf: g[e]
105
+ conf: y[e]
100
106
  });
101
107
  }
102
- if (_.length === 0) throw Error("Classifier produced no probabilities (empty output).");
103
- let v = _[0], y = {
104
- classId: v.classId,
105
- className: v.className,
106
- confidence: v.probability,
107
- cls: v.classId,
108
- name: v.className,
109
- conf: v.probability,
110
- image: s,
111
- probabilities: _
112
- }, b = [s.height, s.width], x = new n(p);
113
- return a.stage("postprocess"), [new t(x, y, this._names, s, b, o, a.speed())];
108
+ if (b.length === 0) throw Error("Classifier produced no probabilities (empty output).");
109
+ let x = b[0], S = {
110
+ classId: x.classId,
111
+ className: x.className,
112
+ confidence: x.probability,
113
+ cls: x.classId,
114
+ name: x.className,
115
+ conf: x.probability,
116
+ image: c,
117
+ probabilities: b
118
+ }, C = [c.height, c.width], w = new n(_);
119
+ return o.stage("postprocess"), [new t(w, S, this._names, c, C, s, o.speed())];
114
120
  }
115
- _preprocess(e) {
116
- let [t, n] = this._inputSize, { data: r } = this._pipeline.run(e);
117
- return d(r, [
118
- 1,
119
- 3,
120
- n,
121
- t
122
- ]);
121
+ _preprocess(e, t) {
122
+ let [n, r] = this._inputSize, { data: i, reused: a } = this._pipeline.run(e, t);
123
+ return {
124
+ tensor: f(i, [
125
+ 1,
126
+ 3,
127
+ r,
128
+ n
129
+ ]),
130
+ reused: a
131
+ };
123
132
  }
124
133
  _postprocess(e) {
125
- return this._applySoftmax ? m(e) : new Float32Array(e);
134
+ return this._applySoftmax ? h(e) : new Float32Array(e);
126
135
  }
127
136
  };
128
137
  //#endregion
129
- export { _ as Classifier };
138
+ export { v as Classifier };
130
139
 
131
140
  //# sourceMappingURL=classifier.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"classifier.js","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImage } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const original = await loadImage(image);\n timer.stage(\"load\");\n const tensor = this._preprocess(original);\n timer.stage(\"preprocess\");\n const outputs = await this._session.run({ [this._session.inputName]: tensor });\n this._pipeline.release();\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(raw.data as Float32Array);\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n private _preprocess(image: RGBImage): ort.Tensor {\n const [tw, th] = this._inputSize;\n const { data } = this._pipeline.run(image);\n return toFloat32Tensor(data, [1, 3, th, tw]);\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAyGA,IAAa,IAAb,MAAa,UAAmB,EAAW;CAGlB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GARI,KAAA,UAAA,GACA,KAAA,SAAA,GACA,KAAA,aAAA,GACA,KAAA,QAAA,GACA,KAAA,OAAA,GACA,KAAA,gBAAA,GACA,KAAA,iBAAA;CAGrB;CAUA,IAAI,iBAA0B;EAC1B,OAAO,KAAK;CAChB;CAWA,IAAI,gBAAwB;EACxB,OAAO,KAAK;CAChB;CAEA,iBAAgD;CAchD,MAAM,OAAO,IAAe,GAAkB;EAC1C,IAAM,CAAC,GAAI,KAAM,KAAK;EACtB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAM,KAAK;GAC3B,IAAM,IAAS,EAAgB,EAAe,GAAI,CAAE,GAAG;IAAC;IAAG;IAAG;IAAI;GAAE,CAAC;GACrE,MAAM,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,YAAY,EAAO,CAAC;EACjE;CACJ;CASA,IAAY,YAA4B;EACpC,IAAI,KAAK,mBAAmB,MAAM;GAC9B,IAAM,CAAC,GAAI,KAAM,KAAK;GACtB,KAAK,iBAAiB,IAAI,EAAe,GAAI,GAAI,KAAK,OAAO,KAAK,IAAI;EAC1E;EACA,OAAO,KAAK;CAChB;CAUA,aAAa,OAAO,GAAoB,IAA6B,CAAC,GAAwB;EAC1F,IAAM,IAAU,MAAM,EAAW,OAAO,GAAO,CAAO,GAChD,I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AU,MAAM,kCAAkC;EAEtD,IAAM,IAAM,EAAQ;EACpB,IAAI,MAAQ,KAAA,GACR,MAAU,MACN,2BAA2B,EAAgB,4BAC/C;EAEJ,IAAM,IAAY,KAAK,aAAa,EAAI,IAAoB,GAEtD,EAAE,YAAS,cAAW,EAAK,GAAW,EAAQ,QAAQ,IAAI,GAC1D,IAAoC,CAAC;EAC3C,KAAK,IAAI,IAAI,GAAG,IAAI,EAAQ,QAAQ,KAAK;GACrC,IAAM,IAAK,EAAQ,IACb,IAAY,KAAK,QAAQ,MAAO,SAAS;GAC/C,EAAc,KAAK;IACf,SAAS;IACT;IACA,aAAa,EAAO;IACpB,KAAK;IACL,MAAM;IACN,MAAM,EAAO;GACjB,CAAC;EACL;EACA,IAAI,EAAc,WAAW,GACzB,MAAU,MAAM,sDAAsD;EAG1E,IAAM,IAAM,EAAc,IACpB,IAA+B;GACjC,SAAS,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,EAAE,YAAS,KAAK,UAAU,IAAI,CAAK;EACzC,OAAO,EAAgB,GAAM;GAAC;GAAG;GAAG;GAAI;EAAE,CAAC;CAC/C;CAEA,aAAqB,GAAiC;EAClD,OAAO,KAAK,gBAAgB,EAAQ,CAAG,IAAI,IAAI,aAAa,CAAG;CACnE;AACJ"}
1
+ {"version":3,"file":"classifier.js","names":[],"sources":["../../../src/vision/tasks/classifier.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Image classification task using ONNX Runtime Web.\n */\n\nimport type * as ort from \"onnxruntime-web\";\n\nimport { type ModelSource, type OrtSessionOptions, OrtSession } from \"../core/session\";\nimport { SpeedTimer } from \"../core/timing\";\nimport { type ImageInput, loadImageSource } from \"../io/image\";\nimport { classificationNumClasses, resolveInputSize } from \"../core/graph\";\nimport { modelNames } from \"../core/metadata\";\nimport { type LabelSpec, resolveLabels } from \"../labels\";\nimport {\n type Normalization,\n isUltralyticsClassifier,\n resolveNormalization,\n} from \"../normalization\";\nimport { softmax, topK } from \"../postprocess/classification\";\nimport { asFloat32Array } from \"../core/dtypes\";\nimport { toFloat32Tensor } from \"../preprocess/image\";\nimport { ResizePipeline, zeroTensorData } from \"../preprocess/pipeline\";\nimport { ClassificationResults, Probs } from \"../results\";\nimport { VisionTask } from \"./base\";\nimport { type ClassProbability, type ClassificationResult, type RGBImage } from \"../types\";\n\nexport interface ClassifierOptions extends OrtSessionOptions {\n /**\n * Class label spec — see {@link resolveLabels}.\n *\n * Optional: when omitted, the names the export baked into the model are used\n * (Ultralytics writes them as `names` in the metadata map). Only when the\n * model carries none does this fall back to generated `class_<id>` labels.\n * Passing a spec always wins, for a model whose names are wrong or absent.\n */\n readonly labels?: LabelSpec;\n /**\n * Number of classes the model can predict.\n *\n * Optional: inferred from the classification head's declared output shape\n * `(B, nc)`. Pass it to validate that the supplied labels match the model.\n */\n readonly numClasses?: number;\n /**\n * Model input `[width, height]` in pixels.\n *\n * Only used when the model's graph leaves its spatial axes dynamic: a graph\n * that declares a static size always wins, since that is the only shape ONNX\n * Runtime will accept. Defaults to `[224, 224]`.\n */\n readonly inputSize?: readonly [number, number];\n /**\n * Which preprocessing this model expects — see {@link Normalization}.\n *\n * Defaults to `\"auto\"`, which reads the model's own export metadata and picks\n * `\"ultralytics\"` (raw `[0, 1]`) for an Ultralytics classification head and\n * `\"imagenet\"` for everything else.\n */\n readonly normalization?: Normalization;\n /** Per-channel RGB mean, overriding the preset. Defaults to the preset's. */\n readonly mean?: readonly [number, number, number];\n /** Per-channel RGB standard deviation, overriding the preset. */\n readonly std?: readonly [number, number, number];\n /**\n * Whether the model's output still needs a softmax.\n *\n * Left undefined (the default), this reads the model's metadata and answers\n * `false` for an Ultralytics classification export, whose graph already ends\n * in one — applying a second softmax to a probability vector keeps the\n * ranking but flattens the confidences, so the top-1 stays right while every\n * number attached to it is wrong. Detection covers that family; for any other\n * model that already emits probabilities, pass `false` explicitly.\n */\n readonly applySoftmax?: boolean;\n}\n\nexport interface ClassifierPredictOptions {\n /**\n * If set, the per-class probability list in `results[0].result.probabilities`\n * is truncated to the top-K entries. The bulk `probs` view always exposes\n * the full vector.\n */\n readonly topK?: number;\n}\n\n/**\n * Image classifier wrapping an ONNX model with ImageNet-style preprocessing.\n *\n * `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a\n * single image), mirroring Ultralytics' API. The envelope exposes a `probs`\n * collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the\n * legacy per-class probability list with names already resolved.\n *\n * Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,\n * NCHW layout, batch size 1, softmax applied to the raw output.\n *\n * @example\n * ```typescript\n * const clf = await Classifier.create(\"/models/resnet50.onnx\", {\n * labels: [\"tench\", \"goldfish\", ...] // 1000 ImageNet labels\n * });\n * const r = (await clf.predict(\"/images/dog.jpg\"))[0];\n * console.log(r.cls, r.conf, r.name);\n * console.log(r.probs.top5, r.probs.top5conf);\n * ```\n */\nexport class Classifier extends VisionTask {\n private constructor(\n session: OrtSession,\n private readonly _labels: readonly string[],\n private readonly _names: Readonly<Record<number, string>>,\n private readonly _inputSize: readonly [number, number],\n private readonly _mean: readonly [number, number, number],\n private readonly _std: readonly [number, number, number],\n private readonly _applySoftmax: boolean,\n private readonly _normalization: string,\n ) {\n super(session);\n }\n\n /**\n * Whether a softmax is applied to the model's output before ranking.\n *\n * Resolved once at construction. Worth reading when confidences look\n * compressed: a second softmax over an already-normalized vector leaves the\n * ordering intact and the numbers meaningless, which is invisible to any check\n * that only looks at the predicted class.\n */\n get appliesSoftmax(): boolean {\n return this._applySoftmax;\n }\n\n /**\n * Which preprocessing this classifier applies to every image.\n *\n * One of the {@link Normalization} preset names, or `\"custom\"` when the caller\n * supplied `mean`/`std` directly. Worth reading when a model underperforms:\n * feeding a classifier a differently prepared tensor than it was trained on\n * degrades it without throwing anything, so \"what does this assume\" is the\n * first question.\n */\n get normalization(): string {\n return this._normalization;\n }\n\n private _pipelineCache: ResizePipeline | null = null;\n\n /**\n * Run the model once on a zero tensor, paying one-time costs up front.\n *\n * The first inference of a session is not representative: WebGPU compiles its\n * shaders on it and the WASM backend faults in its arenas. Calling this while\n * a loading spinner is still up moves that cost somewhere the user is already\n * waiting — which matters most for a classifier running as the last step of\n * an on-device analysis, where the delay lands right before the answer shows.\n *\n * @param runs How many warm-up inferences to run. One is enough for WASM;\n * WebGPU sometimes settles on the second.\n */\n async warmup(runs: number = 1): Promise<void> {\n const [tw, th] = this._inputSize;\n for (let i = 0; i < runs; i++) {\n const tensor = toFloat32Tensor(zeroTensorData(tw, th), [1, 3, th, tw]);\n await this._session.run({ [this._session.inputName]: tensor });\n }\n }\n\n /**\n * The fused preprocessing pipeline, built on first use.\n *\n * Lazily, because constructing it reserves the output buffer: a task built in\n * an environment without a canvas implementation stays constructible, and only\n * fails if it is actually asked to preprocess something.\n */\n private get _pipeline(): ResizePipeline {\n if (this._pipelineCache === null) {\n const [tw, th] = this._inputSize;\n this._pipelineCache = new ResizePipeline(tw, th, this._mean, this._std);\n }\n return this._pipelineCache;\n }\n\n /**\n * Load the model, resolve labels, and settle the preprocessing.\n *\n * @param model The model source — a URL, or the bytes.\n * @param options Labels, input size, normalization, and session options.\n * @throws {RangeError} If `normalization` names an unknown preset, or names\n * one while `mean`/`std` are also given.\n */\n static async create(model: ModelSource, options: ClassifierOptions = {}): Promise<Classifier> {\n const session = await OrtSession.create(model, options);\n const normalization = resolveNormalization(session.metadata, {\n normalization: options.normalization,\n mean: options.mean,\n std: options.std,\n });\n const numClasses =\n options.numClasses ?? classificationNumClasses(session.outputShape) ?? undefined;\n const labels = resolveLabels(options.labels ?? modelNames(session.metadata), {\n numClasses,\n });\n const names: Record<number, string> = {};\n for (let i = 0; i < labels.length; i++) {\n names[i] = labels[i] as string;\n }\n return new Classifier(\n session,\n labels,\n names,\n resolveInputSize({\n graphShape: session.inputShape,\n requested: options.inputSize,\n fallback: [224, 224],\n }),\n normalization.mean,\n normalization.std,\n options.applySoftmax ?? !isUltralyticsClassifier(session.metadata),\n normalization.name,\n );\n }\n\n /** Class labels indexed by class id. */\n get labels(): readonly string[] {\n return this._labels;\n }\n\n /** Class id → class name dict (matches Ultralytics' `model.names`). */\n get names(): Readonly<Record<number, string>> {\n return this._names;\n }\n\n /**\n * The `[width, height]` this task preprocesses to.\n *\n * Resolved at creation time from the model's graph when it declares a static\n * input, so reading it back tells you the resolution inference really runs at\n * — not merely what was requested.\n */\n get inputSize(): readonly [number, number] {\n return this._inputSize;\n }\n\n /** Number of classes the model can predict. */\n get numClasses(): number {\n return this._labels.length;\n }\n\n /** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */\n async call(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n return this.predict(image, options);\n }\n\n /** Run classification on a single image. */\n async predict(\n image: ImageInput,\n options: ClassifierPredictOptions = {},\n ): Promise<ClassificationResults[]> {\n const timer = new SpeedTimer();\n const path = typeof image === \"string\" ? image : null;\n const { image: original, canvas } = await loadImageSource(image);\n timer.stage(\"load\");\n const { tensor, reused } = this._preprocess(original, canvas);\n timer.stage(\"preprocess\");\n let outputs: Record<string, ort.Tensor>;\n try {\n outputs = await this._session.run({ [this._session.inputName]: tensor });\n } finally {\n this._pipeline.release({ reused });\n }\n timer.stage(\"inference\");\n const firstOutputName = this._session.outputNames[0];\n if (firstOutputName === undefined) {\n throw new Error(\"Classifier model has no outputs.\");\n }\n const raw = outputs[firstOutputName];\n if (raw === undefined) {\n throw new Error(\n `Classifier model output ${firstOutputName} missing from run() result.`,\n );\n }\n const fullProbs = this._postprocess(asFloat32Array(raw.data));\n\n const { indices, values } = topK(fullProbs, options.topK ?? null);\n const probabilities: ClassProbability[] = [];\n for (let i = 0; i < indices.length; i++) {\n const id = indices[i] as number;\n const className = this._labels[id] ?? `class_${id}`;\n probabilities.push({\n classId: id,\n className,\n probability: values[i] as number,\n cls: id,\n name: className,\n conf: values[i] as number,\n });\n }\n if (probabilities.length === 0) {\n throw new Error(\"Classifier produced no probabilities (empty output).\");\n }\n\n const top = probabilities[0] as ClassProbability;\n const result: ClassificationResult = {\n classId: top.classId,\n className: top.className,\n confidence: top.probability,\n cls: top.classId,\n name: top.className,\n conf: top.probability,\n image: original,\n probabilities,\n };\n\n const orig: readonly [number, number] = [original.height, original.width];\n const probs = new Probs(fullProbs);\n timer.stage(\"postprocess\");\n return [\n new ClassificationResults(\n probs,\n result,\n this._names,\n original,\n orig,\n path,\n timer.speed(),\n ),\n ];\n }\n\n /**\n * Resize and pack the image into the tensor the model expects.\n *\n * @param image The decoded input.\n * @param canvas The opaque canvas it was decoded on, when there is one — see\n * {@link ResizePipeline.run}.\n */\n private _preprocess(\n image: RGBImage,\n canvas: CanvasImageSource | null,\n ): { tensor: ort.Tensor; reused: boolean } {\n const [tw, th] = this._inputSize;\n const { data, reused } = this._pipeline.run(image, canvas);\n return { tensor: toFloat32Tensor(data, [1, 3, th, tw]), reused };\n }\n\n private _postprocess(raw: Float32Array): Float32Array {\n return this._applySoftmax ? softmax(raw) : new Float32Array(raw);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AA0GA,IAAa,IAAb,MAAa,UAAmB,EAAW;CAGlB;CACA;CACA;CACA;CACA;CACA;CACA;CARrB,YACI,GACA,GACA,GACA,GACA,GACA,GACA,GACA,GACF;EADmB,AAEjB,MAAM,CAAO,GARI,KAAA,UAAA,GACA,KAAA,SAAA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AAK;IACf,SAAS;IACT;IACA,aAAa,EAAO;IACpB,KAAK;IACL,MAAM;IACN,MAAM,EAAO;GACjB,CAAC;EACL;EACA,IAAI,EAAc,WAAW,GACzB,MAAU,MAAM,sDAAsD;EAG1E,IAAM,IAAM,EAAc,IACpB,IAA+B;GACjC,SAAS,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;CASA,YACI,GACA,GACuC;EACvC,IAAM,CAAC,GAAI,KAAM,KAAK,YAChB,EAAE,SAAM,cAAW,KAAK,UAAU,IAAI,GAAO,CAAM;EACzD,OAAO;GAAE,QAAQ,EAAgB,GAAM;IAAC;IAAG;IAAG;IAAI;GAAE,CAAC;GAAG;EAAO;CACnE;CAEA,aAAqB,GAAiC;EAClD,OAAO,KAAK,gBAAgB,EAAQ,CAAG,IAAI,IAAI,aAAa,CAAG;CACnE;AACJ"}
@@ -1,2 +1,2 @@
1
- const e=require("../core/exceptions.cjs"),t=require("../types.cjs"),n=require("../core/timing.cjs"),r=require("../results.cjs"),i=require("../labels.cjs"),a=require("../core/session.cjs"),o=require("../io/image.cjs"),s=require("../fusion.cjs"),c=require("../preprocess/image.cjs"),l=require("../preprocess/pipeline.cjs"),u=require("../postprocess/classification.cjs"),d=require("./base.cjs");var f=class f extends d.VisionTask{_spec;_labels;_names;_classifierLabels;_classifierNames;_raiseOnEmpty;constructor(e,t,n,r,i,a,o){super(e),this._spec=t,this._labels=n,this._names=r,this._classifierLabels=i,this._classifierNames=a,this._raiseOnEmpty=o}_pipelineCache=null;async warmup(e=1){let[t,n]=this._spec.inputSize;for(let r=0;r<e;r++){let e={[s.INPUT_IMAGE]:c.toFloat32Tensor(l.zeroTensorData(t,n),[1,3,n,t])};this._spec.needsSourceImage&&(e[s.INPUT_SOURCE]=c.toFloat32Tensor(l.zeroTensorData(t,n),[1,3,n,t]),e[s.INPUT_SCALE]=c.toFloat32Tensor(new Float32Array([1]),[1]),e[s.INPUT_PAD]=c.toFloat32Tensor(new Float32Array([0,0]),[2])),await this._session.run(e)}}get _pipeline(){if(this._pipelineCache===null){let[e,t]=this._spec.inputSize;this._pipelineCache=new l.LetterboxPipeline(e,t)}return this._pipelineCache}static async create(t,n={}){let r=await a.OrtSession.create(t,n),o=s.readFusionSpec(r.metadata);if(o===null)throw new e.FusionError(`This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a plain model with Detector/Classifier instead.`);let c=i.resolveLabels(n.labels??o.detectorNames??`coco`),l=i.resolveLabels(n.classifierLabels??o.classifierNames,{numClasses:p(r)??void 0});return new f(r,o,c,m(c),l,m(l),n.raiseOnEmpty??!1)}get spec(){return this._spec}get inputSize(){return this._spec.inputSize}get labels(){return this._labels}get names(){return this._names}get classifierLabels(){return this._classifierLabels}get classifierNames(){return this._classifierNames}async call(e,t={}){return this.predict(e,t)}async predict(e,t={}){let i=new n.SpeedTimer,a=typeof e==`string`?e:null,c=await o.loadImage(e);i.stage(`load`);let{feeds:l,scale:u,padLeft:f,padTop:p}=this._preprocess(c);i.stage(`preprocess`);let m=await this._session.run(l);this._pipeline.release(),i.stage(`inference`);let v=h(m,s.OUTPUT_PROBS),S=g(m,s.OUTPUT_BOXES),C=g(m,s.OUTPUT_SCORES),w=_(m,s.OUTPUT_CLASSES),T=v.data,E=_(m,`num_detections`)[0]??0,D=Math.min(E,Math.floor(S.length/4)),O=v.dims[v.dims.length-1]??0,k=t.classes===void 0?null:new Set(t.classes),A=t.confThreshold??0,j=[];for(let e=0;e<D;e++){let n=w[e]??0,r=C[e]??0;if(r<A||k!==null&&!k.has(n))continue;let i=this._toOriginal(S,e,{scale:u,padLeft:f,padTop:p,original:c}),a=y(c,i);j.push(b(n,this._names[n]??`class_${n}`,r,i,a,this._classify(T.subarray(e*O,(e+1)*O),a,t.topK)))}d.requireDetections(j.length,{raiseOnEmpty:t.raiseOnEmpty??this._raiseOnEmpty,confThreshold:Math.max(A,this._spec.confThreshold),classes:t.classes,path:a});let M=[c.height,c.width];return i.stage(`postprocess`),[new r.DetectClassifyResults(x(j,M),j,this._names,this._classifierNames,c,M,a,i.speed())]}_preprocess(e){let[t,n]=this._spec.inputSize,r=this._pipeline.run(e),i={[s.INPUT_IMAGE]:c.toFloat32Tensor(r.data,[1,3,n,t])};return this._spec.needsSourceImage&&(i[s.INPUT_SOURCE]=v(e),i[s.INPUT_SCALE]=c.toFloat32Tensor(new Float32Array([r.scale]),[1]),i[s.INPUT_PAD]=c.toFloat32Tensor(new Float32Array([r.padLeft,r.padTop]),[2])),{feeds:i,scale:r.scale,padLeft:r.padLeft,padTop:r.padTop}}_toOriginal(e,n,r){let{scale:i,padLeft:a,padTop:o,original:s}=r,c=t=>e[n*4+t]??0,l=e=>Math.min(Math.max(e,0),s.width),u=e=>Math.min(Math.max(e,0),s.height);return new t.BoundingBox(l((c(0)-a)/i),u((c(1)-o)/i),l((c(2)-a)/i),u((c(3)-o)/i))}_classify(e,t,n){let r=this._spec.applySoftmax?u.softmax(e):e,{indices:i,values:a}=u.topK(r,n??null),o=[];for(let e=0;e<i.length;e++){let t=i[e]??0,n=a[e]??0,r=this._classifierLabels[t]??`class_${t}`;o.push({classId:t,className:r,probability:n,cls:t,name:r,conf:n})}let s=o[0]??{classId:0,className:`class_0`,probability:0,cls:0,name:`class_0`,conf:0};return{classId:s.classId,className:s.className,confidence:s.probability,cls:s.classId,name:s.className,conf:s.probability,image:t,probabilities:o}}};function p(e){let t=e.outputNames.indexOf(s.OUTPUT_PROBS);if(t<0)return null;let n=e.outputShapes[t];return n===void 0||n.length===0?null:n[n.length-1]??null}function m(e){let t={};for(let n=0;n<e.length;n++)t[n]=e[n];return t}function h(t,n){let r=t[n];if(r===void 0)throw new e.FusionError(`The fused pipeline is missing its '${n}' output.`);return r}function g(e,t){return h(e,t).data}function _(e,t){let n=h(e,t).data,r=[];for(let e=0;e<n.length;e++)r.push(Number(n[e]));return r}function v(e){let t=c.toCHW(c.toFloat32(e),e.width,e.height,3);return c.toFloat32Tensor(t,[1,3,e.height,e.width])}function y(e,n){let[r,i,a,o]=n.asIntXyxy(),s=Math.max(0,r),c=Math.max(0,i),l=Math.min(e.width,a),u=Math.min(e.height,o);if(l<=s||u<=c)return new t.RGBImage(new Uint8Array,0,0);let d=l-s,f=u-c,p=new Uint8Array(d*f*3);for(let t=0;t<f;t++){let n=((c+t)*e.width+s)*3;p.set(e.data.subarray(n,n+d*3),t*d*3)}return new t.RGBImage(p,d,f)}function b(e,t,n,r,i,a){return{classId:e,className:t,confidence:n,bbox:r,cls:e,name:t,conf:n,box:r,croppedImage:i,classification:a}}function x(e,t){let n=new Float32Array(e.length*4),i=new Int32Array(e.length),a=new Float32Array(e.length);return e.forEach((e,t)=>{let[r,o,s,c]=e.bbox.xyxy;n[t*4]=r,n[t*4+1]=o,n[t*4+2]=s,n[t*4+3]=c,i[t]=e.classId,a[t]=e.confidence}),new r.Boxes(n,i,a,t)}exports.DetectClassify=f;
1
+ const e=require("../core/exceptions.cjs"),t=require("../types.cjs"),n=require("../core/timing.cjs"),r=require("../results.cjs"),i=require("../labels.cjs"),a=require("../core/dtypes.cjs"),o=require("../core/session.cjs"),s=require("../io/image.cjs"),c=require("../fusion.cjs"),l=require("../preprocess/image.cjs"),u=require("../preprocess/pipeline.cjs"),d=require("../postprocess/classification.cjs"),f=require("./base.cjs");var p=class p extends f.VisionTask{_spec;_labels;_names;_classifierLabels;_classifierNames;_raiseOnEmpty;constructor(e,t,n,r,i,a,o){super(e),this._spec=t,this._labels=n,this._names=r,this._classifierLabels=i,this._classifierNames=a,this._raiseOnEmpty=o}_pipelineCache=null;async warmup(e=1){let[t,n]=this._spec.inputSize;for(let r=0;r<e;r++){let e={[c.INPUT_IMAGE]:l.toFloat32Tensor(u.zeroTensorData(t,n),[1,3,n,t])};this._spec.needsSourceImage&&(e[c.INPUT_SOURCE]=l.toFloat32Tensor(u.zeroTensorData(t,n),[1,3,n,t]),e[c.INPUT_SCALE]=l.toFloat32Tensor(new Float32Array([1]),[1]),e[c.INPUT_PAD]=l.toFloat32Tensor(new Float32Array([0,0]),[2])),await this._session.run(e)}}get _pipeline(){if(this._pipelineCache===null){let[e,t]=this._spec.inputSize;this._pipelineCache=new u.LetterboxPipeline(e,t)}return this._pipelineCache}static async create(t,n={}){let r=await o.OrtSession.create(t,n),a=c.readFusionSpec(r.metadata);if(a===null)throw new e.FusionError(`This model carries no fused-pipeline metadata, so DetectClassify cannot tell how to drive it. Build one with ort_vision_sdk.compose.fuse_detect_classify, or load a plain model with Detector/Classifier instead.`);let s=i.resolveLabels(n.labels??a.detectorNames??`coco`),l=i.resolveLabels(n.classifierLabels??a.classifierNames,{numClasses:m(r)??void 0});return new p(r,a,s,h(s),l,h(l),n.raiseOnEmpty??!1)}get spec(){return this._spec}get inputSize(){return this._spec.inputSize}get labels(){return this._labels}get names(){return this._names}get classifierLabels(){return this._classifierLabels}get classifierNames(){return this._classifierNames}async call(e,t={}){return this.predict(e,t)}async predict(e,t={}){let i=new n.SpeedTimer,o=typeof e==`string`?e:null,{image:l,canvas:u}=await s.loadImageSource(e);i.stage(`load`);let{feeds:d,scale:p,padLeft:m,padTop:h,reused:y}=this._preprocess(l,u);i.stage(`preprocess`);let C;try{C=await this._session.run(d)}finally{this._pipeline.release({reused:y})}i.stage(`inference`);let w=g(C,c.OUTPUT_PROBS),T=_(C,c.OUTPUT_BOXES),E=_(C,c.OUTPUT_SCORES),D=v(C,c.OUTPUT_CLASSES),O=a.asFloat32Array(w.data),k=v(C,`num_detections`)[0]??0,A=this._spec.hasMasks?_(C,c.OUTPUT_MASKS):null,[j,M]=this._spec.cropSize,N=Math.min(k,Math.floor(T.length/4)),P=w.dims[w.dims.length-1]??0,F=t.classes===void 0?null:new Set(t.classes),I=t.confThreshold??0,L=[];for(let e=0;e<N;e++){let n=D[e]??0,r=E[e]??0;if(r<I||F!==null&&!F.has(n))continue;let i=this._toOriginal(T,e,{scale:p,padLeft:m,padTop:h,original:l}),a=f.cropToBox(l,i);L.push(b(n,this._names[n]??`class_${n}`,r,i,a,this._classify(O.subarray(e*P,(e+1)*P),a,t.topK),A===null?null:x(A,e,j,M,a)))}f.requireDetections(L.length,{raiseOnEmpty:t.raiseOnEmpty??this._raiseOnEmpty,confThreshold:Math.max(I,this._spec.confThreshold),classes:t.classes,path:o});let R=[l.height,l.width];return i.stage(`postprocess`),[new r.DetectClassifyResults(S(L,R),L,this._names,this._classifierNames,l,R,o,i.speed())]}_preprocess(e,t){let[n,r]=this._spec.inputSize,i=this._pipeline.run(e,t),a={[c.INPUT_IMAGE]:l.toFloat32Tensor(i.data,[1,3,r,n])};return this._spec.needsSourceImage&&(a[c.INPUT_SOURCE]=y(e),a[c.INPUT_SCALE]=l.toFloat32Tensor(new Float32Array([i.scale]),[1]),a[c.INPUT_PAD]=l.toFloat32Tensor(new Float32Array([i.padLeft,i.padTop]),[2])),{feeds:a,scale:i.scale,padLeft:i.padLeft,padTop:i.padTop,reused:i.reused}}_toOriginal(e,n,r){let{scale:i,padLeft:a,padTop:o,original:s}=r,c=t=>e[n*4+t]??0,l=e=>Math.min(Math.max(e,0),s.width),u=e=>Math.min(Math.max(e,0),s.height);return new t.BoundingBox(l((c(0)-a)/i),u((c(1)-o)/i),l((c(2)-a)/i),u((c(3)-o)/i))}_classify(e,t,n){let r=this._spec.applySoftmax?d.softmax(e):e,{indices:i,values:a}=d.topK(r,n??null),o=[];for(let e=0;e<i.length;e++){let t=i[e]??0,n=a[e]??0,r=this._classifierLabels[t]??`class_${t}`;o.push({classId:t,className:r,probability:n,cls:t,name:r,conf:n})}let s=o[0]??{classId:0,className:`class_0`,probability:0,cls:0,name:`class_0`,conf:0};return{classId:s.classId,className:s.className,confidence:s.probability,cls:s.classId,name:s.className,conf:s.probability,image:t,probabilities:o}}};function m(e){let t=e.outputNames.indexOf(c.OUTPUT_PROBS);if(t<0)return null;let n=e.outputShapes[t];return n===void 0||n.length===0?null:n[n.length-1]??null}function h(e){let t={};for(let n=0;n<e.length;n++)t[n]=e[n];return t}function g(t,n){let r=t[n];if(r===void 0)throw new e.FusionError(`The fused pipeline is missing its '${n}' output.`);return r}function _(e,t){return a.asFloat32Array(g(e,t).data)}function v(e,t){let n=g(e,t).data,r=[];for(let e=0;e<n.length;e++)r.push(Number(n[e]));return r}function y(e){let t=l.toCHW(l.toFloat32(e),e.width,e.height,3);return l.toFloat32Tensor(t,[1,3,e.height,e.width])}function b(e,t,n,r,i,a,o){return{classId:e,className:t,confidence:n,bbox:r,cls:e,name:t,conf:n,box:r,croppedImage:i,classification:a,mask:o}}function x(e,n,r,i,a){let{width:o,height:s}=a;if(o<1||s<1)return null;let c=r*i,l=e.subarray(n*c,(n+1)*c),u=new Uint8Array(o*s);for(let e=0;e<s;e++){let t=Math.min(Math.floor(e*i/s),i-1);for(let n=0;n<o;n++){let i=Math.min(Math.floor(n*r/o),r-1);u[e*o+n]=(l[t*r+i]??0)>.5?255:0}}return new t.Mask(u,o,s)}function S(e,t){let n=new Float32Array(e.length*4),i=new Int32Array(e.length),a=new Float32Array(e.length);return e.forEach((e,t)=>{let[r,o,s,c]=e.bbox.xyxy;n[t*4]=r,n[t*4+1]=o,n[t*4+2]=s,n[t*4+3]=c,i[t]=e.classId,a[t]=e.confidence}),new r.Boxes(n,i,a,t)}exports.DetectClassify=p,exports.maskToBox=x;
2
2
  //# sourceMappingURL=detectClassify.cjs.map