tempest-react-sdk 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/vision.cjs +2 -0
- package/dist/vision.cjs.map +1 -0
- package/dist/vision.d.ts +1030 -0
- package/dist/vision.js +1416 -0
- package/dist/vision.js.map +1 -0
- package/package.json +12 -1
package/dist/vision.js
ADDED
|
@@ -0,0 +1,1416 @@
|
|
|
1
|
+
import * as H from "onnxruntime-web";
|
|
2
|
+
class R extends Error {
|
|
3
|
+
constructor(t, e) {
|
|
4
|
+
super(t, e), this.name = new.target.name;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
class Ft extends R {
|
|
8
|
+
}
|
|
9
|
+
class lt extends R {
|
|
10
|
+
}
|
|
11
|
+
class Wt extends R {
|
|
12
|
+
}
|
|
13
|
+
class $ extends R {
|
|
14
|
+
}
|
|
15
|
+
class J extends R {
|
|
16
|
+
}
|
|
17
|
+
class L {
|
|
18
|
+
constructor(t, e, s) {
|
|
19
|
+
if (this.data = t, this.width = e, this.height = s, t.length !== e * s * 3)
|
|
20
|
+
throw new $(
|
|
21
|
+
`RGBImage data length ${t.length} does not match width * height * 3 = ${e * s * 3}.`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
data;
|
|
25
|
+
width;
|
|
26
|
+
height;
|
|
27
|
+
}
|
|
28
|
+
class gt {
|
|
29
|
+
constructor(t, e, s, n) {
|
|
30
|
+
this.x1 = t, this.y1 = e, this.x2 = s, this.y2 = n;
|
|
31
|
+
}
|
|
32
|
+
x1;
|
|
33
|
+
y1;
|
|
34
|
+
x2;
|
|
35
|
+
y2;
|
|
36
|
+
/** Box width in pixels (clamped to non-negative). */
|
|
37
|
+
get width() {
|
|
38
|
+
return Math.max(0, this.x2 - this.x1);
|
|
39
|
+
}
|
|
40
|
+
/** Box height in pixels (clamped to non-negative). */
|
|
41
|
+
get height() {
|
|
42
|
+
return Math.max(0, this.y2 - this.y1);
|
|
43
|
+
}
|
|
44
|
+
/** Box area in pixels squared. */
|
|
45
|
+
get area() {
|
|
46
|
+
return this.width * this.height;
|
|
47
|
+
}
|
|
48
|
+
/** The box as `[x1, y1, x2, y2]` in absolute pixels (Ultralytics-style). */
|
|
49
|
+
get xyxy() {
|
|
50
|
+
return [this.x1, this.y1, this.x2, this.y2];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The box as `[cx, cy, w, h]` with `(cx, cy)` at the center.
|
|
54
|
+
*
|
|
55
|
+
* Matches Ultralytics' `boxes.xywh` and YOLO's native head format. For the
|
|
56
|
+
* top-left `[x, y, w, h]` convention, use {@link asXywh}.
|
|
57
|
+
*/
|
|
58
|
+
get xywh() {
|
|
59
|
+
return [(this.x1 + this.x2) / 2, (this.y1 + this.y2) / 2, this.width, this.height];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The box as `[x1, y1, x2, y2]` normalized to `[0, 1]`.
|
|
63
|
+
*
|
|
64
|
+
* @param origShape `[height, width]` of the source image, in pixels.
|
|
65
|
+
*/
|
|
66
|
+
xyxyn(t) {
|
|
67
|
+
const [e, s] = t;
|
|
68
|
+
return s <= 0 || e <= 0 ? [0, 0, 0, 0] : [this.x1 / s, this.y1 / e, this.x2 / s, this.y2 / e];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The box as `[cx, cy, w, h]` normalized to `[0, 1]`.
|
|
72
|
+
*
|
|
73
|
+
* @param origShape `[height, width]` of the source image, in pixels.
|
|
74
|
+
*/
|
|
75
|
+
xywhn(t) {
|
|
76
|
+
const [e, s] = t;
|
|
77
|
+
if (s <= 0 || e <= 0) return [0, 0, 0, 0];
|
|
78
|
+
const [n, r, a, i] = this.xywh;
|
|
79
|
+
return [n / s, r / e, a / s, i / e];
|
|
80
|
+
}
|
|
81
|
+
/** Returns `[x1, y1, x2, y2]`. */
|
|
82
|
+
asXyxy() {
|
|
83
|
+
return [this.x1, this.y1, this.x2, this.y2];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returns `[x, y, width, height]` with `(x, y)` at the **top-left**.
|
|
87
|
+
*
|
|
88
|
+
* Note: this is the top-left convention. Ultralytics' `xywh` getter uses
|
|
89
|
+
* **center** coordinates — for that, read {@link xywh}.
|
|
90
|
+
*/
|
|
91
|
+
asXywh() {
|
|
92
|
+
return [this.x1, this.y1, this.width, this.height];
|
|
93
|
+
}
|
|
94
|
+
/** Returns `[x1, y1, x2, y2]` truncated to integers, useful for slicing arrays. */
|
|
95
|
+
asIntXyxy() {
|
|
96
|
+
return [Math.trunc(this.x1), Math.trunc(this.y1), Math.trunc(this.x2), Math.trunc(this.y2)];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
class B {
|
|
100
|
+
constructor(t, e, s) {
|
|
101
|
+
if (this.data = t, this.width = e, this.height = s, t.length !== e * s)
|
|
102
|
+
throw new $(
|
|
103
|
+
`Mask data length ${t.length} does not match width * height = ${e * s}.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
data;
|
|
107
|
+
width;
|
|
108
|
+
height;
|
|
109
|
+
}
|
|
110
|
+
class xt {
|
|
111
|
+
/**
|
|
112
|
+
* @param xyxy Flat array of length `4 * N` in `[x1, y1, x2, y2, ...]` order.
|
|
113
|
+
* @param cls One class index per box, length `N`.
|
|
114
|
+
* @param conf One confidence per box, length `N`.
|
|
115
|
+
* @param origShape `[height, width]` of the original image.
|
|
116
|
+
*/
|
|
117
|
+
constructor(t, e, s, n) {
|
|
118
|
+
this.xyxy = t, this.cls = e, this.conf = s, this.origShape = n;
|
|
119
|
+
}
|
|
120
|
+
xyxy;
|
|
121
|
+
cls;
|
|
122
|
+
conf;
|
|
123
|
+
origShape;
|
|
124
|
+
/** Number of detected boxes. */
|
|
125
|
+
get length() {
|
|
126
|
+
return this.cls.length;
|
|
127
|
+
}
|
|
128
|
+
/** `[N, 4]` shape of the `xyxy` view. */
|
|
129
|
+
get shape() {
|
|
130
|
+
return [this.length, 4];
|
|
131
|
+
}
|
|
132
|
+
/** Boxes as `[N, 4]` `[cx, cy, w, h]` flat array in absolute pixels. */
|
|
133
|
+
get xywh() {
|
|
134
|
+
const t = new Float32Array(this.xyxy.length);
|
|
135
|
+
for (let e = 0; e < this.length; e++) {
|
|
136
|
+
const s = this.xyxy[e * 4], n = this.xyxy[e * 4 + 1], r = this.xyxy[e * 4 + 2], a = this.xyxy[e * 4 + 3];
|
|
137
|
+
t[e * 4] = (s + r) / 2, t[e * 4 + 1] = (n + a) / 2, t[e * 4 + 2] = r - s, t[e * 4 + 3] = a - n;
|
|
138
|
+
}
|
|
139
|
+
return t;
|
|
140
|
+
}
|
|
141
|
+
/** Boxes as `[N, 4]` `[x1, y1, x2, y2]` normalized to `[0, 1]`. */
|
|
142
|
+
get xyxyn() {
|
|
143
|
+
const [t, e] = this.origShape, s = new Float32Array(this.xyxy.length);
|
|
144
|
+
if (this.length === 0 || e <= 0 || t <= 0) return s;
|
|
145
|
+
for (let n = 0; n < this.length; n++)
|
|
146
|
+
s[n * 4] = this.xyxy[n * 4] / e, s[n * 4 + 1] = this.xyxy[n * 4 + 1] / t, s[n * 4 + 2] = this.xyxy[n * 4 + 2] / e, s[n * 4 + 3] = this.xyxy[n * 4 + 3] / t;
|
|
147
|
+
return s;
|
|
148
|
+
}
|
|
149
|
+
/** Boxes as `[N, 4]` `[cx, cy, w, h]` normalized to `[0, 1]`. */
|
|
150
|
+
get xywhn() {
|
|
151
|
+
const t = this.xywh, [e, s] = this.origShape;
|
|
152
|
+
if (this.length === 0 || s <= 0 || e <= 0) return t;
|
|
153
|
+
for (let n = 0; n < this.length; n++)
|
|
154
|
+
t[n * 4] = t[n * 4] / s, t[n * 4 + 1] = t[n * 4 + 1] / e, t[n * 4 + 2] = t[n * 4 + 2] / s, t[n * 4 + 3] = t[n * 4 + 3] / e;
|
|
155
|
+
return t;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Concatenated `[N, 6]` array of `[x1, y1, x2, y2, conf, cls]`.
|
|
159
|
+
*
|
|
160
|
+
* Matches Ultralytics' `boxes.data`.
|
|
161
|
+
*/
|
|
162
|
+
get data() {
|
|
163
|
+
const t = new Float32Array(this.length * 6);
|
|
164
|
+
for (let e = 0; e < this.length; e++)
|
|
165
|
+
t[e * 6] = this.xyxy[e * 4], t[e * 6 + 1] = this.xyxy[e * 4 + 1], t[e * 6 + 2] = this.xyxy[e * 4 + 2], t[e * 6 + 3] = this.xyxy[e * 4 + 3], t[e * 6 + 4] = this.conf[e], t[e * 6 + 5] = this.cls[e];
|
|
166
|
+
return t;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
class Nt {
|
|
170
|
+
/** @param data `[numClasses]` per-class probabilities, indexed by class id. */
|
|
171
|
+
constructor(t) {
|
|
172
|
+
this.data = t;
|
|
173
|
+
}
|
|
174
|
+
data;
|
|
175
|
+
/** Number of classes. */
|
|
176
|
+
get length() {
|
|
177
|
+
return this.data.length;
|
|
178
|
+
}
|
|
179
|
+
/** `[numClasses]` shape of the underlying vector. */
|
|
180
|
+
get shape() {
|
|
181
|
+
return [this.length];
|
|
182
|
+
}
|
|
183
|
+
/** Index of the most probable class. */
|
|
184
|
+
get top1() {
|
|
185
|
+
if (this.data.length === 0) return 0;
|
|
186
|
+
let t = 0, e = this.data[0];
|
|
187
|
+
for (let s = 1; s < this.data.length; s++) {
|
|
188
|
+
const n = this.data[s];
|
|
189
|
+
n > e && (t = s, e = n);
|
|
190
|
+
}
|
|
191
|
+
return t;
|
|
192
|
+
}
|
|
193
|
+
/** Probability of the top-1 class. */
|
|
194
|
+
get top1conf() {
|
|
195
|
+
return this.data.length === 0 ? 0 : this.data[this.top1];
|
|
196
|
+
}
|
|
197
|
+
/** Indices of the top-5 most probable classes, descending. */
|
|
198
|
+
get top5() {
|
|
199
|
+
return this._topK(5).indices;
|
|
200
|
+
}
|
|
201
|
+
/** Probabilities of the top-5 classes, descending. */
|
|
202
|
+
get top5conf() {
|
|
203
|
+
return this._topK(5).values;
|
|
204
|
+
}
|
|
205
|
+
_topK(t) {
|
|
206
|
+
const e = Math.min(t, this.data.length), s = [];
|
|
207
|
+
for (let a = 0; a < this.data.length; a++) s.push(a);
|
|
208
|
+
s.sort((a, i) => this.data[i] - this.data[a]);
|
|
209
|
+
const n = new Int32Array(e), r = new Float32Array(e);
|
|
210
|
+
for (let a = 0; a < e; a++)
|
|
211
|
+
n[a] = s[a], r[a] = this.data[s[a]];
|
|
212
|
+
return { indices: n, values: r };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
class Ot {
|
|
216
|
+
/**
|
|
217
|
+
* @param data Per-instance binary masks (`Mask` objects from `types.ts`).
|
|
218
|
+
* @param xyxy Flat `[N, 4]` of bounding-box coordinates in original pixels.
|
|
219
|
+
* @param origShape `[height, width]` of the original image.
|
|
220
|
+
*/
|
|
221
|
+
constructor(t, e, s) {
|
|
222
|
+
this.data = t, this.xyxy = e, this.origShape = s;
|
|
223
|
+
}
|
|
224
|
+
data;
|
|
225
|
+
xyxy;
|
|
226
|
+
origShape;
|
|
227
|
+
/** Number of instance masks. */
|
|
228
|
+
get length() {
|
|
229
|
+
return this.data.length;
|
|
230
|
+
}
|
|
231
|
+
/** `[N]` shape of the masks collection. */
|
|
232
|
+
get shape() {
|
|
233
|
+
return [this.length];
|
|
234
|
+
}
|
|
235
|
+
[Symbol.iterator]() {
|
|
236
|
+
return this.data[Symbol.iterator]();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
class zt {
|
|
240
|
+
constructor(t, e, s, n, r, a = null, i = {}) {
|
|
241
|
+
this.boxes = t, this.detections = e, this.names = s, this.origImg = n, this.origShape = r, this.path = a, this.speed = i;
|
|
242
|
+
}
|
|
243
|
+
boxes;
|
|
244
|
+
detections;
|
|
245
|
+
names;
|
|
246
|
+
origImg;
|
|
247
|
+
origShape;
|
|
248
|
+
path;
|
|
249
|
+
speed;
|
|
250
|
+
/** Number of surviving detections. */
|
|
251
|
+
get length() {
|
|
252
|
+
return this.detections.length;
|
|
253
|
+
}
|
|
254
|
+
/** Index into the per-instance detections. */
|
|
255
|
+
get(t) {
|
|
256
|
+
return this.detections[t];
|
|
257
|
+
}
|
|
258
|
+
[Symbol.iterator]() {
|
|
259
|
+
return this.detections[Symbol.iterator]();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
class Lt {
|
|
263
|
+
constructor(t, e, s, n, r, a = null, i = {}) {
|
|
264
|
+
this.probs = t, this.result = e, this.names = s, this.origImg = n, this.origShape = r, this.path = a, this.speed = i;
|
|
265
|
+
}
|
|
266
|
+
probs;
|
|
267
|
+
result;
|
|
268
|
+
names;
|
|
269
|
+
origImg;
|
|
270
|
+
origShape;
|
|
271
|
+
path;
|
|
272
|
+
speed;
|
|
273
|
+
/** Top-1 class index (Ultralytics-style alias). */
|
|
274
|
+
get cls() {
|
|
275
|
+
return this.probs.top1;
|
|
276
|
+
}
|
|
277
|
+
/** Top-1 confidence (Ultralytics-style alias). */
|
|
278
|
+
get conf() {
|
|
279
|
+
return this.probs.top1conf;
|
|
280
|
+
}
|
|
281
|
+
/** Top-1 class name. */
|
|
282
|
+
get name() {
|
|
283
|
+
return this.names[this.cls] ?? `class_${this.cls}`;
|
|
284
|
+
}
|
|
285
|
+
/** Per-class probability list, sorted descending (legacy field). */
|
|
286
|
+
get probabilities() {
|
|
287
|
+
return this.result.probabilities;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
class Dt {
|
|
291
|
+
constructor(t, e, s, n, r, a, i = null, c = {}) {
|
|
292
|
+
this.boxes = t, this.masks = e, this.detections = s, this.names = n, this.origImg = r, this.origShape = a, this.path = i, this.speed = c;
|
|
293
|
+
}
|
|
294
|
+
boxes;
|
|
295
|
+
masks;
|
|
296
|
+
detections;
|
|
297
|
+
names;
|
|
298
|
+
origImg;
|
|
299
|
+
origShape;
|
|
300
|
+
path;
|
|
301
|
+
speed;
|
|
302
|
+
/** Number of surviving instances. */
|
|
303
|
+
get length() {
|
|
304
|
+
return this.detections.length;
|
|
305
|
+
}
|
|
306
|
+
/** Index into the per-instance results. */
|
|
307
|
+
get(t) {
|
|
308
|
+
return this.detections[t];
|
|
309
|
+
}
|
|
310
|
+
[Symbol.iterator]() {
|
|
311
|
+
return this.detections[Symbol.iterator]();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const Yt = Object.freeze([
|
|
315
|
+
"person",
|
|
316
|
+
"bicycle",
|
|
317
|
+
"car",
|
|
318
|
+
"motorcycle",
|
|
319
|
+
"airplane",
|
|
320
|
+
"bus",
|
|
321
|
+
"train",
|
|
322
|
+
"truck",
|
|
323
|
+
"boat",
|
|
324
|
+
"traffic light",
|
|
325
|
+
"fire hydrant",
|
|
326
|
+
"stop sign",
|
|
327
|
+
"parking meter",
|
|
328
|
+
"bench",
|
|
329
|
+
"bird",
|
|
330
|
+
"cat",
|
|
331
|
+
"dog",
|
|
332
|
+
"horse",
|
|
333
|
+
"sheep",
|
|
334
|
+
"cow",
|
|
335
|
+
"elephant",
|
|
336
|
+
"bear",
|
|
337
|
+
"zebra",
|
|
338
|
+
"giraffe",
|
|
339
|
+
"backpack",
|
|
340
|
+
"umbrella",
|
|
341
|
+
"handbag",
|
|
342
|
+
"tie",
|
|
343
|
+
"suitcase",
|
|
344
|
+
"frisbee",
|
|
345
|
+
"skis",
|
|
346
|
+
"snowboard",
|
|
347
|
+
"sports ball",
|
|
348
|
+
"kite",
|
|
349
|
+
"baseball bat",
|
|
350
|
+
"baseball glove",
|
|
351
|
+
"skateboard",
|
|
352
|
+
"surfboard",
|
|
353
|
+
"tennis racket",
|
|
354
|
+
"bottle",
|
|
355
|
+
"wine glass",
|
|
356
|
+
"cup",
|
|
357
|
+
"fork",
|
|
358
|
+
"knife",
|
|
359
|
+
"spoon",
|
|
360
|
+
"bowl",
|
|
361
|
+
"banana",
|
|
362
|
+
"apple",
|
|
363
|
+
"sandwich",
|
|
364
|
+
"orange",
|
|
365
|
+
"broccoli",
|
|
366
|
+
"carrot",
|
|
367
|
+
"hot dog",
|
|
368
|
+
"pizza",
|
|
369
|
+
"donut",
|
|
370
|
+
"cake",
|
|
371
|
+
"chair",
|
|
372
|
+
"couch",
|
|
373
|
+
"potted plant",
|
|
374
|
+
"bed",
|
|
375
|
+
"dining table",
|
|
376
|
+
"toilet",
|
|
377
|
+
"tv",
|
|
378
|
+
"laptop",
|
|
379
|
+
"mouse",
|
|
380
|
+
"remote",
|
|
381
|
+
"keyboard",
|
|
382
|
+
"cell phone",
|
|
383
|
+
"microwave",
|
|
384
|
+
"oven",
|
|
385
|
+
"toaster",
|
|
386
|
+
"sink",
|
|
387
|
+
"refrigerator",
|
|
388
|
+
"book",
|
|
389
|
+
"clock",
|
|
390
|
+
"vase",
|
|
391
|
+
"scissors",
|
|
392
|
+
"teddy bear",
|
|
393
|
+
"hair drier",
|
|
394
|
+
"toothbrush"
|
|
395
|
+
]), dt = {
|
|
396
|
+
coco: Yt
|
|
397
|
+
};
|
|
398
|
+
function et(o, t = {}) {
|
|
399
|
+
const e = Ut(o, t.numClasses);
|
|
400
|
+
if (t.numClasses !== void 0 && e.length !== t.numClasses)
|
|
401
|
+
throw new J(
|
|
402
|
+
`Resolved ${e.length} labels but the model has ${t.numClasses} classes.`
|
|
403
|
+
);
|
|
404
|
+
return e;
|
|
405
|
+
}
|
|
406
|
+
function Ut(o, t) {
|
|
407
|
+
if (o == null) {
|
|
408
|
+
if (t === void 0)
|
|
409
|
+
throw new J(
|
|
410
|
+
"Cannot auto-generate labels without numClasses. Pass an explicit labels spec or numClasses."
|
|
411
|
+
);
|
|
412
|
+
return Array.from({ length: t }, (e, s) => `class_${s}`);
|
|
413
|
+
}
|
|
414
|
+
if (Array.isArray(o))
|
|
415
|
+
return [...o];
|
|
416
|
+
if (typeof o == "string") {
|
|
417
|
+
const e = dt[o];
|
|
418
|
+
if (e !== void 0)
|
|
419
|
+
return e;
|
|
420
|
+
throw new J(
|
|
421
|
+
`Unknown labels preset: ${JSON.stringify(o)}. Known presets: ${Object.keys(dt).join(", ")}.`
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
if (typeof o == "object") {
|
|
425
|
+
const e = o, s = Object.keys(e).map((r) => Number(r));
|
|
426
|
+
if (s.length === 0)
|
|
427
|
+
return [];
|
|
428
|
+
const n = Math.max(...s);
|
|
429
|
+
return Array.from({ length: n + 1 }, (r, a) => e[a] ?? `class_${a}`);
|
|
430
|
+
}
|
|
431
|
+
throw new J(`Unsupported labels spec type: ${typeof o}.`);
|
|
432
|
+
}
|
|
433
|
+
const ut = ["webgpu", "wasm"];
|
|
434
|
+
function Bt(o) {
|
|
435
|
+
return o === void 0 ? [...ut] : o.length === 0 ? [...ut] : [...o];
|
|
436
|
+
}
|
|
437
|
+
class X {
|
|
438
|
+
constructor(t, e) {
|
|
439
|
+
this._session = t, this.providers = e;
|
|
440
|
+
}
|
|
441
|
+
_session;
|
|
442
|
+
providers;
|
|
443
|
+
/**
|
|
444
|
+
* Load an ONNX model into an ORT inference session.
|
|
445
|
+
*
|
|
446
|
+
* @param model Either a URL string fetched by ORT, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.
|
|
447
|
+
* @param options Provider list and pass-through `SessionOptions`.
|
|
448
|
+
* @throws {@link ModelLoadError} if the model cannot be loaded.
|
|
449
|
+
*/
|
|
450
|
+
static async create(t, e = {}) {
|
|
451
|
+
const s = Bt(e.providers), n = {
|
|
452
|
+
...e.sessionOptions ?? {},
|
|
453
|
+
executionProviders: s
|
|
454
|
+
};
|
|
455
|
+
let r;
|
|
456
|
+
try {
|
|
457
|
+
typeof t == "string" ? r = await H.InferenceSession.create(t, n) : t instanceof Uint8Array ? r = await H.InferenceSession.create(t, n) : r = await H.InferenceSession.create(
|
|
458
|
+
t,
|
|
459
|
+
n
|
|
460
|
+
);
|
|
461
|
+
} catch (a) {
|
|
462
|
+
throw new Ft(`Failed to load ONNX model: ${a.message}`, {
|
|
463
|
+
cause: a
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
return new X(r, s);
|
|
467
|
+
}
|
|
468
|
+
/** Names of the model's inputs, in declaration order. */
|
|
469
|
+
get inputNames() {
|
|
470
|
+
return this._session.inputNames;
|
|
471
|
+
}
|
|
472
|
+
/** Name of the first (and usually only) input. */
|
|
473
|
+
get inputName() {
|
|
474
|
+
const t = this._session.inputNames[0];
|
|
475
|
+
if (t === void 0)
|
|
476
|
+
throw new lt("Model has no inputs.");
|
|
477
|
+
return t;
|
|
478
|
+
}
|
|
479
|
+
/** Names of the model's outputs, in declaration order. */
|
|
480
|
+
get outputNames() {
|
|
481
|
+
return this._session.outputNames;
|
|
482
|
+
}
|
|
483
|
+
/** The underlying `onnxruntime-web` session, for advanced use cases. */
|
|
484
|
+
get raw() {
|
|
485
|
+
return this._session;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Run inference and return all outputs.
|
|
489
|
+
*
|
|
490
|
+
* @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.
|
|
491
|
+
* @throws {@link InferenceError} if ORT raises any error during execution.
|
|
492
|
+
*/
|
|
493
|
+
async run(t) {
|
|
494
|
+
try {
|
|
495
|
+
return await this._session.run(t);
|
|
496
|
+
} catch (e) {
|
|
497
|
+
throw new lt(`Inference failed: ${e.message}`, { cause: e });
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function W(o, t) {
|
|
502
|
+
if (typeof OffscreenCanvas < "u")
|
|
503
|
+
return new OffscreenCanvas(o, t);
|
|
504
|
+
if (typeof document < "u") {
|
|
505
|
+
const e = document.createElement("canvas");
|
|
506
|
+
return e.width = o, e.height = t, e;
|
|
507
|
+
}
|
|
508
|
+
throw new $("No canvas implementation available in this environment.");
|
|
509
|
+
}
|
|
510
|
+
function tt(o) {
|
|
511
|
+
const t = o.getContext("2d");
|
|
512
|
+
if (t === null)
|
|
513
|
+
throw new $("Failed to obtain 2D rendering context.");
|
|
514
|
+
return t;
|
|
515
|
+
}
|
|
516
|
+
function nt(o) {
|
|
517
|
+
const { data: t, width: e, height: s } = o;
|
|
518
|
+
if (t.length !== e * s * 4)
|
|
519
|
+
throw new $(
|
|
520
|
+
`Unexpected ImageData length ${t.length} for ${e}x${s} (expected ${e * s * 4}).`
|
|
521
|
+
);
|
|
522
|
+
const n = new Uint8Array(e * s * 3);
|
|
523
|
+
for (let r = 0, a = 0; r < t.length; r += 4, a += 3)
|
|
524
|
+
n[a] = t[r], n[a + 1] = t[r + 1], n[a + 2] = t[r + 2];
|
|
525
|
+
return new L(n, e, s);
|
|
526
|
+
}
|
|
527
|
+
function Rt(o) {
|
|
528
|
+
const t = new Uint8ClampedArray(o.width * o.height * 4);
|
|
529
|
+
for (let e = 0, s = 0; e < o.data.length; e += 3, s += 4)
|
|
530
|
+
t[s] = o.data[e], t[s + 1] = o.data[e + 1], t[s + 2] = o.data[e + 2], t[s + 3] = 255;
|
|
531
|
+
return new ImageData(t, o.width, o.height);
|
|
532
|
+
}
|
|
533
|
+
async function st(o) {
|
|
534
|
+
if (o instanceof L)
|
|
535
|
+
return o;
|
|
536
|
+
if (typeof ImageData < "u" && o instanceof ImageData)
|
|
537
|
+
return nt(o);
|
|
538
|
+
if (typeof o == "string")
|
|
539
|
+
return Xt(o);
|
|
540
|
+
if (typeof Blob < "u" && o instanceof Blob)
|
|
541
|
+
return pt(o);
|
|
542
|
+
if (typeof HTMLImageElement < "u" && o instanceof HTMLImageElement)
|
|
543
|
+
return await Vt(o), U(o, o.naturalWidth, o.naturalHeight);
|
|
544
|
+
if (typeof HTMLCanvasElement < "u" && o instanceof HTMLCanvasElement || typeof OffscreenCanvas < "u" && o instanceof OffscreenCanvas || typeof ImageBitmap < "u" && o instanceof ImageBitmap)
|
|
545
|
+
return U(o, o.width, o.height);
|
|
546
|
+
throw new $(
|
|
547
|
+
`Unsupported image source type: ${Object.prototype.toString.call(o)}`
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
async function Xt(o) {
|
|
551
|
+
let t;
|
|
552
|
+
try {
|
|
553
|
+
t = await fetch(o);
|
|
554
|
+
} catch (s) {
|
|
555
|
+
throw new $(`Failed to fetch image from ${o}: ${s.message}`, {
|
|
556
|
+
cause: s
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
if (!t.ok)
|
|
560
|
+
throw new $(
|
|
561
|
+
`Failed to fetch image from ${o}: HTTP ${t.status} ${t.statusText}`
|
|
562
|
+
);
|
|
563
|
+
const e = await t.blob();
|
|
564
|
+
return pt(e);
|
|
565
|
+
}
|
|
566
|
+
async function pt(o) {
|
|
567
|
+
let t;
|
|
568
|
+
try {
|
|
569
|
+
t = await createImageBitmap(o);
|
|
570
|
+
} catch (e) {
|
|
571
|
+
throw new $(`Failed to decode image blob: ${e.message}`, {
|
|
572
|
+
cause: e
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
try {
|
|
576
|
+
return U(t, t.width, t.height);
|
|
577
|
+
} finally {
|
|
578
|
+
t.close();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function Vt(o) {
|
|
582
|
+
return o.complete && o.naturalWidth > 0 ? Promise.resolve() : new Promise((t, e) => {
|
|
583
|
+
const s = () => {
|
|
584
|
+
r(), t();
|
|
585
|
+
}, n = () => {
|
|
586
|
+
r(), e(new $("Failed to load HTMLImageElement (load event errored)"));
|
|
587
|
+
}, r = () => {
|
|
588
|
+
o.removeEventListener("load", s), o.removeEventListener("error", n);
|
|
589
|
+
};
|
|
590
|
+
o.addEventListener("load", s, { once: !0 }), o.addEventListener("error", n, { once: !0 });
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
function U(o, t, e) {
|
|
594
|
+
if (t === 0 || e === 0)
|
|
595
|
+
throw new $(`Cannot load image with zero dimension (${t}x${e}).`);
|
|
596
|
+
const s = W(t, e), n = tt(s);
|
|
597
|
+
n.drawImage(o, 0, 0);
|
|
598
|
+
const r = n.getImageData(0, 0, t, e);
|
|
599
|
+
return nt(r);
|
|
600
|
+
}
|
|
601
|
+
function bt(o, t, e) {
|
|
602
|
+
if (t <= 0 || e <= 0)
|
|
603
|
+
throw new Error(`Invalid resize target ${t}x${e}.`);
|
|
604
|
+
if (t === o.width && e === o.height)
|
|
605
|
+
return o;
|
|
606
|
+
const s = W(o.width, o.height);
|
|
607
|
+
tt(s).putImageData(Rt(o), 0, 0);
|
|
608
|
+
const r = W(t, e), a = tt(r);
|
|
609
|
+
a.imageSmoothingEnabled = !0, a.imageSmoothingQuality = "high", a.drawImage(s, 0, 0, t, e);
|
|
610
|
+
const i = a.getImageData(0, 0, t, e);
|
|
611
|
+
return nt(i);
|
|
612
|
+
}
|
|
613
|
+
function jt(o, t, e, s = 1 / 255) {
|
|
614
|
+
const n = new Float32Array(o.data.length), r = o.data, a = t[0], i = t[1], c = t[2], f = e[0], y = e[1], h = e[2];
|
|
615
|
+
for (let l = 0; l < r.length; l += 3)
|
|
616
|
+
n[l] = (r[l] * s - a) / f, n[l + 1] = (r[l + 1] * s - i) / y, n[l + 2] = (r[l + 2] * s - c) / h;
|
|
617
|
+
return n;
|
|
618
|
+
}
|
|
619
|
+
function ot(o, t = 1 / 255) {
|
|
620
|
+
const e = new Float32Array(o.data.length), s = o.data;
|
|
621
|
+
for (let n = 0; n < s.length; n++)
|
|
622
|
+
e[n] = s[n] * t;
|
|
623
|
+
return e;
|
|
624
|
+
}
|
|
625
|
+
function Q(o, t, e, s = 3) {
|
|
626
|
+
const n = t * e * s;
|
|
627
|
+
if (o.length !== n)
|
|
628
|
+
throw new Error(
|
|
629
|
+
`toCHW: expected length ${n} for ${t}x${e}x${s}, got ${o.length}.`
|
|
630
|
+
);
|
|
631
|
+
const r = new Float32Array(n), a = t * e;
|
|
632
|
+
for (let i = 0; i < e; i++)
|
|
633
|
+
for (let c = 0; c < t; c++) {
|
|
634
|
+
const f = (i * t + c) * s, y = i * t + c;
|
|
635
|
+
for (let h = 0; h < s; h++)
|
|
636
|
+
r[h * a + y] = o[f + h];
|
|
637
|
+
}
|
|
638
|
+
return r;
|
|
639
|
+
}
|
|
640
|
+
function rt(o, t) {
|
|
641
|
+
return new H.Tensor("float32", o, t);
|
|
642
|
+
}
|
|
643
|
+
function te(o) {
|
|
644
|
+
const t = ot(o);
|
|
645
|
+
return Q(t, o.width, o.height, 3);
|
|
646
|
+
}
|
|
647
|
+
function ee(o, t, e) {
|
|
648
|
+
if (o.length !== t * e * 3)
|
|
649
|
+
throw new Error(
|
|
650
|
+
`fromCv2: data length ${o.length} does not match width * height * 3 = ${t * e * 3}.`
|
|
651
|
+
);
|
|
652
|
+
const s = new Uint8Array(o.length);
|
|
653
|
+
for (let n = 0; n < o.length; n += 3)
|
|
654
|
+
s[n] = o[n + 2], s[n + 1] = o[n + 1], s[n + 2] = o[n];
|
|
655
|
+
return new L(s, t, e);
|
|
656
|
+
}
|
|
657
|
+
function ne(o) {
|
|
658
|
+
const t = o.data, e = new Uint8Array(t.length);
|
|
659
|
+
for (let s = 0; s < t.length; s += 3)
|
|
660
|
+
e[s] = t[s + 2], e[s + 1] = t[s + 1], e[s + 2] = t[s];
|
|
661
|
+
return e;
|
|
662
|
+
}
|
|
663
|
+
function _t(o, t, e, s = [114, 114, 114]) {
|
|
664
|
+
const n = Math.min(t / o.width, e / o.height), r = Math.round(o.width * n), a = Math.round(o.height * n), i = bt(o, r, a), c = new Uint8Array(t * e * 3), f = s[0], y = s[1], h = s[2];
|
|
665
|
+
for (let d = 0; d < c.length; d += 3)
|
|
666
|
+
c[d] = f, c[d + 1] = y, c[d + 2] = h;
|
|
667
|
+
const l = Math.floor((t - r) / 2), w = Math.floor((e - a) / 2), x = r * 3;
|
|
668
|
+
for (let d = 0; d < a; d++) {
|
|
669
|
+
const u = d * x, p = ((w + d) * t + l) * 3;
|
|
670
|
+
c.set(i.data.subarray(u, u + x), p);
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
image: new L(c, t, e),
|
|
674
|
+
scale: n,
|
|
675
|
+
padLeft: l,
|
|
676
|
+
padTop: w
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
function Pt(o) {
|
|
680
|
+
const t = o.length, e = new Float32Array(t);
|
|
681
|
+
let s = -1 / 0;
|
|
682
|
+
for (let r = 0; r < t; r++) {
|
|
683
|
+
const a = o[r];
|
|
684
|
+
a > s && (s = a);
|
|
685
|
+
}
|
|
686
|
+
let n = 0;
|
|
687
|
+
for (let r = 0; r < t; r++) {
|
|
688
|
+
const a = Math.exp(o[r] - s);
|
|
689
|
+
e[r] = a, n += a;
|
|
690
|
+
}
|
|
691
|
+
for (let r = 0; r < t; r++)
|
|
692
|
+
e[r] = e[r] / n;
|
|
693
|
+
return e;
|
|
694
|
+
}
|
|
695
|
+
function Kt(o, t) {
|
|
696
|
+
const e = o.length, s = t === null ? e : Math.min(Math.max(0, t), e), n = new Array(e);
|
|
697
|
+
for (let i = 0; i < e; i++) n[i] = i;
|
|
698
|
+
n.sort((i, c) => o[c] - o[i]);
|
|
699
|
+
const r = new Int32Array(s), a = new Float32Array(s);
|
|
700
|
+
for (let i = 0; i < s; i++) {
|
|
701
|
+
const c = n[i];
|
|
702
|
+
r[i] = c, a[i] = o[c];
|
|
703
|
+
}
|
|
704
|
+
return { indices: r, values: a };
|
|
705
|
+
}
|
|
706
|
+
function Gt(o, t, e) {
|
|
707
|
+
const s = t.length;
|
|
708
|
+
if (s === 0) return new Int32Array(0);
|
|
709
|
+
const n = new Float32Array(s);
|
|
710
|
+
for (let c = 0; c < s; c++) {
|
|
711
|
+
const f = o[c * 4], y = o[c * 4 + 1], h = o[c * 4 + 2], l = o[c * 4 + 3];
|
|
712
|
+
n[c] = Math.max(0, h - f) * Math.max(0, l - y);
|
|
713
|
+
}
|
|
714
|
+
const r = new Array(s);
|
|
715
|
+
for (let c = 0; c < s; c++) r[c] = c;
|
|
716
|
+
r.sort((c, f) => t[f] - t[c]);
|
|
717
|
+
const a = new Uint8Array(s), i = [];
|
|
718
|
+
for (let c = 0; c < r.length; c++) {
|
|
719
|
+
const f = r[c];
|
|
720
|
+
if (a[f]) continue;
|
|
721
|
+
i.push(f);
|
|
722
|
+
const y = o[f * 4], h = o[f * 4 + 1], l = o[f * 4 + 2], w = o[f * 4 + 3], x = n[f];
|
|
723
|
+
for (let d = c + 1; d < r.length; d++) {
|
|
724
|
+
const u = r[d];
|
|
725
|
+
if (a[u]) continue;
|
|
726
|
+
const p = o[u * 4], _ = o[u * 4 + 1], g = o[u * 4 + 2], S = o[u * 4 + 3], I = Math.max(y, p), v = Math.max(h, _), M = Math.min(l, g), C = Math.min(w, S), E = Math.max(0, M - I), m = Math.max(0, C - v), b = E * m, A = x + n[u] - b;
|
|
727
|
+
(A > 0 ? b / A : 0) > e && (a[u] = 1);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return Int32Array.from(i);
|
|
731
|
+
}
|
|
732
|
+
function Ht(o, t, e, s) {
|
|
733
|
+
if (t.length === 0) return new Int32Array(0);
|
|
734
|
+
const n = /* @__PURE__ */ new Map();
|
|
735
|
+
for (let a = 0; a < e.length; a++) {
|
|
736
|
+
const i = e[a], c = n.get(i);
|
|
737
|
+
c === void 0 ? n.set(i, [a]) : c.push(a);
|
|
738
|
+
}
|
|
739
|
+
const r = [];
|
|
740
|
+
for (const a of n.values()) {
|
|
741
|
+
const i = a.length, c = new Float32Array(i * 4), f = new Float32Array(i);
|
|
742
|
+
for (let h = 0; h < i; h++) {
|
|
743
|
+
const l = a[h];
|
|
744
|
+
c[h * 4] = o[l * 4], c[h * 4 + 1] = o[l * 4 + 1], c[h * 4 + 2] = o[l * 4 + 2], c[h * 4 + 3] = o[l * 4 + 3], f[h] = t[l];
|
|
745
|
+
}
|
|
746
|
+
const y = Gt(c, f, s);
|
|
747
|
+
for (let h = 0; h < y.length; h++)
|
|
748
|
+
r.push(a[y[h]]);
|
|
749
|
+
}
|
|
750
|
+
return r.sort((a, i) => t[i] - t[a]), Int32Array.from(r);
|
|
751
|
+
}
|
|
752
|
+
function at(o, t, e) {
|
|
753
|
+
let s = t;
|
|
754
|
+
if (s.length === 3) {
|
|
755
|
+
if (s[0] !== 1)
|
|
756
|
+
throw new Error(`decodeYoloAnchors: expected batch size 1, got ${s[0]}.`);
|
|
757
|
+
s = [s[1], s[2]];
|
|
758
|
+
}
|
|
759
|
+
if (s.length !== 2)
|
|
760
|
+
throw new Error(
|
|
761
|
+
`decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(t)}.`
|
|
762
|
+
);
|
|
763
|
+
const n = s[0], r = s[1], {
|
|
764
|
+
numClasses: a,
|
|
765
|
+
originalWidth: i,
|
|
766
|
+
originalHeight: c,
|
|
767
|
+
padLeft: f,
|
|
768
|
+
padTop: y,
|
|
769
|
+
scale: h,
|
|
770
|
+
confThreshold: l,
|
|
771
|
+
iouThreshold: w,
|
|
772
|
+
maxDetections: x
|
|
773
|
+
} = e;
|
|
774
|
+
if (a < 1 || a + 4 > n)
|
|
775
|
+
throw new Error(
|
|
776
|
+
`decodeYoloAnchors: invalid numClasses=${a} for channels=${n}.`
|
|
777
|
+
);
|
|
778
|
+
if (o.length !== n * r)
|
|
779
|
+
throw new Error(
|
|
780
|
+
`decodeYoloAnchors: data length ${o.length} does not match channels*numAnchors=${n * r}.`
|
|
781
|
+
);
|
|
782
|
+
const d = [];
|
|
783
|
+
for (let m = 0; m < r; m++) {
|
|
784
|
+
let b = 0, A = -1 / 0;
|
|
785
|
+
for (let Y = 0; Y < a; Y++) {
|
|
786
|
+
const z = o[(4 + Y) * r + m];
|
|
787
|
+
z !== void 0 && z > A && (A = z, b = Y);
|
|
788
|
+
}
|
|
789
|
+
if (A < l) continue;
|
|
790
|
+
const T = o[m], V = o[r + m], j = o[2 * r + m], P = o[3 * r + m];
|
|
791
|
+
let D = T - j / 2, k = V - P / 2, F = T + j / 2, O = V + P / 2;
|
|
792
|
+
D = (D - f) / h, k = (k - y) / h, F = (F - f) / h, O = (O - y) / h, D = Math.max(0, Math.min(i, D)), k = Math.max(0, Math.min(c, k)), F = Math.max(0, Math.min(i, F)), O = Math.max(0, Math.min(c, O)), d.push({ anchorIdx: m, x1: D, y1: k, x2: F, y2: O, classId: b, confidence: A });
|
|
793
|
+
}
|
|
794
|
+
if (d.length === 0) return ft();
|
|
795
|
+
const u = new Float32Array(d.length * 4), p = new Float32Array(d.length), _ = new Int32Array(d.length);
|
|
796
|
+
for (let m = 0; m < d.length; m++) {
|
|
797
|
+
const b = d[m];
|
|
798
|
+
u[m * 4] = b.x1, u[m * 4 + 1] = b.y1, u[m * 4 + 2] = b.x2, u[m * 4 + 3] = b.y2, p[m] = b.confidence, _[m] = b.classId;
|
|
799
|
+
}
|
|
800
|
+
const g = Ht(u, p, _, w);
|
|
801
|
+
if (g.length === 0) return ft();
|
|
802
|
+
const S = Array.from(g).slice(0, x), I = S.length, v = new Int32Array(I), M = new Float32Array(I * 4), C = new Int32Array(I), E = new Float32Array(I);
|
|
803
|
+
for (let m = 0; m < I; m++) {
|
|
804
|
+
const b = d[S[m]];
|
|
805
|
+
v[m] = b.anchorIdx, M[m * 4] = b.x1, M[m * 4 + 1] = b.y1, M[m * 4 + 2] = b.x2, M[m * 4 + 3] = b.y2, C[m] = b.classId, E[m] = b.confidence;
|
|
806
|
+
}
|
|
807
|
+
return { anchorIndices: v, boxesXyxy: M, classIds: C, confidences: E };
|
|
808
|
+
}
|
|
809
|
+
function ft() {
|
|
810
|
+
return {
|
|
811
|
+
anchorIndices: new Int32Array(0),
|
|
812
|
+
boxesXyxy: new Float32Array(0),
|
|
813
|
+
classIds: new Int32Array(0),
|
|
814
|
+
confidences: new Float32Array(0)
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
function Mt(o, t, e) {
|
|
818
|
+
const s = t.length === 3 ? t[1] : t[0];
|
|
819
|
+
if (s === void 0 || s < 5)
|
|
820
|
+
throw new Error(`decodeYolo: invalid output channel count ${s} (expected >= 5).`);
|
|
821
|
+
const n = s - 4, r = at(o, t, {
|
|
822
|
+
numClasses: n,
|
|
823
|
+
...e
|
|
824
|
+
}), a = [];
|
|
825
|
+
for (let i = 0; i < r.classIds.length; i++)
|
|
826
|
+
a.push({
|
|
827
|
+
bbox: new gt(
|
|
828
|
+
r.boxesXyxy[i * 4],
|
|
829
|
+
r.boxesXyxy[i * 4 + 1],
|
|
830
|
+
r.boxesXyxy[i * 4 + 2],
|
|
831
|
+
r.boxesXyxy[i * 4 + 3]
|
|
832
|
+
),
|
|
833
|
+
classId: r.classIds[i],
|
|
834
|
+
confidence: r.confidences[i]
|
|
835
|
+
});
|
|
836
|
+
return a;
|
|
837
|
+
}
|
|
838
|
+
let yt = !1, mt = !1;
|
|
839
|
+
function se(o, t, e) {
|
|
840
|
+
return yt || (yt = !0, console.warn(
|
|
841
|
+
"[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. The alias will be removed in 0.3.0."
|
|
842
|
+
)), Mt(o, t, e);
|
|
843
|
+
}
|
|
844
|
+
function oe(o, t, e) {
|
|
845
|
+
return mt || (mt = !0, console.warn(
|
|
846
|
+
"[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. The alias will be removed in 0.3.0."
|
|
847
|
+
)), at(o, t, e);
|
|
848
|
+
}
|
|
849
|
+
function vt(o, t, e, s, n) {
|
|
850
|
+
let r = s;
|
|
851
|
+
if (r.length === 4) {
|
|
852
|
+
if (r[0] !== 1)
|
|
853
|
+
throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${r[0]}.`);
|
|
854
|
+
r = [r[1], r[2], r[3]];
|
|
855
|
+
}
|
|
856
|
+
if (r.length !== 3)
|
|
857
|
+
throw new Error(
|
|
858
|
+
`decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(s)}.`
|
|
859
|
+
);
|
|
860
|
+
const a = r[0], i = r[1], c = r[2], f = t.length === 3 ? t[1] : t[0], y = t.length === 3 ? t[2] : t[1];
|
|
861
|
+
if (f === void 0 || y === void 0)
|
|
862
|
+
throw new Error(
|
|
863
|
+
`decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(t)}.`
|
|
864
|
+
);
|
|
865
|
+
const h = 4 + n.numClasses + a;
|
|
866
|
+
if (f !== h)
|
|
867
|
+
throw new Error(
|
|
868
|
+
`decodeYoloSeg: channels=${f} does not match 4 + numClasses(${n.numClasses}) + numMaskCoefs(${a}) = ${h}.`
|
|
869
|
+
);
|
|
870
|
+
if (e.length !== a * i * c)
|
|
871
|
+
throw new Error(
|
|
872
|
+
`decodeYoloSeg: prototype length ${e.length} does not match dims=${JSON.stringify(s)}.`
|
|
873
|
+
);
|
|
874
|
+
const l = at(o, t, {
|
|
875
|
+
numClasses: n.numClasses,
|
|
876
|
+
originalWidth: n.originalWidth,
|
|
877
|
+
originalHeight: n.originalHeight,
|
|
878
|
+
padLeft: n.padLeft,
|
|
879
|
+
padTop: n.padTop,
|
|
880
|
+
scale: n.scale,
|
|
881
|
+
confThreshold: n.confThreshold,
|
|
882
|
+
iouThreshold: n.iouThreshold,
|
|
883
|
+
maxDetections: n.maxDetections
|
|
884
|
+
});
|
|
885
|
+
if (l.anchorIndices.length === 0) return [];
|
|
886
|
+
const w = n.maskThreshold ?? 0.5, x = c / n.inputWidth, d = i / n.inputHeight, u = i * c, p = (4 + n.numClasses) * y, _ = [];
|
|
887
|
+
for (let g = 0; g < l.anchorIndices.length; g++) {
|
|
888
|
+
const S = l.anchorIndices[g], I = l.boxesXyxy[g * 4], v = l.boxesXyxy[g * 4 + 1], M = l.boxesXyxy[g * 4 + 2], C = l.boxesXyxy[g * 4 + 3], E = new gt(I, v, M, C), m = l.classIds[g], b = l.confidences[g], A = Math.max(0, Math.trunc(M) - Math.trunc(I)), T = Math.max(0, Math.trunc(C) - Math.trunc(v));
|
|
889
|
+
if (A === 0 || T === 0) {
|
|
890
|
+
_.push({ bbox: E, classId: m, confidence: b, mask: new B(new Uint8Array(0), 0, 0) });
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
const V = I * n.scale + n.padLeft, j = v * n.scale + n.padTop, P = M * n.scale + n.padLeft, D = C * n.scale + n.padTop, k = Math.max(0, Math.floor(V * x)), F = Math.max(0, Math.floor(j * d)), O = Math.min(c, Math.ceil(P * x)), Y = Math.min(i, Math.ceil(D * d));
|
|
894
|
+
if (O <= k || Y <= F) {
|
|
895
|
+
_.push({
|
|
896
|
+
bbox: E,
|
|
897
|
+
classId: m,
|
|
898
|
+
confidence: b,
|
|
899
|
+
mask: new B(new Uint8Array(A * T), A, T)
|
|
900
|
+
});
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
const z = O - k, Z = Y - F, ct = new Float32Array(z * Z);
|
|
904
|
+
for (let N = 0; N < Z; N++) {
|
|
905
|
+
const $t = F + N;
|
|
906
|
+
for (let K = 0; K < z; K++) {
|
|
907
|
+
const Ct = k + K;
|
|
908
|
+
let ht = 0;
|
|
909
|
+
for (let G = 0; G < a; G++) {
|
|
910
|
+
const Et = o[p + G * y + S], kt = e[G * u + $t * c + Ct];
|
|
911
|
+
ht += Et * kt;
|
|
912
|
+
}
|
|
913
|
+
ct[N * z + K] = Jt(ht);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
const Tt = Qt(ct, z, Z, A, T), q = new Uint8Array(A * T);
|
|
917
|
+
for (let N = 0; N < q.length; N++)
|
|
918
|
+
q[N] = Tt[N] >= w ? 255 : 0;
|
|
919
|
+
_.push({ bbox: E, classId: m, confidence: b, mask: new B(q, A, T) });
|
|
920
|
+
}
|
|
921
|
+
return _;
|
|
922
|
+
}
|
|
923
|
+
function Jt(o) {
|
|
924
|
+
if (o >= 0)
|
|
925
|
+
return 1 / (1 + Math.exp(-o));
|
|
926
|
+
const t = Math.exp(o);
|
|
927
|
+
return t / (1 + t);
|
|
928
|
+
}
|
|
929
|
+
let wt = !1;
|
|
930
|
+
function re(o, t, e, s, n) {
|
|
931
|
+
return wt || (wt = !0, console.warn(
|
|
932
|
+
"[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. The alias will be removed in 0.3.0."
|
|
933
|
+
)), vt(o, t, e, s, n);
|
|
934
|
+
}
|
|
935
|
+
function Qt(o, t, e, s, n) {
|
|
936
|
+
const r = new Float32Array(s * n);
|
|
937
|
+
if (s === 0 || n === 0 || t === 0 || e === 0)
|
|
938
|
+
return r;
|
|
939
|
+
if (s === t && n === e)
|
|
940
|
+
return r.set(o), r;
|
|
941
|
+
const a = t / s, i = e / n;
|
|
942
|
+
for (let c = 0; c < n; c++) {
|
|
943
|
+
const f = (c + 0.5) * i - 0.5, y = Math.max(0, Math.floor(f)), h = Math.min(e - 1, y + 1), l = Math.max(0, Math.min(1, f - y));
|
|
944
|
+
for (let w = 0; w < s; w++) {
|
|
945
|
+
const x = (w + 0.5) * a - 0.5, d = Math.max(0, Math.floor(x)), u = Math.min(t - 1, d + 1), p = Math.max(0, Math.min(1, x - d)), _ = o[y * t + d], g = o[y * t + u], S = o[h * t + d], I = o[h * t + u], v = _ * (1 - p) + g * p, M = S * (1 - p) + I * p;
|
|
946
|
+
r[c * s + w] = v * (1 - l) + M * l;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return r;
|
|
950
|
+
}
|
|
951
|
+
class it {
|
|
952
|
+
constructor(t) {
|
|
953
|
+
this._session = t;
|
|
954
|
+
}
|
|
955
|
+
_session;
|
|
956
|
+
/** The underlying {@link OrtSession} used to run inference. */
|
|
957
|
+
get session() {
|
|
958
|
+
return this._session;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
const Zt = [0.485, 0.456, 0.406], qt = [0.229, 0.224, 0.225];
|
|
962
|
+
class It extends it {
|
|
963
|
+
constructor(t, e, s, n, r, a, i) {
|
|
964
|
+
super(t), this._labels = e, this._names = s, this._inputSize = n, this._mean = r, this._std = a, this._applySoftmax = i;
|
|
965
|
+
}
|
|
966
|
+
_labels;
|
|
967
|
+
_names;
|
|
968
|
+
_inputSize;
|
|
969
|
+
_mean;
|
|
970
|
+
_std;
|
|
971
|
+
_applySoftmax;
|
|
972
|
+
/** Load the model and resolve labels. */
|
|
973
|
+
static async create(t, e) {
|
|
974
|
+
const s = await X.create(t, e), n = et(e.labels, { numClasses: e.numClasses }), r = {};
|
|
975
|
+
for (let a = 0; a < n.length; a++)
|
|
976
|
+
r[a] = n[a];
|
|
977
|
+
return new It(
|
|
978
|
+
s,
|
|
979
|
+
n,
|
|
980
|
+
r,
|
|
981
|
+
e.inputSize ?? [224, 224],
|
|
982
|
+
e.mean ?? Zt,
|
|
983
|
+
e.std ?? qt,
|
|
984
|
+
e.applySoftmax ?? !0
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
/** Class labels indexed by class id. */
|
|
988
|
+
get labels() {
|
|
989
|
+
return this._labels;
|
|
990
|
+
}
|
|
991
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
992
|
+
get names() {
|
|
993
|
+
return this._names;
|
|
994
|
+
}
|
|
995
|
+
/** Number of classes the model can predict. */
|
|
996
|
+
get numClasses() {
|
|
997
|
+
return this._labels.length;
|
|
998
|
+
}
|
|
999
|
+
/** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */
|
|
1000
|
+
async call(t, e = {}) {
|
|
1001
|
+
return this.predict(t, e);
|
|
1002
|
+
}
|
|
1003
|
+
/** Run classification on a single image. */
|
|
1004
|
+
async predict(t, e = {}) {
|
|
1005
|
+
const s = typeof t == "string" ? t : null, n = await st(t), r = this._preprocess(n), a = await this._session.run({ [this._session.inputName]: r }), i = this._session.outputNames[0];
|
|
1006
|
+
if (i === void 0)
|
|
1007
|
+
throw new Error("Classifier model has no outputs.");
|
|
1008
|
+
const c = a[i];
|
|
1009
|
+
if (c === void 0)
|
|
1010
|
+
throw new Error(
|
|
1011
|
+
`Classifier model output ${i} missing from run() result.`
|
|
1012
|
+
);
|
|
1013
|
+
const f = this._postprocess(c.data), { indices: y, values: h } = Kt(f, e.topK ?? null), l = [];
|
|
1014
|
+
for (let u = 0; u < y.length; u++) {
|
|
1015
|
+
const p = y[u], _ = this._labels[p] ?? `class_${p}`;
|
|
1016
|
+
l.push({
|
|
1017
|
+
classId: p,
|
|
1018
|
+
className: _,
|
|
1019
|
+
probability: h[u],
|
|
1020
|
+
cls: p,
|
|
1021
|
+
name: _,
|
|
1022
|
+
conf: h[u]
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
if (l.length === 0)
|
|
1026
|
+
throw new Error("Classifier produced no probabilities (empty output).");
|
|
1027
|
+
const w = l[0], x = {
|
|
1028
|
+
classId: w.classId,
|
|
1029
|
+
className: w.className,
|
|
1030
|
+
confidence: w.probability,
|
|
1031
|
+
cls: w.classId,
|
|
1032
|
+
name: w.className,
|
|
1033
|
+
conf: w.probability,
|
|
1034
|
+
image: n,
|
|
1035
|
+
probabilities: l
|
|
1036
|
+
}, d = [n.height, n.width];
|
|
1037
|
+
return [
|
|
1038
|
+
new Lt(
|
|
1039
|
+
new Nt(f),
|
|
1040
|
+
x,
|
|
1041
|
+
this._names,
|
|
1042
|
+
n,
|
|
1043
|
+
d,
|
|
1044
|
+
s
|
|
1045
|
+
)
|
|
1046
|
+
];
|
|
1047
|
+
}
|
|
1048
|
+
_preprocess(t) {
|
|
1049
|
+
const [e, s] = this._inputSize, n = bt(t, e, s), r = jt(n, this._mean, this._std), a = Q(r, n.width, n.height, 3);
|
|
1050
|
+
return rt(a, [1, 3, n.height, n.width]);
|
|
1051
|
+
}
|
|
1052
|
+
_postprocess(t) {
|
|
1053
|
+
return this._applySoftmax ? Pt(t) : new Float32Array(t);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
class At extends it {
|
|
1057
|
+
constructor(t, e, s, n, r, a, i, c) {
|
|
1058
|
+
super(t), this._head = e, this._labels = s, this._names = n, this._inputSize = r, this._confThreshold = a, this._iouThreshold = i, this._maxDetections = c;
|
|
1059
|
+
}
|
|
1060
|
+
_head;
|
|
1061
|
+
_labels;
|
|
1062
|
+
_names;
|
|
1063
|
+
_inputSize;
|
|
1064
|
+
_confThreshold;
|
|
1065
|
+
_iouThreshold;
|
|
1066
|
+
_maxDetections;
|
|
1067
|
+
/** Load the model and resolve labels. */
|
|
1068
|
+
static async create(t, e = {}) {
|
|
1069
|
+
const s = e.head ?? "yolo";
|
|
1070
|
+
if (s !== "yolo")
|
|
1071
|
+
throw new Error(`Unsupported detector head '${s}'. Supported: 'yolo'.`);
|
|
1072
|
+
const n = await X.create(t, e), r = et(e.labels ?? "coco", {
|
|
1073
|
+
numClasses: e.numClasses
|
|
1074
|
+
}), a = {};
|
|
1075
|
+
for (let i = 0; i < r.length; i++)
|
|
1076
|
+
a[i] = r[i];
|
|
1077
|
+
return new At(
|
|
1078
|
+
n,
|
|
1079
|
+
s,
|
|
1080
|
+
r,
|
|
1081
|
+
a,
|
|
1082
|
+
e.inputSize ?? [640, 640],
|
|
1083
|
+
e.confThreshold ?? 0.25,
|
|
1084
|
+
e.iouThreshold ?? 0.45,
|
|
1085
|
+
e.maxDetections ?? 300
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
/** The decoder family used to interpret the model's output. */
|
|
1089
|
+
get head() {
|
|
1090
|
+
return this._head;
|
|
1091
|
+
}
|
|
1092
|
+
/** Class labels indexed by class id. */
|
|
1093
|
+
get labels() {
|
|
1094
|
+
return this._labels;
|
|
1095
|
+
}
|
|
1096
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
1097
|
+
get names() {
|
|
1098
|
+
return this._names;
|
|
1099
|
+
}
|
|
1100
|
+
/** Number of classes the model predicts. */
|
|
1101
|
+
get numClasses() {
|
|
1102
|
+
return this._labels.length;
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Alias for {@link predict} — call the detector like a torch `nn.Module`.
|
|
1106
|
+
*
|
|
1107
|
+
* Use as `det.call(img)` since JavaScript class instances are not callable;
|
|
1108
|
+
* for direct invocation, prefer `det.predict(img)`. The full
|
|
1109
|
+
* {@link DetectorPredictOptions} (including `classes`) is supported.
|
|
1110
|
+
*/
|
|
1111
|
+
async call(t, e = {}) {
|
|
1112
|
+
return this.predict(t, e);
|
|
1113
|
+
}
|
|
1114
|
+
/** Run detection on a single image. */
|
|
1115
|
+
async predict(t, e = {}) {
|
|
1116
|
+
const s = typeof t == "string" ? t : null, n = await st(t), { tensor: r, scale: a, padLeft: i, padTop: c } = this._preprocess(n), f = await this._session.run({ [this._session.inputName]: r }), y = this._session.outputNames[0];
|
|
1117
|
+
if (y === void 0)
|
|
1118
|
+
throw new Error("Detector model has no outputs.");
|
|
1119
|
+
const h = f[y];
|
|
1120
|
+
if (h === void 0)
|
|
1121
|
+
throw new Error(`Detector model output ${y} missing from run() result.`);
|
|
1122
|
+
const l = Mt(h.data, h.dims, {
|
|
1123
|
+
originalWidth: n.width,
|
|
1124
|
+
originalHeight: n.height,
|
|
1125
|
+
padLeft: i,
|
|
1126
|
+
padTop: c,
|
|
1127
|
+
scale: a,
|
|
1128
|
+
confThreshold: e.confThreshold ?? this._confThreshold,
|
|
1129
|
+
iouThreshold: e.iouThreshold ?? this._iouThreshold,
|
|
1130
|
+
maxDetections: this._maxDetections
|
|
1131
|
+
}), x = (e.classes !== void 0 ? (() => {
|
|
1132
|
+
const u = new Set(e.classes);
|
|
1133
|
+
return l.filter((p) => u.has(p.classId));
|
|
1134
|
+
})() : l).map(
|
|
1135
|
+
(u) => this._buildResult(n, u.bbox, u.classId, u.confidence)
|
|
1136
|
+
), d = [n.height, n.width];
|
|
1137
|
+
return [
|
|
1138
|
+
new zt(
|
|
1139
|
+
this._buildBoxes(x, d),
|
|
1140
|
+
x,
|
|
1141
|
+
this._names,
|
|
1142
|
+
n,
|
|
1143
|
+
d,
|
|
1144
|
+
s
|
|
1145
|
+
)
|
|
1146
|
+
];
|
|
1147
|
+
}
|
|
1148
|
+
_preprocess(t) {
|
|
1149
|
+
const [e, s] = this._inputSize, n = _t(t, e, s), r = ot(n.image), a = Q(r, n.image.width, n.image.height, 3);
|
|
1150
|
+
return {
|
|
1151
|
+
tensor: rt(a, [1, 3, n.image.height, n.image.width]),
|
|
1152
|
+
scale: n.scale,
|
|
1153
|
+
padLeft: n.padLeft,
|
|
1154
|
+
padTop: n.padTop
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
_buildResult(t, e, s, n) {
|
|
1158
|
+
const [r, a, i, c] = e.asIntXyxy(), f = Math.max(0, r), y = Math.max(0, a), h = Math.min(t.width, i), l = Math.min(t.height, c);
|
|
1159
|
+
let w;
|
|
1160
|
+
if (h > f && l > y) {
|
|
1161
|
+
const d = h - f, u = l - y, p = new Uint8Array(d * u * 3);
|
|
1162
|
+
for (let _ = 0; _ < u; _++) {
|
|
1163
|
+
const g = ((y + _) * t.width + f) * 3;
|
|
1164
|
+
p.set(t.data.subarray(g, g + d * 3), _ * d * 3);
|
|
1165
|
+
}
|
|
1166
|
+
w = new L(p, d, u);
|
|
1167
|
+
} else
|
|
1168
|
+
w = new L(new Uint8Array(0), 0, 0);
|
|
1169
|
+
const x = this._names[s] ?? `class_${s}`;
|
|
1170
|
+
return {
|
|
1171
|
+
classId: s,
|
|
1172
|
+
className: x,
|
|
1173
|
+
confidence: n,
|
|
1174
|
+
bbox: e,
|
|
1175
|
+
cls: s,
|
|
1176
|
+
name: x,
|
|
1177
|
+
conf: n,
|
|
1178
|
+
box: e,
|
|
1179
|
+
croppedImage: w
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
_buildBoxes(t, e) {
|
|
1183
|
+
const s = t.length, n = new Float32Array(s * 4), r = new Int32Array(s), a = new Float32Array(s);
|
|
1184
|
+
for (let i = 0; i < s; i++) {
|
|
1185
|
+
const c = t[i];
|
|
1186
|
+
n[i * 4] = c.bbox.x1, n[i * 4 + 1] = c.bbox.y1, n[i * 4 + 2] = c.bbox.x2, n[i * 4 + 3] = c.bbox.y2, r[i] = c.classId, a[i] = c.confidence;
|
|
1187
|
+
}
|
|
1188
|
+
return new xt(n, r, a, e);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
class St extends it {
|
|
1192
|
+
constructor(t, e, s, n, r, a, i, c, f) {
|
|
1193
|
+
super(t), this._head = e, this._labels = s, this._names = n, this._inputSize = r, this._confThreshold = a, this._iouThreshold = i, this._maxDetections = c, this._maskThreshold = f;
|
|
1194
|
+
}
|
|
1195
|
+
_head;
|
|
1196
|
+
_labels;
|
|
1197
|
+
_names;
|
|
1198
|
+
_inputSize;
|
|
1199
|
+
_confThreshold;
|
|
1200
|
+
_iouThreshold;
|
|
1201
|
+
_maxDetections;
|
|
1202
|
+
_maskThreshold;
|
|
1203
|
+
/** Load the model and resolve labels. */
|
|
1204
|
+
static async create(t, e = {}) {
|
|
1205
|
+
const s = e.head ?? "yolo-seg";
|
|
1206
|
+
if (s !== "yolo-seg")
|
|
1207
|
+
throw new Error(`Unsupported segmenter head '${s}'. Supported: 'yolo-seg'.`);
|
|
1208
|
+
const n = await X.create(t, e), r = et(e.labels ?? "coco", {
|
|
1209
|
+
numClasses: e.numClasses
|
|
1210
|
+
}), a = {};
|
|
1211
|
+
for (let i = 0; i < r.length; i++)
|
|
1212
|
+
a[i] = r[i];
|
|
1213
|
+
return new St(
|
|
1214
|
+
n,
|
|
1215
|
+
s,
|
|
1216
|
+
r,
|
|
1217
|
+
a,
|
|
1218
|
+
e.inputSize ?? [640, 640],
|
|
1219
|
+
e.confThreshold ?? 0.25,
|
|
1220
|
+
e.iouThreshold ?? 0.45,
|
|
1221
|
+
e.maxDetections ?? 300,
|
|
1222
|
+
e.maskThreshold ?? 0.5
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
/** The decoder family used to interpret the model's output. */
|
|
1226
|
+
get head() {
|
|
1227
|
+
return this._head;
|
|
1228
|
+
}
|
|
1229
|
+
/** Class labels indexed by class id. */
|
|
1230
|
+
get labels() {
|
|
1231
|
+
return this._labels;
|
|
1232
|
+
}
|
|
1233
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
1234
|
+
get names() {
|
|
1235
|
+
return this._names;
|
|
1236
|
+
}
|
|
1237
|
+
/** Number of classes the model predicts. */
|
|
1238
|
+
get numClasses() {
|
|
1239
|
+
return this._labels.length;
|
|
1240
|
+
}
|
|
1241
|
+
/** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */
|
|
1242
|
+
async call(t, e = {}) {
|
|
1243
|
+
return this.predict(t, e);
|
|
1244
|
+
}
|
|
1245
|
+
/** Run instance segmentation on a single image. */
|
|
1246
|
+
async predict(t, e = {}) {
|
|
1247
|
+
const s = typeof t == "string" ? t : null, n = await st(t), { tensor: r, scale: a, padLeft: i, padTop: c } = this._preprocess(n), f = await this._session.run({ [this._session.inputName]: r }), { perAnchor: y, prototypes: h } = this._splitOutputs(f), l = vt(
|
|
1248
|
+
y.data,
|
|
1249
|
+
y.dims,
|
|
1250
|
+
h.data,
|
|
1251
|
+
h.dims,
|
|
1252
|
+
{
|
|
1253
|
+
numClasses: this._labels.length,
|
|
1254
|
+
inputWidth: this._inputSize[0],
|
|
1255
|
+
inputHeight: this._inputSize[1],
|
|
1256
|
+
originalWidth: n.width,
|
|
1257
|
+
originalHeight: n.height,
|
|
1258
|
+
padLeft: i,
|
|
1259
|
+
padTop: c,
|
|
1260
|
+
scale: a,
|
|
1261
|
+
confThreshold: e.confThreshold ?? this._confThreshold,
|
|
1262
|
+
iouThreshold: e.iouThreshold ?? this._iouThreshold,
|
|
1263
|
+
maxDetections: this._maxDetections,
|
|
1264
|
+
maskThreshold: this._maskThreshold
|
|
1265
|
+
}
|
|
1266
|
+
), x = (e.classes !== void 0 ? (() => {
|
|
1267
|
+
const u = new Set(e.classes);
|
|
1268
|
+
return l.filter((p) => u.has(p.classId));
|
|
1269
|
+
})() : l).map(
|
|
1270
|
+
(u) => this._buildResult(n, u.bbox, u.classId, u.confidence, u.mask)
|
|
1271
|
+
), d = [n.height, n.width];
|
|
1272
|
+
return [
|
|
1273
|
+
new Dt(
|
|
1274
|
+
this._buildBoxes(x, d),
|
|
1275
|
+
this._buildMasks(x, d),
|
|
1276
|
+
x,
|
|
1277
|
+
this._names,
|
|
1278
|
+
n,
|
|
1279
|
+
d,
|
|
1280
|
+
s
|
|
1281
|
+
)
|
|
1282
|
+
];
|
|
1283
|
+
}
|
|
1284
|
+
_preprocess(t) {
|
|
1285
|
+
const [e, s] = this._inputSize, n = _t(t, e, s), r = ot(n.image), a = Q(r, n.image.width, n.image.height, 3);
|
|
1286
|
+
return {
|
|
1287
|
+
tensor: rt(a, [1, 3, n.image.height, n.image.width]),
|
|
1288
|
+
scale: n.scale,
|
|
1289
|
+
padLeft: n.padLeft,
|
|
1290
|
+
padTop: n.padTop
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
_splitOutputs(t) {
|
|
1294
|
+
let e, s;
|
|
1295
|
+
for (const n of this._session.outputNames) {
|
|
1296
|
+
const r = t[n];
|
|
1297
|
+
r !== void 0 && (r.dims.length === 3 && e === void 0 ? e = r : r.dims.length === 4 && s === void 0 && (s = r));
|
|
1298
|
+
}
|
|
1299
|
+
if (e === void 0 || s === void 0) {
|
|
1300
|
+
const n = this._session.outputNames.map(
|
|
1301
|
+
(r) => `${r}: ${JSON.stringify(t[r]?.dims ?? [])}`
|
|
1302
|
+
);
|
|
1303
|
+
throw new Error(
|
|
1304
|
+
`Segmenter expected one 3-D and one 4-D output, got [${n.join(", ")}].`
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
return { perAnchor: e, prototypes: s };
|
|
1308
|
+
}
|
|
1309
|
+
_buildResult(t, e, s, n, r) {
|
|
1310
|
+
const [a, i, c, f] = e.asIntXyxy(), y = Math.max(0, a), h = Math.max(0, i), l = Math.min(t.width, c), w = Math.min(t.height, f);
|
|
1311
|
+
let x, d = r;
|
|
1312
|
+
if (l > y && w > h && r.data.length > 0) {
|
|
1313
|
+
const p = l - y, _ = w - h, g = Math.min(r.width, p), S = Math.min(r.height, _), I = new Uint8Array(g * S * 3);
|
|
1314
|
+
for (let v = 0; v < S; v++) {
|
|
1315
|
+
const M = ((h + v) * t.width + y) * 3, C = v * g * 3, E = v * r.width;
|
|
1316
|
+
for (let m = 0; m < g; m++)
|
|
1317
|
+
if (r.data[E + m] !== 0) {
|
|
1318
|
+
const A = M + m * 3, T = C + m * 3;
|
|
1319
|
+
I[T] = t.data[A], I[T + 1] = t.data[A + 1], I[T + 2] = t.data[A + 2];
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
if (x = new L(I, g, S), g !== r.width || S !== r.height) {
|
|
1323
|
+
const v = new Uint8Array(g * S);
|
|
1324
|
+
for (let M = 0; M < S; M++)
|
|
1325
|
+
v.set(
|
|
1326
|
+
r.data.subarray(M * r.width, M * r.width + g),
|
|
1327
|
+
M * g
|
|
1328
|
+
);
|
|
1329
|
+
d = new B(v, g, S);
|
|
1330
|
+
}
|
|
1331
|
+
} else
|
|
1332
|
+
d = new B(new Uint8Array(0), 0, 0), x = new L(new Uint8Array(0), 0, 0);
|
|
1333
|
+
const u = this._names[s] ?? `class_${s}`;
|
|
1334
|
+
return {
|
|
1335
|
+
classId: s,
|
|
1336
|
+
className: u,
|
|
1337
|
+
confidence: n,
|
|
1338
|
+
bbox: e,
|
|
1339
|
+
cls: s,
|
|
1340
|
+
name: u,
|
|
1341
|
+
conf: n,
|
|
1342
|
+
box: e,
|
|
1343
|
+
mask: d,
|
|
1344
|
+
segmentedImage: x
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
_buildBoxes(t, e) {
|
|
1348
|
+
const s = t.length, n = new Float32Array(s * 4), r = new Int32Array(s), a = new Float32Array(s);
|
|
1349
|
+
for (let i = 0; i < s; i++) {
|
|
1350
|
+
const c = t[i];
|
|
1351
|
+
n[i * 4] = c.bbox.x1, n[i * 4 + 1] = c.bbox.y1, n[i * 4 + 2] = c.bbox.x2, n[i * 4 + 3] = c.bbox.y2, r[i] = c.classId, a[i] = c.confidence;
|
|
1352
|
+
}
|
|
1353
|
+
return new xt(n, r, a, e);
|
|
1354
|
+
}
|
|
1355
|
+
_buildMasks(t, e) {
|
|
1356
|
+
const s = new Float32Array(t.length * 4);
|
|
1357
|
+
for (let n = 0; n < t.length; n++) {
|
|
1358
|
+
const r = t[n];
|
|
1359
|
+
s[n * 4] = r.bbox.x1, s[n * 4 + 1] = r.bbox.y1, s[n * 4 + 2] = r.bbox.x2, s[n * 4 + 3] = r.bbox.y2;
|
|
1360
|
+
}
|
|
1361
|
+
return new Ot(
|
|
1362
|
+
t.map((n) => n.mask),
|
|
1363
|
+
s,
|
|
1364
|
+
e
|
|
1365
|
+
);
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
const ae = "0.2.1";
|
|
1369
|
+
export {
|
|
1370
|
+
gt as BoundingBox,
|
|
1371
|
+
xt as Boxes,
|
|
1372
|
+
Yt as COCO_CLASSES,
|
|
1373
|
+
Lt as ClassificationResults,
|
|
1374
|
+
It as Classifier,
|
|
1375
|
+
ut as DEFAULT_PROVIDERS,
|
|
1376
|
+
zt as DetectionResults,
|
|
1377
|
+
At as Detector,
|
|
1378
|
+
$ as ImageLoadError,
|
|
1379
|
+
lt as InferenceError,
|
|
1380
|
+
J as LabelMapError,
|
|
1381
|
+
B as Mask,
|
|
1382
|
+
Ot as Masks,
|
|
1383
|
+
Ft as ModelLoadError,
|
|
1384
|
+
X as OrtSession,
|
|
1385
|
+
R as OrtVisionError,
|
|
1386
|
+
Nt as Probs,
|
|
1387
|
+
Wt as ProviderNotAvailableError,
|
|
1388
|
+
L as RGBImage,
|
|
1389
|
+
Dt as SegmentationResults,
|
|
1390
|
+
St as Segmenter,
|
|
1391
|
+
ae as VERSION,
|
|
1392
|
+
it as VisionTask,
|
|
1393
|
+
Ht as batchedNms,
|
|
1394
|
+
Mt as decodeYolo,
|
|
1395
|
+
at as decodeYoloAnchors,
|
|
1396
|
+
vt as decodeYoloSeg,
|
|
1397
|
+
se as decodeYoloV8,
|
|
1398
|
+
oe as decodeYoloV8Anchors,
|
|
1399
|
+
re as decodeYoloV8Seg,
|
|
1400
|
+
ee as fromCv2,
|
|
1401
|
+
_t as letterbox,
|
|
1402
|
+
st as loadImage,
|
|
1403
|
+
Gt as nms,
|
|
1404
|
+
jt as normalize,
|
|
1405
|
+
bt as resize,
|
|
1406
|
+
et as resolveLabels,
|
|
1407
|
+
Bt as resolveProviders,
|
|
1408
|
+
Pt as softmax,
|
|
1409
|
+
Q as toCHW,
|
|
1410
|
+
ne as toCv2,
|
|
1411
|
+
ot as toFloat32,
|
|
1412
|
+
rt as toFloat32Tensor,
|
|
1413
|
+
te as toTensor,
|
|
1414
|
+
Kt as topK
|
|
1415
|
+
};
|
|
1416
|
+
//# sourceMappingURL=vision.js.map
|