@mauriciobenjamin700/ort-vision-sdk-web 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/LICENSE +21 -0
  3. package/README.md +90 -0
  4. package/dist/core/canvas.d.ts +21 -0
  5. package/dist/core/canvas.d.ts.map +1 -0
  6. package/dist/core/canvas.js +58 -0
  7. package/dist/core/canvas.js.map +1 -0
  8. package/dist/core/exceptions.d.ts +25 -0
  9. package/dist/core/exceptions.d.ts.map +1 -0
  10. package/dist/core/exceptions.js +28 -0
  11. package/dist/core/exceptions.js.map +1 -0
  12. package/dist/core/providers.d.ts +21 -0
  13. package/dist/core/providers.d.ts.map +1 -0
  14. package/dist/core/providers.js +29 -0
  15. package/dist/core/providers.js.map +1 -0
  16. package/dist/core/session.d.ts +47 -0
  17. package/dist/core/session.d.ts.map +1 -0
  18. package/dist/core/session.js +86 -0
  19. package/dist/core/session.js.map +1 -0
  20. package/dist/index.d.ts +21 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +21 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/io/image.d.ts +17 -0
  25. package/dist/io/image.d.ts.map +1 -0
  26. package/dist/io/image.js +104 -0
  27. package/dist/io/image.js.map +1 -0
  28. package/dist/labels.d.ts +38 -0
  29. package/dist/labels.d.ts.map +1 -0
  30. package/dist/labels.js +71 -0
  31. package/dist/labels.js.map +1 -0
  32. package/dist/postprocess/classification.d.ts +16 -0
  33. package/dist/postprocess/classification.d.ts.map +1 -0
  34. package/dist/postprocess/classification.js +46 -0
  35. package/dist/postprocess/classification.js.map +1 -0
  36. package/dist/postprocess/detection.d.ts +112 -0
  37. package/dist/postprocess/detection.d.ts.map +1 -0
  38. package/dist/postprocess/detection.js +283 -0
  39. package/dist/postprocess/detection.js.map +1 -0
  40. package/dist/postprocess/segmentation.d.ts +61 -0
  41. package/dist/postprocess/segmentation.d.ts.map +1 -0
  42. package/dist/postprocess/segmentation.js +184 -0
  43. package/dist/postprocess/segmentation.js.map +1 -0
  44. package/dist/preprocess/image.d.ts +71 -0
  45. package/dist/preprocess/image.d.ts.map +1 -0
  46. package/dist/preprocess/image.js +171 -0
  47. package/dist/preprocess/image.js.map +1 -0
  48. package/dist/results.d.ts +182 -0
  49. package/dist/results.d.ts.map +1 -0
  50. package/dist/results.js +321 -0
  51. package/dist/results.js.map +1 -0
  52. package/dist/tasks/base.d.ts +14 -0
  53. package/dist/tasks/base.d.ts.map +1 -0
  54. package/dist/tasks/base.js +17 -0
  55. package/dist/tasks/base.js.map +1 -0
  56. package/dist/tasks/classifier.d.ts +82 -0
  57. package/dist/tasks/classifier.d.ts.map +1 -0
  58. package/dist/tasks/classifier.js +135 -0
  59. package/dist/tasks/classifier.js.map +1 -0
  60. package/dist/tasks/detector.d.ts +102 -0
  61. package/dist/tasks/detector.d.ts.map +1 -0
  62. package/dist/tasks/detector.js +189 -0
  63. package/dist/tasks/detector.js.map +1 -0
  64. package/dist/tasks/segmenter.d.ts +105 -0
  65. package/dist/tasks/segmenter.d.ts.map +1 -0
  66. package/dist/tasks/segmenter.js +242 -0
  67. package/dist/tasks/segmenter.js.map +1 -0
  68. package/dist/types.d.ts +182 -0
  69. package/dist/types.d.ts.map +1 -0
  70. package/dist/types.js +147 -0
  71. package/dist/types.js.map +1 -0
  72. package/package.json +71 -0
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Detection head postprocessing: anchor-free YOLO decoding + non-maximum suppression.
3
+ *
4
+ * The shared {@link decodeYoloAnchors} helper does the per-anchor work that
5
+ * is identical for plain detection and segmentation (transpose, xywh→xyxy,
6
+ * letterbox unmap, per-class NMS, sort & cap). {@link decodeYolo} is a thin
7
+ * wrapper around it; the segmentation module ({@link ./segmentation.js})
8
+ * calls the helper directly so it can also recover the per-anchor mask
9
+ * coefficients.
10
+ *
11
+ * Works for any YOLO export with the post-v8 anchor-free head:
12
+ * **YOLOv8 / v9 / v10 / v11 / v12** detect heads, all of which share the
13
+ * `[1, 4 + nc, N]` output layout.
14
+ */
15
+ import { BoundingBox } from "../types.js";
16
+ /**
17
+ * Greedy non-maximum suppression on axis-aligned bounding boxes.
18
+ *
19
+ * Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops
20
+ * any subsequent box whose IoU exceeds the threshold).
21
+ *
22
+ * @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.
23
+ * @param scores Detection score per box, length `N`.
24
+ * @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.
25
+ * @returns Indices of kept boxes, in descending score order.
26
+ */
27
+ export function nms(boxes, scores, iouThreshold) {
28
+ const n = scores.length;
29
+ if (n === 0)
30
+ return new Int32Array(0);
31
+ const areas = new Float32Array(n);
32
+ for (let i = 0; i < n; i++) {
33
+ const x1 = boxes[i * 4];
34
+ const y1 = boxes[i * 4 + 1];
35
+ const x2 = boxes[i * 4 + 2];
36
+ const y2 = boxes[i * 4 + 3];
37
+ areas[i] = Math.max(0, x2 - x1) * Math.max(0, y2 - y1);
38
+ }
39
+ const order = new Array(n);
40
+ for (let i = 0; i < n; i++)
41
+ order[i] = i;
42
+ order.sort((a, b) => scores[b] - scores[a]);
43
+ const suppressed = new Uint8Array(n);
44
+ const keep = [];
45
+ for (let oi = 0; oi < order.length; oi++) {
46
+ const i = order[oi];
47
+ if (suppressed[i])
48
+ continue;
49
+ keep.push(i);
50
+ const ax1 = boxes[i * 4];
51
+ const ay1 = boxes[i * 4 + 1];
52
+ const ax2 = boxes[i * 4 + 2];
53
+ const ay2 = boxes[i * 4 + 3];
54
+ const ai = areas[i];
55
+ for (let oj = oi + 1; oj < order.length; oj++) {
56
+ const j = order[oj];
57
+ if (suppressed[j])
58
+ continue;
59
+ const bx1 = boxes[j * 4];
60
+ const by1 = boxes[j * 4 + 1];
61
+ const bx2 = boxes[j * 4 + 2];
62
+ const by2 = boxes[j * 4 + 3];
63
+ const ix1 = Math.max(ax1, bx1);
64
+ const iy1 = Math.max(ay1, by1);
65
+ const ix2 = Math.min(ax2, bx2);
66
+ const iy2 = Math.min(ay2, by2);
67
+ const iw = Math.max(0, ix2 - ix1);
68
+ const ih = Math.max(0, iy2 - iy1);
69
+ const inter = iw * ih;
70
+ const union = ai + areas[j] - inter;
71
+ const iou = union > 0 ? inter / union : 0;
72
+ if (iou > iouThreshold)
73
+ suppressed[j] = 1;
74
+ }
75
+ }
76
+ return Int32Array.from(keep);
77
+ }
78
+ /**
79
+ * Per-class NMS — boxes are suppressed only by other boxes of the same class.
80
+ *
81
+ * Mirrors `torchvision.ops.batched_nms`.
82
+ *
83
+ * @param boxes Flat array of length `4 * N` in xyxy order.
84
+ * @param scores Detection score per box, length `N`.
85
+ * @param idxs Class index per box, length `N`. Boxes with different `idxs`
86
+ * never suppress each other.
87
+ * @param iouThreshold IoU threshold for suppression within a class.
88
+ */
89
+ export function batchedNms(boxes, scores, idxs, iouThreshold) {
90
+ if (scores.length === 0)
91
+ return new Int32Array(0);
92
+ const byClass = new Map();
93
+ for (let i = 0; i < idxs.length; i++) {
94
+ const c = idxs[i];
95
+ const list = byClass.get(c);
96
+ if (list === undefined)
97
+ byClass.set(c, [i]);
98
+ else
99
+ list.push(i);
100
+ }
101
+ const keep = [];
102
+ for (const indices of byClass.values()) {
103
+ const m = indices.length;
104
+ const subBoxes = new Float32Array(m * 4);
105
+ const subScores = new Float32Array(m);
106
+ for (let k = 0; k < m; k++) {
107
+ const i = indices[k];
108
+ subBoxes[k * 4] = boxes[i * 4];
109
+ subBoxes[k * 4 + 1] = boxes[i * 4 + 1];
110
+ subBoxes[k * 4 + 2] = boxes[i * 4 + 2];
111
+ subBoxes[k * 4 + 3] = boxes[i * 4 + 3];
112
+ subScores[k] = scores[i];
113
+ }
114
+ const subKeep = nms(subBoxes, subScores, iouThreshold);
115
+ for (let k = 0; k < subKeep.length; k++) {
116
+ keep.push(indices[subKeep[k]]);
117
+ }
118
+ }
119
+ keep.sort((a, b) => scores[b] - scores[a]);
120
+ return Int32Array.from(keep);
121
+ }
122
+ /**
123
+ * Shared YOLO per-anchor decode used by both detection and segmentation
124
+ * (v8 / v9 / v10 / v11 / v12).
125
+ *
126
+ * Only the first `4 + numClasses` channels are read; later channels (e.g.
127
+ * mask coefficients) are ignored — callers can fetch them via the returned
128
+ * {@link DecodedAnchors.anchorIndices}.
129
+ *
130
+ * @param data Flat per-anchor output, length `channels * numAnchors`.
131
+ * @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or
132
+ * `[1, 116, 8400]` (seg). The leading batch dim must be 1.
133
+ */
134
+ export function decodeYoloAnchors(data, dims, options) {
135
+ let normalized = dims;
136
+ if (normalized.length === 3) {
137
+ if (normalized[0] !== 1) {
138
+ throw new Error(`decodeYoloAnchors: expected batch size 1, got ${normalized[0]}.`);
139
+ }
140
+ normalized = [normalized[1], normalized[2]];
141
+ }
142
+ if (normalized.length !== 2) {
143
+ throw new Error(`decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(dims)}.`);
144
+ }
145
+ const channels = normalized[0];
146
+ const numAnchors = normalized[1];
147
+ const { numClasses, originalWidth, originalHeight, padLeft, padTop, scale, confThreshold, iouThreshold, maxDetections, } = options;
148
+ if (numClasses < 1 || numClasses + 4 > channels) {
149
+ throw new Error(`decodeYoloAnchors: invalid numClasses=${numClasses} for channels=${channels}.`);
150
+ }
151
+ if (data.length !== channels * numAnchors) {
152
+ throw new Error(`decodeYoloAnchors: data length ${data.length} does not match channels*numAnchors=${channels * numAnchors}.`);
153
+ }
154
+ const candidates = [];
155
+ for (let a = 0; a < numAnchors; a++) {
156
+ let bestCls = 0;
157
+ let bestScore = -Infinity;
158
+ for (let c = 0; c < numClasses; c++) {
159
+ const s = data[(4 + c) * numAnchors + a];
160
+ if (s !== undefined && s > bestScore) {
161
+ bestScore = s;
162
+ bestCls = c;
163
+ }
164
+ }
165
+ if (bestScore < confThreshold)
166
+ continue;
167
+ const cx = data[a];
168
+ const cy = data[numAnchors + a];
169
+ const w = data[2 * numAnchors + a];
170
+ const h = data[3 * numAnchors + a];
171
+ let x1 = cx - w / 2;
172
+ let y1 = cy - h / 2;
173
+ let x2 = cx + w / 2;
174
+ let y2 = cy + h / 2;
175
+ x1 = (x1 - padLeft) / scale;
176
+ y1 = (y1 - padTop) / scale;
177
+ x2 = (x2 - padLeft) / scale;
178
+ y2 = (y2 - padTop) / scale;
179
+ x1 = Math.max(0, Math.min(originalWidth, x1));
180
+ y1 = Math.max(0, Math.min(originalHeight, y1));
181
+ x2 = Math.max(0, Math.min(originalWidth, x2));
182
+ y2 = Math.max(0, Math.min(originalHeight, y2));
183
+ candidates.push({ anchorIdx: a, x1, y1, x2, y2, classId: bestCls, confidence: bestScore });
184
+ }
185
+ if (candidates.length === 0)
186
+ return emptyDecoded();
187
+ // Build flat arrays then delegate to batchedNms — same algorithm as before
188
+ // but funnelled through the public per-class NMS helper.
189
+ const flatBoxes = new Float32Array(candidates.length * 4);
190
+ const scoresArr = new Float32Array(candidates.length);
191
+ const idxsArr = new Int32Array(candidates.length);
192
+ for (let i = 0; i < candidates.length; i++) {
193
+ const c = candidates[i];
194
+ flatBoxes[i * 4] = c.x1;
195
+ flatBoxes[i * 4 + 1] = c.y1;
196
+ flatBoxes[i * 4 + 2] = c.x2;
197
+ flatBoxes[i * 4 + 3] = c.y2;
198
+ scoresArr[i] = c.confidence;
199
+ idxsArr[i] = c.classId;
200
+ }
201
+ const kept = batchedNms(flatBoxes, scoresArr, idxsArr, iouThreshold);
202
+ if (kept.length === 0)
203
+ return emptyDecoded();
204
+ const limited = Array.from(kept).slice(0, maxDetections);
205
+ const k = limited.length;
206
+ const anchorIndices = new Int32Array(k);
207
+ const boxesXyxy = new Float32Array(k * 4);
208
+ const classIds = new Int32Array(k);
209
+ const confidences = new Float32Array(k);
210
+ for (let i = 0; i < k; i++) {
211
+ const c = candidates[limited[i]];
212
+ anchorIndices[i] = c.anchorIdx;
213
+ boxesXyxy[i * 4] = c.x1;
214
+ boxesXyxy[i * 4 + 1] = c.y1;
215
+ boxesXyxy[i * 4 + 2] = c.x2;
216
+ boxesXyxy[i * 4 + 3] = c.y2;
217
+ classIds[i] = c.classId;
218
+ confidences[i] = c.confidence;
219
+ }
220
+ return { anchorIndices, boxesXyxy, classIds, confidences };
221
+ }
222
+ function emptyDecoded() {
223
+ return {
224
+ anchorIndices: new Int32Array(0),
225
+ boxesXyxy: new Float32Array(0),
226
+ classIds: new Int32Array(0),
227
+ confidences: new Float32Array(0),
228
+ };
229
+ }
230
+ /**
231
+ * Decode an anchor-free YOLO detection output into a list of detections.
232
+ *
233
+ * Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.
234
+ *
235
+ * Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred
236
+ * from the channel count.
237
+ */
238
+ export function decodeYolo(output, outputDims, options) {
239
+ const channels = outputDims.length === 3 ? outputDims[1] : outputDims[0];
240
+ if (channels === undefined || channels < 5) {
241
+ throw new Error(`decodeYolo: invalid output channel count ${channels} (expected >= 5).`);
242
+ }
243
+ const numClasses = channels - 4;
244
+ const decoded = decodeYoloAnchors(output, outputDims, {
245
+ numClasses,
246
+ ...options,
247
+ });
248
+ const results = [];
249
+ for (let i = 0; i < decoded.classIds.length; i++) {
250
+ results.push({
251
+ bbox: new BoundingBox(decoded.boxesXyxy[i * 4], decoded.boxesXyxy[i * 4 + 1], decoded.boxesXyxy[i * 4 + 2], decoded.boxesXyxy[i * 4 + 3]),
252
+ classId: decoded.classIds[i],
253
+ confidence: decoded.confidences[i],
254
+ });
255
+ }
256
+ return results;
257
+ }
258
+ let _warnedDecodeYoloV8 = false;
259
+ let _warnedDecodeYoloV8Anchors = false;
260
+ /**
261
+ * @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the
262
+ * decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.3.0.
263
+ */
264
+ export function decodeYoloV8(output, outputDims, options) {
265
+ if (!_warnedDecodeYoloV8) {
266
+ _warnedDecodeYoloV8 = true;
267
+ console.warn("[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. " +
268
+ "The alias will be removed in 0.3.0.");
269
+ }
270
+ return decodeYolo(output, outputDims, options);
271
+ }
272
+ /**
273
+ * @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.3.0.
274
+ */
275
+ export function decodeYoloV8Anchors(data, dims, options) {
276
+ if (!_warnedDecodeYoloV8Anchors) {
277
+ _warnedDecodeYoloV8Anchors = true;
278
+ console.warn("[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. " +
279
+ "The alias will be removed in 0.3.0.");
280
+ }
281
+ return decodeYoloAnchors(data, dims, options);
282
+ }
283
+ //# sourceMappingURL=detection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"detection.js","sourceRoot":"","sources":["../../src/postprocess/detection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,GAAG,CACjB,KAAmB,EACnB,MAAoB,EACpB,YAAoB;IAEpB,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAEtC,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAW,CAAC;QAClC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAW,CAAC;QACtC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,GAA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CACV,8FAA8F;YAC5F,qCAAqC,CACxC,CAAC;IACJ,CAAC;IACD,OAAO,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Segmentation head postprocessing: YOLO instance-segmentation decoding.
3
+ *
4
+ * Compatible with YOLOv8-seg / YOLOv11-seg exports (and any later seg head
5
+ * sharing the layout). The model produces two output tensors:
6
+ *
7
+ * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the
8
+ * same per-anchor predictions as plain YOLO detection plus an extra
9
+ * `numMaskCoefs` (typically 32) channels of mask coefficients.
10
+ * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of "prototype"
11
+ * masks shared across all anchors.
12
+ *
13
+ * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)
14
+ * is delegated to {@link decodeYoloAnchors}; this module only handles the
15
+ * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and
16
+ * thresholding.
17
+ */
18
+ import { BoundingBox, Mask } from "../types.js";
19
+ export interface DecodeYoloSegOptions {
20
+ readonly numClasses: number;
21
+ /** Model input `[width, height]` (post-letterbox). */
22
+ readonly inputWidth: number;
23
+ readonly inputHeight: number;
24
+ /** Original image `[width, height]`. */
25
+ readonly originalWidth: number;
26
+ readonly originalHeight: number;
27
+ /** Letterbox horizontal padding in input-tensor pixels. */
28
+ readonly padLeft: number;
29
+ /** Letterbox vertical padding in input-tensor pixels. */
30
+ readonly padTop: number;
31
+ /** Letterbox scale factor. */
32
+ readonly scale: number;
33
+ readonly confThreshold: number;
34
+ readonly iouThreshold: number;
35
+ readonly maxDetections: number;
36
+ /** Probability cutoff applied to the soft mask. Defaults to `0.5`. */
37
+ readonly maskThreshold?: number;
38
+ }
39
+ export interface DecodedSegmentation {
40
+ readonly bbox: BoundingBox;
41
+ readonly classId: number;
42
+ readonly confidence: number;
43
+ /** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */
44
+ readonly mask: Mask;
45
+ }
46
+ /**
47
+ * Decode YOLO segmentation raw outputs into a list of segmented instances.
48
+ *
49
+ * Compatible with YOLOv8-seg / YOLOv11-seg.
50
+ *
51
+ * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.
52
+ * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.
53
+ * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.
54
+ * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.
55
+ */
56
+ export declare function decodeYoloSeg(perAnchorData: Float32Array, perAnchorDims: readonly number[], prototypeData: Float32Array, prototypeDims: readonly number[], options: DecodeYoloSegOptions): DecodedSegmentation[];
57
+ /** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.3.0. */
58
+ export declare function decodeYoloV8Seg(perAnchorData: Float32Array, perAnchorDims: readonly number[], prototypeData: Float32Array, prototypeDims: readonly number[], options: DecodeYoloSegOptions): DecodedSegmentation[];
59
+ /** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */
60
+ export type DecodeYoloV8SegOptions = DecodeYoloSegOptions;
61
+ //# sourceMappingURL=segmentation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"segmentation.d.ts","sourceRoot":"","sources":["../../src/postprocess/segmentation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,sDAAsD;IACtD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,wCAAwC;IACxC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC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(and any later seg head
5
+ * sharing the layout). The model produces two output tensors:
6
+ *
7
+ * - `output0` of shape `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — the
8
+ * same per-anchor predictions as plain YOLO detection plus an extra
9
+ * `numMaskCoefs` (typically 32) channels of mask coefficients.
10
+ * - `output1` of shape `(1, numMaskCoefs, maskH, maskW)` — a set of "prototype"
11
+ * masks shared across all anchors.
12
+ *
13
+ * The per-anchor decode (xywh→xyxy, undo letterbox, per-class NMS, sort & cap)
14
+ * is delegated to {@link decodeYoloAnchors}; this module only handles the
15
+ * mask-specific work: matmul against prototypes, sigmoid, bilinear resize and
16
+ * thresholding.
17
+ */
18
+ import { decodeYoloAnchors } from "./detection.js";
19
+ import { BoundingBox, Mask } from "../types.js";
20
+ /**
21
+ * Decode YOLO segmentation raw outputs into a list of segmented instances.
22
+ *
23
+ * Compatible with YOLOv8-seg / YOLOv11-seg.
24
+ *
25
+ * @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.
26
+ * @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.
27
+ * @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.
28
+ * @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.
29
+ */
30
+ export function decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options) {
31
+ // Strip batch dim from prototypes; perAnchor batch is validated by the helper.
32
+ let pDims = prototypeDims;
33
+ if (pDims.length === 4) {
34
+ if (pDims[0] !== 1) {
35
+ throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${pDims[0]}.`);
36
+ }
37
+ pDims = [pDims[1], pDims[2], pDims[3]];
38
+ }
39
+ if (pDims.length !== 3) {
40
+ throw new Error(`decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(prototypeDims)}.`);
41
+ }
42
+ const numMaskCoefs = pDims[0];
43
+ const maskH = pDims[1];
44
+ const maskW = pDims[2];
45
+ const channels = perAnchorDims.length === 3 ? perAnchorDims[1] : perAnchorDims[0];
46
+ const numAnchors = perAnchorDims.length === 3 ? perAnchorDims[2] : perAnchorDims[1];
47
+ if (channels === undefined || numAnchors === undefined) {
48
+ throw new Error(`decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(perAnchorDims)}.`);
49
+ }
50
+ const expectedChannels = 4 + options.numClasses + numMaskCoefs;
51
+ if (channels !== expectedChannels) {
52
+ throw new Error(`decodeYoloSeg: channels=${channels} does not match 4 + numClasses(${options.numClasses}) + numMaskCoefs(${numMaskCoefs}) = ${expectedChannels}.`);
53
+ }
54
+ if (prototypeData.length !== numMaskCoefs * maskH * maskW) {
55
+ throw new Error(`decodeYoloSeg: prototype length ${prototypeData.length} does not match dims=${JSON.stringify(prototypeDims)}.`);
56
+ }
57
+ const decoded = decodeYoloAnchors(perAnchorData, perAnchorDims, {
58
+ numClasses: options.numClasses,
59
+ originalWidth: options.originalWidth,
60
+ originalHeight: options.originalHeight,
61
+ padLeft: options.padLeft,
62
+ padTop: options.padTop,
63
+ scale: options.scale,
64
+ confThreshold: options.confThreshold,
65
+ iouThreshold: options.iouThreshold,
66
+ maxDetections: options.maxDetections,
67
+ });
68
+ if (decoded.anchorIndices.length === 0)
69
+ return [];
70
+ const maskThreshold = options.maskThreshold ?? 0.5;
71
+ const scaleX = maskW / options.inputWidth;
72
+ const scaleY = maskH / options.inputHeight;
73
+ const protoPlane = maskH * maskW;
74
+ const coefBase = (4 + options.numClasses) * numAnchors;
75
+ const results = [];
76
+ for (let i = 0; i < decoded.anchorIndices.length; i++) {
77
+ const a = decoded.anchorIndices[i];
78
+ const x1 = decoded.boxesXyxy[i * 4];
79
+ const y1 = decoded.boxesXyxy[i * 4 + 1];
80
+ const x2 = decoded.boxesXyxy[i * 4 + 2];
81
+ const y2 = decoded.boxesXyxy[i * 4 + 3];
82
+ const bbox = new BoundingBox(x1, y1, x2, y2);
83
+ const classId = decoded.classIds[i];
84
+ const confidence = decoded.confidences[i];
85
+ const bboxW = Math.max(0, Math.trunc(x2) - Math.trunc(x1));
86
+ const bboxH = Math.max(0, Math.trunc(y2) - Math.trunc(y1));
87
+ if (bboxW === 0 || bboxH === 0) {
88
+ results.push({ bbox, classId, confidence, mask: new Mask(new Uint8Array(0), 0, 0) });
89
+ continue;
90
+ }
91
+ // Bbox in input-tensor coords, then in low-res mask coords.
92
+ const ibx1 = x1 * options.scale + options.padLeft;
93
+ const iby1 = y1 * options.scale + options.padTop;
94
+ const ibx2 = x2 * options.scale + options.padLeft;
95
+ const iby2 = y2 * options.scale + options.padTop;
96
+ const mbx1 = Math.max(0, Math.floor(ibx1 * scaleX));
97
+ const mby1 = Math.max(0, Math.floor(iby1 * scaleY));
98
+ const mbx2 = Math.min(maskW, Math.ceil(ibx2 * scaleX));
99
+ const mby2 = Math.min(maskH, Math.ceil(iby2 * scaleY));
100
+ if (mbx2 <= mbx1 || mby2 <= mby1) {
101
+ results.push({
102
+ bbox,
103
+ classId,
104
+ confidence,
105
+ mask: new Mask(new Uint8Array(bboxW * bboxH), bboxW, bboxH),
106
+ });
107
+ continue;
108
+ }
109
+ // Compute soft mask only within the prototype region under this bbox.
110
+ const cropW = mbx2 - mbx1;
111
+ const cropH = mby2 - mby1;
112
+ const softCrop = new Float32Array(cropW * cropH);
113
+ for (let y = 0; y < cropH; y++) {
114
+ const py = mby1 + y;
115
+ for (let x = 0; x < cropW; x++) {
116
+ const px = mbx1 + x;
117
+ let sum = 0;
118
+ for (let kk = 0; kk < numMaskCoefs; kk++) {
119
+ const coef = perAnchorData[coefBase + kk * numAnchors + a];
120
+ const proto = prototypeData[kk * protoPlane + py * maskW + px];
121
+ sum += coef * proto;
122
+ }
123
+ softCrop[y * cropW + x] = sigmoid(sum);
124
+ }
125
+ }
126
+ const resized = resizeBilinear(softCrop, cropW, cropH, bboxW, bboxH);
127
+ const binary = new Uint8Array(bboxW * bboxH);
128
+ for (let j = 0; j < binary.length; j++) {
129
+ binary[j] = resized[j] >= maskThreshold ? 255 : 0;
130
+ }
131
+ results.push({ bbox, classId, confidence, mask: new Mask(binary, bboxW, bboxH) });
132
+ }
133
+ return results;
134
+ }
135
+ function sigmoid(x) {
136
+ if (x >= 0) {
137
+ return 1 / (1 + Math.exp(-x));
138
+ }
139
+ const e = Math.exp(x);
140
+ return e / (1 + e);
141
+ }
142
+ let _warnedDecodeYoloV8Seg = false;
143
+ /** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.3.0. */
144
+ export function decodeYoloV8Seg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options) {
145
+ if (!_warnedDecodeYoloV8Seg) {
146
+ _warnedDecodeYoloV8Seg = true;
147
+ console.warn("[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. " +
148
+ "The alias will be removed in 0.3.0.");
149
+ }
150
+ return decodeYoloSeg(perAnchorData, perAnchorDims, prototypeData, prototypeDims, options);
151
+ }
152
+ function resizeBilinear(src, srcWidth, srcHeight, targetWidth, targetHeight) {
153
+ const out = new Float32Array(targetWidth * targetHeight);
154
+ if (targetWidth === 0 || targetHeight === 0 || srcWidth === 0 || srcHeight === 0) {
155
+ return out;
156
+ }
157
+ if (targetWidth === srcWidth && targetHeight === srcHeight) {
158
+ out.set(src);
159
+ return out;
160
+ }
161
+ const sx = srcWidth / targetWidth;
162
+ const sy = srcHeight / targetHeight;
163
+ for (let y = 0; y < targetHeight; y++) {
164
+ const yy = (y + 0.5) * sy - 0.5;
165
+ const y0 = Math.max(0, Math.floor(yy));
166
+ const y1 = Math.min(srcHeight - 1, y0 + 1);
167
+ const wy = Math.max(0, Math.min(1, yy - y0));
168
+ for (let x = 0; x < targetWidth; x++) {
169
+ const xx = (x + 0.5) * sx - 0.5;
170
+ const x0 = Math.max(0, Math.floor(xx));
171
+ const x1 = Math.min(srcWidth - 1, x0 + 1);
172
+ const wx = Math.max(0, Math.min(1, xx - x0));
173
+ const v00 = src[y0 * srcWidth + x0];
174
+ const v01 = src[y0 * srcWidth + x1];
175
+ const v10 = src[y1 * srcWidth + x0];
176
+ const v11 = src[y1 * srcWidth + x1];
177
+ const top = v00 * (1 - wx) + v01 * wx;
178
+ const bot = v10 * (1 - wx) + v11 * wx;
179
+ out[y * targetWidth + x] = top * (1 - wy) + bot * wy;
180
+ }
181
+ }
182
+ return out;
183
+ }
184
+ //# sourceMappingURL=segmentation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"segmentation.js","sourceRoot":"","sources":["../../src/postprocess/segmentation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AA+BhD;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAC3B,aAA2B,EAC3B,aAAgC,EAChC,aAA2B,EAC3B,aAAgC,EAChC,OAA6B;IAE7B,+EAA+E;IAC/E,IAAI,KAAK,GAAG,aAAa,CAAC;IAC1B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,2DAA2D,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAC1F,CAAC;QACD,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,CAAW,EAAE,KAAK,CAAC,CAAC,CAAW,EAAE,KAAK,CAAC,CAAC,CAAW,CAAC,CAAC;IACvE,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,wEAAwE,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,CACzG,CAAC;IACJ,CAAC;IACD,MAAM,YAAY,GAAG,KAAK,CAAC,CAAC,CAAW,CAAC;IACxC,MAAM,KAAK,GAA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AAG,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC;YACtC,GAAG,CAAC,CAAC,GAAG,WAAW,GAAG,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC;QACvD,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Composable image preprocessing primitives (resize, normalize, letterbox, layout).
3
+ *
4
+ * Mirrors the Python `preprocess.image` module. Operates on the canonical
5
+ * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers
6
+ * depending on the operation.
7
+ */
8
+ import type * as ort from "onnxruntime-web";
9
+ import { RGBImage } from "../types.js";
10
+ /** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */
11
+ export declare function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage;
12
+ /**
13
+ * Convert a uint8 image to a normalized float32 array (HWC layout preserved).
14
+ *
15
+ * Applies `(pixel * scale - mean) / std` channel-wise.
16
+ */
17
+ export declare function normalize(image: RGBImage, mean: readonly [number, number, number], std: readonly [number, number, number], scale?: number): Float32Array;
18
+ /** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */
19
+ export declare function toFloat32(image: RGBImage, scale?: number): Float32Array;
20
+ /**
21
+ * Transpose interleaved HWC data to planar CHW layout.
22
+ *
23
+ * @param hwc Source array of length `width * height * channels`.
24
+ */
25
+ export declare function toCHW(hwc: Float32Array, width: number, height: number, channels?: number): Float32Array;
26
+ /** Wrap a Float32 buffer into an `ort.Tensor`. */
27
+ export declare function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor;
28
+ /**
29
+ * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.
30
+ *
31
+ * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,
32
+ * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that
33
+ * don't require ImageNet normalization.
34
+ *
35
+ * @returns CHW `Float32Array` of length `width * height * 3`.
36
+ */
37
+ export declare function toTensor(image: RGBImage): Float32Array;
38
+ /**
39
+ * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.
40
+ *
41
+ * Use when you receive image bytes from `cv2.imencode` over the wire and
42
+ * want to feed them to the SDK without going through a canvas decode.
43
+ *
44
+ * @param bgr Flat BGR Uint8Array of length `width * height * 3`.
45
+ */
46
+ export declare function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage;
47
+ /**
48
+ * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).
49
+ *
50
+ * Useful for round-tripping data to a Python OpenCV consumer.
51
+ */
52
+ export declare function toCv2(image: RGBImage): Uint8Array;
53
+ export interface LetterboxResult {
54
+ /** The padded image at the target size. */
55
+ readonly image: RGBImage;
56
+ /** The factor applied to the original image (`< 1` if downscaled). */
57
+ readonly scale: number;
58
+ /** Horizontal padding in pixels (left side; right side has the same or +1). */
59
+ readonly padLeft: number;
60
+ /** Vertical padding in pixels (top side). */
61
+ readonly padTop: number;
62
+ }
63
+ /**
64
+ * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`
65
+ * with a constant fill color.
66
+ *
67
+ * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`
68
+ * lets callers map detections back to the original image coordinates.
69
+ */
70
+ export declare function letterbox(image: RGBImage, targetWidth: number, targetHeight: number, fill?: readonly [number, number, number]): LetterboxResult;
71
+ //# sourceMappingURL=image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/preprocess/image.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,KAAK,GAAG,MAAM,iBAAiB,CAAC;AAQ5C,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,6FAA6F;AAC7F,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