tempest-react-sdk 0.11.0 → 0.13.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/styles.css +1 -1
- package/dist/tempest-react-sdk.cjs +3 -3
- package/dist/tempest-react-sdk.cjs.map +1 -1
- package/dist/tempest-react-sdk.d.ts +60 -0
- package/dist/tempest-react-sdk.js +2546 -2478
- package/dist/tempest-react-sdk.js.map +1 -1
- 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.d.ts
ADDED
|
@@ -0,0 +1,1030 @@
|
|
|
1
|
+
import type * as ort from 'onnxruntime-web';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-class NMS — boxes are suppressed only by other boxes of the same class.
|
|
5
|
+
*
|
|
6
|
+
* Mirrors `torchvision.ops.batched_nms`.
|
|
7
|
+
*
|
|
8
|
+
* @param boxes Flat array of length `4 * N` in xyxy order.
|
|
9
|
+
* @param scores Detection score per box, length `N`.
|
|
10
|
+
* @param idxs Class index per box, length `N`. Boxes with different `idxs`
|
|
11
|
+
* never suppress each other.
|
|
12
|
+
* @param iouThreshold IoU threshold for suppression within a class.
|
|
13
|
+
*/
|
|
14
|
+
export declare function batchedNms(boxes: Float32Array, scores: Float32Array, idxs: Int32Array, iouThreshold: number): Int32Array;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Axis-aligned bounding box in absolute pixel coordinates (xyxy format).
|
|
18
|
+
*
|
|
19
|
+
* Coordinates refer to the original input image (before any internal resize),
|
|
20
|
+
* so callers can map detections back onto their source image without any
|
|
21
|
+
* additional bookkeeping.
|
|
22
|
+
*/
|
|
23
|
+
export declare class BoundingBox {
|
|
24
|
+
readonly x1: number;
|
|
25
|
+
readonly y1: number;
|
|
26
|
+
readonly x2: number;
|
|
27
|
+
readonly y2: number;
|
|
28
|
+
constructor(x1: number, y1: number, x2: number, y2: number);
|
|
29
|
+
/** Box width in pixels (clamped to non-negative). */
|
|
30
|
+
get width(): number;
|
|
31
|
+
/** Box height in pixels (clamped to non-negative). */
|
|
32
|
+
get height(): number;
|
|
33
|
+
/** Box area in pixels squared. */
|
|
34
|
+
get area(): number;
|
|
35
|
+
/** The box as `[x1, y1, x2, y2]` in absolute pixels (Ultralytics-style). */
|
|
36
|
+
get xyxy(): readonly [number, number, number, number];
|
|
37
|
+
/**
|
|
38
|
+
* The box as `[cx, cy, w, h]` with `(cx, cy)` at the center.
|
|
39
|
+
*
|
|
40
|
+
* Matches Ultralytics' `boxes.xywh` and YOLO's native head format. For the
|
|
41
|
+
* top-left `[x, y, w, h]` convention, use {@link asXywh}.
|
|
42
|
+
*/
|
|
43
|
+
get xywh(): readonly [number, number, number, number];
|
|
44
|
+
/**
|
|
45
|
+
* The box as `[x1, y1, x2, y2]` normalized to `[0, 1]`.
|
|
46
|
+
*
|
|
47
|
+
* @param origShape `[height, width]` of the source image, in pixels.
|
|
48
|
+
*/
|
|
49
|
+
xyxyn(origShape: readonly [number, number]): readonly [number, number, number, number];
|
|
50
|
+
/**
|
|
51
|
+
* The box as `[cx, cy, w, h]` normalized to `[0, 1]`.
|
|
52
|
+
*
|
|
53
|
+
* @param origShape `[height, width]` of the source image, in pixels.
|
|
54
|
+
*/
|
|
55
|
+
xywhn(origShape: readonly [number, number]): readonly [number, number, number, number];
|
|
56
|
+
/** Returns `[x1, y1, x2, y2]`. */
|
|
57
|
+
asXyxy(): readonly [number, number, number, number];
|
|
58
|
+
/**
|
|
59
|
+
* Returns `[x, y, width, height]` with `(x, y)` at the **top-left**.
|
|
60
|
+
*
|
|
61
|
+
* Note: this is the top-left convention. Ultralytics' `xywh` getter uses
|
|
62
|
+
* **center** coordinates — for that, read {@link xywh}.
|
|
63
|
+
*/
|
|
64
|
+
asXywh(): readonly [number, number, number, number];
|
|
65
|
+
/** Returns `[x1, y1, x2, y2]` truncated to integers, useful for slicing arrays. */
|
|
66
|
+
asIntXyxy(): readonly [number, number, number, number];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Bulk numpy-style view of detected boxes for a single image.
|
|
71
|
+
*
|
|
72
|
+
* Mirrors Ultralytics' `Boxes` interface. Coordinates in {@link xyxy} and
|
|
73
|
+
* {@link xywh} are absolute pixels in the original image; the `*n` variants
|
|
74
|
+
* are normalized to `[0, 1]` using `origShape`.
|
|
75
|
+
*/
|
|
76
|
+
export declare class Boxes {
|
|
77
|
+
readonly xyxy: Float32Array;
|
|
78
|
+
readonly cls: Int32Array;
|
|
79
|
+
readonly conf: Float32Array;
|
|
80
|
+
readonly origShape: readonly [number, number];
|
|
81
|
+
/**
|
|
82
|
+
* @param xyxy Flat array of length `4 * N` in `[x1, y1, x2, y2, ...]` order.
|
|
83
|
+
* @param cls One class index per box, length `N`.
|
|
84
|
+
* @param conf One confidence per box, length `N`.
|
|
85
|
+
* @param origShape `[height, width]` of the original image.
|
|
86
|
+
*/
|
|
87
|
+
constructor(xyxy: Float32Array, cls: Int32Array, conf: Float32Array, origShape: readonly [number, number]);
|
|
88
|
+
/** Number of detected boxes. */
|
|
89
|
+
get length(): number;
|
|
90
|
+
/** `[N, 4]` shape of the `xyxy` view. */
|
|
91
|
+
get shape(): readonly [number, number];
|
|
92
|
+
/** Boxes as `[N, 4]` `[cx, cy, w, h]` flat array in absolute pixels. */
|
|
93
|
+
get xywh(): Float32Array;
|
|
94
|
+
/** Boxes as `[N, 4]` `[x1, y1, x2, y2]` normalized to `[0, 1]`. */
|
|
95
|
+
get xyxyn(): Float32Array;
|
|
96
|
+
/** Boxes as `[N, 4]` `[cx, cy, w, h]` normalized to `[0, 1]`. */
|
|
97
|
+
get xywhn(): Float32Array;
|
|
98
|
+
/**
|
|
99
|
+
* Concatenated `[N, 6]` array of `[x1, y1, x2, y2, conf, cls]`.
|
|
100
|
+
*
|
|
101
|
+
* Matches Ultralytics' `boxes.data`.
|
|
102
|
+
*/
|
|
103
|
+
get data(): Float32Array;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Output of an image classification inference.
|
|
108
|
+
*/
|
|
109
|
+
export declare interface ClassificationResult {
|
|
110
|
+
readonly classId: number;
|
|
111
|
+
readonly className: string;
|
|
112
|
+
readonly confidence: number;
|
|
113
|
+
/** Alias for `classId` (Ultralytics-style). */
|
|
114
|
+
readonly cls: number;
|
|
115
|
+
/** Alias for `className`. */
|
|
116
|
+
readonly name: string;
|
|
117
|
+
/** Alias for `confidence` (Ultralytics-style). */
|
|
118
|
+
readonly conf: number;
|
|
119
|
+
/** The original input image as an HWC RGB uint8 array. */
|
|
120
|
+
readonly image: RGBImage;
|
|
121
|
+
/**
|
|
122
|
+
* Probabilities per class, sorted in descending order. The first entry
|
|
123
|
+
* mirrors `classId`, `className`, and `confidence`. When `topK` was passed
|
|
124
|
+
* to `predict`, the array is truncated to that length.
|
|
125
|
+
*/
|
|
126
|
+
readonly probabilities: readonly ClassProbability[];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Per-image classification envelope (Ultralytics-style `Results`).
|
|
131
|
+
*/
|
|
132
|
+
export declare class ClassificationResults {
|
|
133
|
+
readonly probs: Probs;
|
|
134
|
+
readonly result: ClassificationResult;
|
|
135
|
+
readonly names: Readonly<Record<number, string>>;
|
|
136
|
+
readonly origImg: RGBImage;
|
|
137
|
+
readonly origShape: readonly [number, number];
|
|
138
|
+
readonly path: string | null;
|
|
139
|
+
readonly speed: Readonly<Record<string, number>>;
|
|
140
|
+
constructor(probs: Probs, result: ClassificationResult, names: Readonly<Record<number, string>>, origImg: RGBImage, origShape: readonly [number, number], path?: string | null, speed?: Readonly<Record<string, number>>);
|
|
141
|
+
/** Top-1 class index (Ultralytics-style alias). */
|
|
142
|
+
get cls(): number;
|
|
143
|
+
/** Top-1 confidence (Ultralytics-style alias). */
|
|
144
|
+
get conf(): number;
|
|
145
|
+
/** Top-1 class name. */
|
|
146
|
+
get name(): string;
|
|
147
|
+
/** Per-class probability list, sorted descending (legacy field). */
|
|
148
|
+
get probabilities(): readonly ClassificationResult["probabilities"][number][];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Image classifier wrapping an ONNX model with ImageNet-style preprocessing.
|
|
153
|
+
*
|
|
154
|
+
* `predict()` returns `Promise<ClassificationResults[]>` (length 1 for a
|
|
155
|
+
* single image), mirroring Ultralytics' API. The envelope exposes a `probs`
|
|
156
|
+
* collection (`top1`, `top1conf`, `top5`, `top5conf`, `data`) plus the
|
|
157
|
+
* legacy per-class probability list with names already resolved.
|
|
158
|
+
*
|
|
159
|
+
* Defaults: 224×224 RGB input, `float32` normalized with ImageNet mean/std,
|
|
160
|
+
* NCHW layout, batch size 1, softmax applied to the raw output.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const clf = await Classifier.create("/models/resnet50.onnx", {
|
|
165
|
+
* labels: ["tench", "goldfish", ...] // 1000 ImageNet labels
|
|
166
|
+
* });
|
|
167
|
+
* const r = (await clf.predict("/images/dog.jpg"))[0];
|
|
168
|
+
* console.log(r.cls, r.conf, r.name);
|
|
169
|
+
* console.log(r.probs.top5, r.probs.top5conf);
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export declare class Classifier extends VisionTask {
|
|
173
|
+
private readonly _labels;
|
|
174
|
+
private readonly _names;
|
|
175
|
+
private readonly _inputSize;
|
|
176
|
+
private readonly _mean;
|
|
177
|
+
private readonly _std;
|
|
178
|
+
private readonly _applySoftmax;
|
|
179
|
+
private constructor();
|
|
180
|
+
/** Load the model and resolve labels. */
|
|
181
|
+
static create(model: ModelSource, options: ClassifierOptions): Promise<Classifier>;
|
|
182
|
+
/** Class labels indexed by class id. */
|
|
183
|
+
get labels(): readonly string[];
|
|
184
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
185
|
+
get names(): Readonly<Record<number, string>>;
|
|
186
|
+
/** Number of classes the model can predict. */
|
|
187
|
+
get numClasses(): number;
|
|
188
|
+
/** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */
|
|
189
|
+
call(image: ImageInput, options?: ClassifierPredictOptions): Promise<ClassificationResults[]>;
|
|
190
|
+
/** Run classification on a single image. */
|
|
191
|
+
predict(image: ImageInput, options?: ClassifierPredictOptions): Promise<ClassificationResults[]>;
|
|
192
|
+
private _preprocess;
|
|
193
|
+
private _postprocess;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export declare interface ClassifierOptions extends OrtSessionOptions {
|
|
197
|
+
/** Class label spec — see {@link resolveLabels}. */
|
|
198
|
+
readonly labels: LabelSpec;
|
|
199
|
+
/**
|
|
200
|
+
* Number of classes the model can predict. Required when `labels` is `null`
|
|
201
|
+
* or when you want to validate that the supplied labels match the model.
|
|
202
|
+
*/
|
|
203
|
+
readonly numClasses?: number;
|
|
204
|
+
/** Model input `[width, height]` in pixels. Defaults to `[224, 224]`. */
|
|
205
|
+
readonly inputSize?: readonly [number, number];
|
|
206
|
+
/** Per-channel RGB mean used for normalization. Defaults to ImageNet. */
|
|
207
|
+
readonly mean?: readonly [number, number, number];
|
|
208
|
+
/** Per-channel RGB standard deviation. Defaults to ImageNet. */
|
|
209
|
+
readonly std?: readonly [number, number, number];
|
|
210
|
+
/**
|
|
211
|
+
* If `true` (default), apply softmax to the raw model output. Set to
|
|
212
|
+
* `false` for models whose final layer already produces a probability
|
|
213
|
+
* distribution.
|
|
214
|
+
*/
|
|
215
|
+
readonly applySoftmax?: boolean;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export declare interface ClassifierPredictOptions {
|
|
219
|
+
/**
|
|
220
|
+
* If set, the per-class probability list in `results[0].result.probabilities`
|
|
221
|
+
* is truncated to the top-K entries. The bulk `probs` view always exposes
|
|
222
|
+
* the full vector.
|
|
223
|
+
*/
|
|
224
|
+
readonly topK?: number;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Probability assigned to a single class for a classification prediction.
|
|
229
|
+
*
|
|
230
|
+
* `cls` / `name` / `conf` are Ultralytics-style aliases populated alongside
|
|
231
|
+
* the verbose `classId` / `className` / `probability` fields.
|
|
232
|
+
*/
|
|
233
|
+
export declare interface ClassProbability {
|
|
234
|
+
readonly classId: number;
|
|
235
|
+
readonly className: string;
|
|
236
|
+
readonly probability: number;
|
|
237
|
+
/** Alias for `classId` (Ultralytics-style). */
|
|
238
|
+
readonly cls: number;
|
|
239
|
+
/** Alias for `className`. */
|
|
240
|
+
readonly name: string;
|
|
241
|
+
/** Alias for `probability` (Ultralytics-style). */
|
|
242
|
+
readonly conf: number;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** COCO 2017 80-class labels in canonical class-id order. */
|
|
246
|
+
export declare const COCO_CLASSES: readonly string[];
|
|
247
|
+
|
|
248
|
+
export declare interface DecodedAnchors {
|
|
249
|
+
/** Indices into the original `numAnchors` axis, in descending confidence order. */
|
|
250
|
+
readonly anchorIndices: Int32Array;
|
|
251
|
+
/** `[k, 4]` boxes in original-image pixel coords, flat row-major xyxy. */
|
|
252
|
+
readonly boxesXyxy: Float32Array;
|
|
253
|
+
/** Predicted class id per survivor. */
|
|
254
|
+
readonly classIds: Int32Array;
|
|
255
|
+
/** Confidence per survivor. */
|
|
256
|
+
readonly confidences: Float32Array;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export declare interface DecodedDetection {
|
|
260
|
+
readonly bbox: BoundingBox;
|
|
261
|
+
readonly classId: number;
|
|
262
|
+
readonly confidence: number;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export declare interface DecodedSegmentation {
|
|
266
|
+
readonly bbox: BoundingBox;
|
|
267
|
+
readonly classId: number;
|
|
268
|
+
readonly confidence: number;
|
|
269
|
+
/** Binary mask cropped to `bbox`. Width/height match `bbox.asIntXyxy()` extents. */
|
|
270
|
+
readonly mask: Mask;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Decode an anchor-free YOLO detection output into a list of detections.
|
|
275
|
+
*
|
|
276
|
+
* Works for **YOLOv8 / v9 / v10 / v11 / v12** detect heads.
|
|
277
|
+
*
|
|
278
|
+
* Expected raw shape: `[1, 4 + numClasses, N]`. `numClasses` is inferred
|
|
279
|
+
* from the channel count.
|
|
280
|
+
*/
|
|
281
|
+
export declare function decodeYolo(output: Float32Array, outputDims: readonly number[], options: DecodeYoloOptions): DecodedDetection[];
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Shared YOLO per-anchor decode used by both detection and segmentation
|
|
285
|
+
* (v8 / v9 / v10 / v11 / v12).
|
|
286
|
+
*
|
|
287
|
+
* Only the first `4 + numClasses` channels are read; later channels (e.g.
|
|
288
|
+
* mask coefficients) are ignored — callers can fetch them via the returned
|
|
289
|
+
* {@link DecodedAnchors.anchorIndices}.
|
|
290
|
+
*
|
|
291
|
+
* @param data Flat per-anchor output, length `channels * numAnchors`.
|
|
292
|
+
* @param dims Dims as reported by ORT, e.g. `[1, 84, 8400]` (det) or
|
|
293
|
+
* `[1, 116, 8400]` (seg). The leading batch dim must be 1.
|
|
294
|
+
*/
|
|
295
|
+
export declare function decodeYoloAnchors(data: Float32Array, dims: readonly number[], options: DecodeYoloAnchorsOptions): DecodedAnchors;
|
|
296
|
+
|
|
297
|
+
export declare interface DecodeYoloAnchorsOptions {
|
|
298
|
+
/** Number of class-score channels following the 4 box channels. */
|
|
299
|
+
readonly numClasses: number;
|
|
300
|
+
readonly originalWidth: number;
|
|
301
|
+
readonly originalHeight: number;
|
|
302
|
+
readonly padLeft: number;
|
|
303
|
+
readonly padTop: number;
|
|
304
|
+
readonly scale: number;
|
|
305
|
+
readonly confThreshold: number;
|
|
306
|
+
readonly iouThreshold: number;
|
|
307
|
+
readonly maxDetections: number;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export declare interface DecodeYoloOptions {
|
|
311
|
+
readonly originalWidth: number;
|
|
312
|
+
readonly originalHeight: number;
|
|
313
|
+
readonly padLeft: number;
|
|
314
|
+
readonly padTop: number;
|
|
315
|
+
readonly scale: number;
|
|
316
|
+
readonly confThreshold: number;
|
|
317
|
+
readonly iouThreshold: number;
|
|
318
|
+
readonly maxDetections: number;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Decode YOLO segmentation raw outputs into a list of segmented instances.
|
|
323
|
+
*
|
|
324
|
+
* Compatible with YOLOv8-seg / YOLOv11-seg.
|
|
325
|
+
*
|
|
326
|
+
* @param perAnchorData Flat `output0`, length `(4 + numClasses + numMaskCoefs) * numAnchors`.
|
|
327
|
+
* @param perAnchorDims Dims as reported by ORT, e.g. `[1, 116, 8400]`.
|
|
328
|
+
* @param prototypeData Flat `output1`, length `numMaskCoefs * maskH * maskW`.
|
|
329
|
+
* @param prototypeDims Dims as reported by ORT, e.g. `[1, 32, 160, 160]`.
|
|
330
|
+
*/
|
|
331
|
+
export declare function decodeYoloSeg(perAnchorData: Float32Array, perAnchorDims: readonly number[], prototypeData: Float32Array, prototypeDims: readonly number[], options: DecodeYoloSegOptions): DecodedSegmentation[];
|
|
332
|
+
|
|
333
|
+
export declare interface DecodeYoloSegOptions {
|
|
334
|
+
readonly numClasses: number;
|
|
335
|
+
/** Model input `[width, height]` (post-letterbox). */
|
|
336
|
+
readonly inputWidth: number;
|
|
337
|
+
readonly inputHeight: number;
|
|
338
|
+
/** Original image `[width, height]`. */
|
|
339
|
+
readonly originalWidth: number;
|
|
340
|
+
readonly originalHeight: number;
|
|
341
|
+
/** Letterbox horizontal padding in input-tensor pixels. */
|
|
342
|
+
readonly padLeft: number;
|
|
343
|
+
/** Letterbox vertical padding in input-tensor pixels. */
|
|
344
|
+
readonly padTop: number;
|
|
345
|
+
/** Letterbox scale factor. */
|
|
346
|
+
readonly scale: number;
|
|
347
|
+
readonly confThreshold: number;
|
|
348
|
+
readonly iouThreshold: number;
|
|
349
|
+
readonly maxDetections: number;
|
|
350
|
+
/** Probability cutoff applied to the soft mask. Defaults to `0.5`. */
|
|
351
|
+
readonly maskThreshold?: number;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* @deprecated since 0.2.0 — use {@link decodeYolo}. Same behavior; the
|
|
356
|
+
* decoder covers v8/v9/v10/v11/v12 detect heads. Will be removed in 0.3.0.
|
|
357
|
+
*/
|
|
358
|
+
export declare function decodeYoloV8(output: Float32Array, outputDims: readonly number[], options: DecodeYoloOptions): DecodedDetection[];
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @deprecated since 0.2.0 — use {@link decodeYoloAnchors}. Will be removed in 0.3.0.
|
|
362
|
+
*/
|
|
363
|
+
export declare function decodeYoloV8Anchors(data: Float32Array, dims: readonly number[], options: DecodeYoloAnchorsOptions): DecodedAnchors;
|
|
364
|
+
|
|
365
|
+
/** @deprecated since 0.2.0 — use {@link DecodeYoloAnchorsOptions}. */
|
|
366
|
+
export declare type DecodeYoloV8AnchorsOptions = DecodeYoloAnchorsOptions;
|
|
367
|
+
|
|
368
|
+
/** @deprecated since 0.2.0 — use {@link DecodeYoloOptions}. */
|
|
369
|
+
export declare type DecodeYoloV8Options = DecodeYoloOptions;
|
|
370
|
+
|
|
371
|
+
/** @deprecated since 0.2.0 — use {@link decodeYoloSeg}. Will be removed in 0.3.0. */
|
|
372
|
+
export declare function decodeYoloV8Seg(perAnchorData: Float32Array, perAnchorDims: readonly number[], prototypeData: Float32Array, prototypeDims: readonly number[], options: DecodeYoloSegOptions): DecodedSegmentation[];
|
|
373
|
+
|
|
374
|
+
/** @deprecated since 0.2.0 — use {@link DecodeYoloSegOptions}. */
|
|
375
|
+
export declare type DecodeYoloV8SegOptions = DecodeYoloSegOptions;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Default execution provider preference order for browser ORT.
|
|
379
|
+
*
|
|
380
|
+
* `webgpu` is tried first when available; ORT-Web falls back to `wasm`
|
|
381
|
+
* automatically when WebGPU is not supported by the browser or device.
|
|
382
|
+
*/
|
|
383
|
+
export declare const DEFAULT_PROVIDERS: readonly string[];
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Single detected object produced by an object-detection model.
|
|
387
|
+
*/
|
|
388
|
+
export declare interface DetectionResult {
|
|
389
|
+
readonly classId: number;
|
|
390
|
+
readonly className: string;
|
|
391
|
+
readonly confidence: number;
|
|
392
|
+
readonly bbox: BoundingBox;
|
|
393
|
+
/** Alias for `classId` (Ultralytics-style). */
|
|
394
|
+
readonly cls: number;
|
|
395
|
+
/** Alias for `className`. */
|
|
396
|
+
readonly name: string;
|
|
397
|
+
/** Alias for `confidence` (Ultralytics-style). */
|
|
398
|
+
readonly conf: number;
|
|
399
|
+
/** Alias for `bbox` (Ultralytics-style). */
|
|
400
|
+
readonly box: BoundingBox;
|
|
401
|
+
/**
|
|
402
|
+
* The original image cropped to `bbox`, HWC RGB uint8. Empty boxes
|
|
403
|
+
* (zero area) yield a zero-sized `RGBImage`.
|
|
404
|
+
*/
|
|
405
|
+
readonly croppedImage: RGBImage;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Per-image detection envelope (Ultralytics-style `Results`).
|
|
410
|
+
*
|
|
411
|
+
* Iterating yields per-instance {@link DetectionResult} entries, so legacy
|
|
412
|
+
* code that did `for (const d of detector.predict(img))` only needs an
|
|
413
|
+
* extra `[0]` to bridge:
|
|
414
|
+
*
|
|
415
|
+
* ```typescript
|
|
416
|
+
* for (const d of (await detector.predict(img))[0]) {
|
|
417
|
+
* console.log(d.cls, d.conf, d.box.xyxy);
|
|
418
|
+
* }
|
|
419
|
+
* ```
|
|
420
|
+
*
|
|
421
|
+
* For numpy-style bulk access, use the `boxes` collection.
|
|
422
|
+
*/
|
|
423
|
+
export declare class DetectionResults implements Iterable<DetectionResult> {
|
|
424
|
+
readonly boxes: Boxes;
|
|
425
|
+
readonly detections: readonly DetectionResult[];
|
|
426
|
+
readonly names: Readonly<Record<number, string>>;
|
|
427
|
+
readonly origImg: RGBImage;
|
|
428
|
+
readonly origShape: readonly [number, number];
|
|
429
|
+
readonly path: string | null;
|
|
430
|
+
readonly speed: Readonly<Record<string, number>>;
|
|
431
|
+
constructor(boxes: Boxes, detections: readonly DetectionResult[], names: Readonly<Record<number, string>>, origImg: RGBImage, origShape: readonly [number, number], path?: string | null, speed?: Readonly<Record<string, number>>);
|
|
432
|
+
/** Number of surviving detections. */
|
|
433
|
+
get length(): number;
|
|
434
|
+
/** Index into the per-instance detections. */
|
|
435
|
+
get(index: number): DetectionResult | undefined;
|
|
436
|
+
[Symbol.iterator](): Iterator<DetectionResult>;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Object detector for anchor-free YOLO ONNX models (v8/v9/v10/v11/v12).
|
|
441
|
+
*
|
|
442
|
+
* `predict()` returns `Promise<DetectionResults[]>` (length 1 for a single
|
|
443
|
+
* image), mirroring Ultralytics' `YOLO("img.jpg")`. Iterate the envelope for
|
|
444
|
+
* per-instance dataclasses, or use the bulk `boxes` view (`.xyxy`, `.xywh`,
|
|
445
|
+
* `.xyxyn`, `.xywhn`, `.cls`, `.conf`).
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```typescript
|
|
449
|
+
* const det = await Detector.create("/models/yolov8n.onnx");
|
|
450
|
+
* const results = await det.predict("/images/street.jpg");
|
|
451
|
+
* const r = results[0];
|
|
452
|
+
* console.log(r.boxes.xyxy, r.boxes.cls, r.boxes.conf, r.names);
|
|
453
|
+
* for (const d of r) {
|
|
454
|
+
* console.log(d.cls, d.conf, d.box.xyxy);
|
|
455
|
+
* }
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
export declare class Detector extends VisionTask {
|
|
459
|
+
private readonly _head;
|
|
460
|
+
private readonly _labels;
|
|
461
|
+
private readonly _names;
|
|
462
|
+
private readonly _inputSize;
|
|
463
|
+
private readonly _confThreshold;
|
|
464
|
+
private readonly _iouThreshold;
|
|
465
|
+
private readonly _maxDetections;
|
|
466
|
+
private constructor();
|
|
467
|
+
/** Load the model and resolve labels. */
|
|
468
|
+
static create(model: ModelSource, options?: DetectorOptions): Promise<Detector>;
|
|
469
|
+
/** The decoder family used to interpret the model's output. */
|
|
470
|
+
get head(): DetectorHead;
|
|
471
|
+
/** Class labels indexed by class id. */
|
|
472
|
+
get labels(): readonly string[];
|
|
473
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
474
|
+
get names(): Readonly<Record<number, string>>;
|
|
475
|
+
/** Number of classes the model predicts. */
|
|
476
|
+
get numClasses(): number;
|
|
477
|
+
/**
|
|
478
|
+
* Alias for {@link predict} — call the detector like a torch `nn.Module`.
|
|
479
|
+
*
|
|
480
|
+
* Use as `det.call(img)` since JavaScript class instances are not callable;
|
|
481
|
+
* for direct invocation, prefer `det.predict(img)`. The full
|
|
482
|
+
* {@link DetectorPredictOptions} (including `classes`) is supported.
|
|
483
|
+
*/
|
|
484
|
+
call(image: ImageInput, options?: DetectorPredictOptions): Promise<DetectionResults[]>;
|
|
485
|
+
/** Run detection on a single image. */
|
|
486
|
+
predict(image: ImageInput, options?: DetectorPredictOptions): Promise<DetectionResults[]>;
|
|
487
|
+
private _preprocess;
|
|
488
|
+
private _buildResult;
|
|
489
|
+
private _buildBoxes;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Decoder family for the model's detection head.
|
|
494
|
+
*
|
|
495
|
+
* - `"yolo"`: anchor-free YOLO head with output shape `[1, 4 + nc, N]` —
|
|
496
|
+
* covers YOLOv8, v9, v10, v11, v12, v26 detect exports.
|
|
497
|
+
*
|
|
498
|
+
* The SDK does **not** auto-detect the head from the model — the caller is
|
|
499
|
+
* responsible for picking a head that matches their export. Future families
|
|
500
|
+
* (v5/v6/v7 with `[1, N, 5+nc]`) will be added as new literal members.
|
|
501
|
+
*/
|
|
502
|
+
export declare type DetectorHead = "yolo";
|
|
503
|
+
|
|
504
|
+
export declare interface DetectorOptions extends OrtSessionOptions {
|
|
505
|
+
/**
|
|
506
|
+
* Decoder family for the detection head. Default `"yolo"` covers
|
|
507
|
+
* YOLOv8/v9/v10/v11/v12/v26.
|
|
508
|
+
*/
|
|
509
|
+
readonly head?: DetectorHead;
|
|
510
|
+
/** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */
|
|
511
|
+
readonly labels?: LabelSpec;
|
|
512
|
+
/** Number of classes — used to validate the supplied labels. */
|
|
513
|
+
readonly numClasses?: number;
|
|
514
|
+
/** Model input `[width, height]` for letterboxing. Defaults to `[640, 640]`. */
|
|
515
|
+
readonly inputSize?: readonly [number, number];
|
|
516
|
+
/** Default minimum class score to keep a candidate. */
|
|
517
|
+
readonly confThreshold?: number;
|
|
518
|
+
/** Default IoU threshold for non-maximum suppression. */
|
|
519
|
+
readonly iouThreshold?: number;
|
|
520
|
+
/** Maximum number of detections per image. */
|
|
521
|
+
readonly maxDetections?: number;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export declare interface DetectorPredictOptions {
|
|
525
|
+
/** Override the default confidence threshold. */
|
|
526
|
+
readonly confThreshold?: number;
|
|
527
|
+
/** Override the default IoU threshold. */
|
|
528
|
+
readonly iouThreshold?: number;
|
|
529
|
+
/**
|
|
530
|
+
* If set, keep only detections whose `classId` is in this list.
|
|
531
|
+
* Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.
|
|
532
|
+
*/
|
|
533
|
+
readonly classes?: readonly number[];
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.
|
|
538
|
+
*
|
|
539
|
+
* Use when you receive image bytes from `cv2.imencode` over the wire and
|
|
540
|
+
* want to feed them to the SDK without going through a canvas decode.
|
|
541
|
+
*
|
|
542
|
+
* @param bgr Flat BGR Uint8Array of length `width * height * 3`.
|
|
543
|
+
*/
|
|
544
|
+
export declare function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage;
|
|
545
|
+
|
|
546
|
+
/** Anything {@link loadImage} accepts as an image input. */
|
|
547
|
+
export declare type ImageInput = string | Blob | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap | ImageData | RGBImage;
|
|
548
|
+
|
|
549
|
+
/** Raised when an input image cannot be decoded into the canonical format. */
|
|
550
|
+
export declare class ImageLoadError extends OrtVisionError {
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Raised when ONNX Runtime fails while executing a model. */
|
|
554
|
+
export declare class InferenceError extends OrtVisionError {
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** Raised when class labels cannot be resolved from the supplied spec. */
|
|
558
|
+
export declare class LabelMapError extends OrtVisionError {
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Class label resolution: presets, lists, dicts, or auto-generated.
|
|
563
|
+
*
|
|
564
|
+
* Tasks call {@link resolveLabels} once at construction time to turn whatever
|
|
565
|
+
* the caller passed (preset name, array, dict, or `null`) into an ordered
|
|
566
|
+
* array of class names indexed by class id.
|
|
567
|
+
*
|
|
568
|
+
* In the browser there is no filesystem, so this module does not load labels
|
|
569
|
+
* from a path — fetch the file yourself and pass an array.
|
|
570
|
+
*/
|
|
571
|
+
/**
|
|
572
|
+
* Anything accepted by {@link resolveLabels}.
|
|
573
|
+
*
|
|
574
|
+
* - `string[]` / `readonly string[]`: explicit names indexed by class id.
|
|
575
|
+
* - `Record<number, string>`: sparse mapping (gaps filled with `class_<id>`).
|
|
576
|
+
* - `string`: a preset name (e.g. `"coco"`).
|
|
577
|
+
* - `null` / `undefined`: auto-generate `class_0` ... `class_{numClasses-1}`.
|
|
578
|
+
*/
|
|
579
|
+
export declare type LabelSpec = readonly string[] | Record<number, string> | string | null | undefined;
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`
|
|
583
|
+
* with a constant fill color.
|
|
584
|
+
*
|
|
585
|
+
* Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`
|
|
586
|
+
* lets callers map detections back to the original image coordinates.
|
|
587
|
+
*/
|
|
588
|
+
export declare function letterbox(image: RGBImage, targetWidth: number, targetHeight: number, fill?: readonly [number, number, number]): LetterboxResult;
|
|
589
|
+
|
|
590
|
+
export declare interface LetterboxResult {
|
|
591
|
+
/** The padded image at the target size. */
|
|
592
|
+
readonly image: RGBImage;
|
|
593
|
+
/** The factor applied to the original image (`< 1` if downscaled). */
|
|
594
|
+
readonly scale: number;
|
|
595
|
+
/** Horizontal padding in pixels (left side; right side has the same or +1). */
|
|
596
|
+
readonly padLeft: number;
|
|
597
|
+
/** Vertical padding in pixels (top side). */
|
|
598
|
+
readonly padTop: number;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Load an image from any supported source into a HWC uint8 RGB array.
|
|
603
|
+
*
|
|
604
|
+
* @throws {@link ImageLoadError} if the source cannot be decoded or has an unsupported shape.
|
|
605
|
+
*/
|
|
606
|
+
export declare function loadImage(source: ImageInput): Promise<RGBImage>;
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Single-channel binary or grayscale mask, laid out row-major.
|
|
610
|
+
*
|
|
611
|
+
* `data.length` must equal `width * height`. For binary masks, values are
|
|
612
|
+
* `0` (background) or `255` (foreground); soft masks may use the full
|
|
613
|
+
* `[0, 255]` range.
|
|
614
|
+
*/
|
|
615
|
+
export declare class Mask {
|
|
616
|
+
readonly data: Uint8Array;
|
|
617
|
+
readonly width: number;
|
|
618
|
+
readonly height: number;
|
|
619
|
+
constructor(data: Uint8Array, width: number, height: number);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Per-instance binary masks for a single image.
|
|
624
|
+
*
|
|
625
|
+
* Each mask is cropped to its instance's bounding box. To paint masks onto
|
|
626
|
+
* a full-image canvas, use `xyxy[i]` as the top-left target.
|
|
627
|
+
*/
|
|
628
|
+
export declare class Masks {
|
|
629
|
+
readonly data: ReadonlyArray<{
|
|
630
|
+
readonly data: Uint8Array;
|
|
631
|
+
readonly width: number;
|
|
632
|
+
readonly height: number;
|
|
633
|
+
}>;
|
|
634
|
+
readonly xyxy: Float32Array;
|
|
635
|
+
readonly origShape: readonly [number, number];
|
|
636
|
+
/**
|
|
637
|
+
* @param data Per-instance binary masks (`Mask` objects from `types.ts`).
|
|
638
|
+
* @param xyxy Flat `[N, 4]` of bounding-box coordinates in original pixels.
|
|
639
|
+
* @param origShape `[height, width]` of the original image.
|
|
640
|
+
*/
|
|
641
|
+
constructor(data: ReadonlyArray<{
|
|
642
|
+
readonly data: Uint8Array;
|
|
643
|
+
readonly width: number;
|
|
644
|
+
readonly height: number;
|
|
645
|
+
}>, xyxy: Float32Array, origShape: readonly [number, number]);
|
|
646
|
+
/** Number of instance masks. */
|
|
647
|
+
get length(): number;
|
|
648
|
+
/** `[N]` shape of the masks collection. */
|
|
649
|
+
get shape(): readonly [number];
|
|
650
|
+
[Symbol.iterator](): Iterator<{
|
|
651
|
+
readonly data: Uint8Array;
|
|
652
|
+
readonly width: number;
|
|
653
|
+
readonly height: number;
|
|
654
|
+
}>;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** Raised when an ONNX model cannot be loaded into an inference session. */
|
|
658
|
+
export declare class ModelLoadError extends OrtVisionError {
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** Anything `InferenceSession.create` accepts. */
|
|
662
|
+
export declare type ModelSource = string | ArrayBufferLike | Uint8Array;
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Greedy non-maximum suppression on axis-aligned bounding boxes.
|
|
666
|
+
*
|
|
667
|
+
* Mirrors `torchvision.ops.nms` (keeps boxes with the highest score, drops
|
|
668
|
+
* any subsequent box whose IoU exceeds the threshold).
|
|
669
|
+
*
|
|
670
|
+
* @param boxes Flat array of length `4 * N` in xyxy order: `[x1,y1,x2,y2, ...]`.
|
|
671
|
+
* @param scores Detection score per box, length `N`.
|
|
672
|
+
* @param iouThreshold Boxes with IoU above this threshold relative to a kept box are suppressed.
|
|
673
|
+
* @returns Indices of kept boxes, in descending score order.
|
|
674
|
+
*/
|
|
675
|
+
export declare function nms(boxes: Float32Array, scores: Float32Array, iouThreshold: number): Int32Array;
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Convert a uint8 image to a normalized float32 array (HWC layout preserved).
|
|
679
|
+
*
|
|
680
|
+
* Applies `(pixel * scale - mean) / std` channel-wise.
|
|
681
|
+
*/
|
|
682
|
+
export declare function normalize(image: RGBImage, mean: readonly [number, number, number], std: readonly [number, number, number], scale?: number): Float32Array;
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Wrap an ONNX Runtime Web `InferenceSession` with convenient metadata access.
|
|
686
|
+
*
|
|
687
|
+
* The wrapper exposes input/output names, manages execution-provider
|
|
688
|
+
* selection, and provides a typed {@link OrtSession.run} method.
|
|
689
|
+
*/
|
|
690
|
+
export declare class OrtSession {
|
|
691
|
+
private readonly _session;
|
|
692
|
+
readonly providers: readonly string[];
|
|
693
|
+
private constructor();
|
|
694
|
+
/**
|
|
695
|
+
* Load an ONNX model into an ORT inference session.
|
|
696
|
+
*
|
|
697
|
+
* @param model Either a URL string fetched by ORT, or a `Uint8Array`/`ArrayBuffer` containing the model bytes.
|
|
698
|
+
* @param options Provider list and pass-through `SessionOptions`.
|
|
699
|
+
* @throws {@link ModelLoadError} if the model cannot be loaded.
|
|
700
|
+
*/
|
|
701
|
+
static create(model: ModelSource, options?: OrtSessionOptions): Promise<OrtSession>;
|
|
702
|
+
/** Names of the model's inputs, in declaration order. */
|
|
703
|
+
get inputNames(): readonly string[];
|
|
704
|
+
/** Name of the first (and usually only) input. */
|
|
705
|
+
get inputName(): string;
|
|
706
|
+
/** Names of the model's outputs, in declaration order. */
|
|
707
|
+
get outputNames(): readonly string[];
|
|
708
|
+
/** The underlying `onnxruntime-web` session, for advanced use cases. */
|
|
709
|
+
get raw(): ort.InferenceSession;
|
|
710
|
+
/**
|
|
711
|
+
* Run inference and return all outputs.
|
|
712
|
+
*
|
|
713
|
+
* @param feeds Map of input name to `ort.Tensor`. Keys must match {@link inputNames}.
|
|
714
|
+
* @throws {@link InferenceError} if ORT raises any error during execution.
|
|
715
|
+
*/
|
|
716
|
+
run(feeds: Record<string, ort.Tensor>): Promise<Record<string, ort.Tensor>>;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
export declare interface OrtSessionOptions {
|
|
720
|
+
/** Execution providers in preference order. `undefined` uses {@link DEFAULT_PROVIDERS}. */
|
|
721
|
+
readonly providers?: readonly string[];
|
|
722
|
+
/** Optional ORT session options forwarded to `InferenceSession.create`. */
|
|
723
|
+
readonly sessionOptions?: ort.InferenceSession.SessionOptions;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Exceptions raised by the SDK.
|
|
728
|
+
*
|
|
729
|
+
* All exceptions inherit from {@link OrtVisionError}, so callers can catch
|
|
730
|
+
* the base class to handle any SDK-originated failure uniformly.
|
|
731
|
+
*/
|
|
732
|
+
export declare class OrtVisionError extends Error {
|
|
733
|
+
constructor(message: string, options?: ErrorOptions);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Top-k classification probabilities for a single image.
|
|
738
|
+
*
|
|
739
|
+
* Mirrors Ultralytics' `Probs` interface.
|
|
740
|
+
*/
|
|
741
|
+
export declare class Probs {
|
|
742
|
+
readonly data: Float32Array;
|
|
743
|
+
/** @param data `[numClasses]` per-class probabilities, indexed by class id. */
|
|
744
|
+
constructor(data: Float32Array);
|
|
745
|
+
/** Number of classes. */
|
|
746
|
+
get length(): number;
|
|
747
|
+
/** `[numClasses]` shape of the underlying vector. */
|
|
748
|
+
get shape(): readonly [number];
|
|
749
|
+
/** Index of the most probable class. */
|
|
750
|
+
get top1(): number;
|
|
751
|
+
/** Probability of the top-1 class. */
|
|
752
|
+
get top1conf(): number;
|
|
753
|
+
/** Indices of the top-5 most probable classes, descending. */
|
|
754
|
+
get top5(): Int32Array;
|
|
755
|
+
/** Probabilities of the top-5 classes, descending. */
|
|
756
|
+
get top5conf(): Float32Array;
|
|
757
|
+
private _topK;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** Raised when a requested execution provider is not available. */
|
|
761
|
+
export declare class ProviderNotAvailableError extends OrtVisionError {
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */
|
|
765
|
+
export declare function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage;
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Resolve a labels specification into an ordered array of class names.
|
|
769
|
+
*
|
|
770
|
+
* @throws {@link LabelMapError} if the spec is invalid, the preset is unknown,
|
|
771
|
+
* or the resolved length disagrees with `numClasses`.
|
|
772
|
+
*/
|
|
773
|
+
export declare function resolveLabels(spec: LabelSpec, options?: ResolveLabelsOptions): readonly string[];
|
|
774
|
+
|
|
775
|
+
export declare interface ResolveLabelsOptions {
|
|
776
|
+
/**
|
|
777
|
+
* Expected number of classes.
|
|
778
|
+
*
|
|
779
|
+
* - When `spec` is `null`/`undefined`, this is required to auto-generate names.
|
|
780
|
+
* - When `spec` is provided, it validates that the resolved length matches.
|
|
781
|
+
*/
|
|
782
|
+
readonly numClasses?: number;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Resolve the execution providers to pass to `InferenceSession.create`.
|
|
787
|
+
*
|
|
788
|
+
* @param requested Explicit provider list in preference order; `undefined` returns the default.
|
|
789
|
+
*/
|
|
790
|
+
export declare function resolveProviders(requested?: readonly string[]): string[];
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Public output types returned by the SDK's vision tasks.
|
|
794
|
+
*
|
|
795
|
+
* These types form the contract between the SDK and its callers. They mirror
|
|
796
|
+
* the Python `ort-vision-sdk` output dataclasses 1-to-1.
|
|
797
|
+
*
|
|
798
|
+
* Naming is intentionally compatible with the Ultralytics / torchvision idiom
|
|
799
|
+
* (`cls`, `conf`, `box`, `xyxy`, `xywh`, normalized variants) so code ported
|
|
800
|
+
* from those projects keeps working with minimal edits. The original verbose
|
|
801
|
+
* names (`classId`, `className`, `confidence`, `bbox`) are still populated for
|
|
802
|
+
* backwards compatibility.
|
|
803
|
+
*/
|
|
804
|
+
/**
|
|
805
|
+
* HWC RGB uint8 image — the canonical image format used across the SDK.
|
|
806
|
+
*
|
|
807
|
+
* `data.length` must equal `width * height * 3`. The buffer is laid out row
|
|
808
|
+
* by row, top-to-bottom, with each pixel as `[R, G, B]`.
|
|
809
|
+
*/
|
|
810
|
+
export declare class RGBImage {
|
|
811
|
+
readonly data: Uint8Array;
|
|
812
|
+
readonly width: number;
|
|
813
|
+
readonly height: number;
|
|
814
|
+
constructor(data: Uint8Array, width: number, height: number);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Single segmented instance produced by an instance-segmentation model.
|
|
819
|
+
*
|
|
820
|
+
* Mirrors {@link DetectionResult} and adds the per-instance binary mask
|
|
821
|
+
* plus a "ready-to-display" background-removed crop.
|
|
822
|
+
*/
|
|
823
|
+
export declare interface SegmentationResult {
|
|
824
|
+
readonly classId: number;
|
|
825
|
+
readonly className: string;
|
|
826
|
+
readonly confidence: number;
|
|
827
|
+
readonly bbox: BoundingBox;
|
|
828
|
+
/** Alias for `classId` (Ultralytics-style). */
|
|
829
|
+
readonly cls: number;
|
|
830
|
+
/** Alias for `className`. */
|
|
831
|
+
readonly name: string;
|
|
832
|
+
/** Alias for `confidence` (Ultralytics-style). */
|
|
833
|
+
readonly conf: number;
|
|
834
|
+
/** Alias for `bbox` (Ultralytics-style). */
|
|
835
|
+
readonly box: BoundingBox;
|
|
836
|
+
/**
|
|
837
|
+
* Binary mask cropped to `bbox`. Values are `0` (background) or `255`
|
|
838
|
+
* (foreground). Empty boxes yield a zero-sized `Mask`.
|
|
839
|
+
*/
|
|
840
|
+
readonly mask: Mask;
|
|
841
|
+
/**
|
|
842
|
+
* The original image cropped to `bbox` with background pixels (where
|
|
843
|
+
* `mask.data[i] === 0`) zeroed out. Empty boxes yield a zero-sized
|
|
844
|
+
* `RGBImage`.
|
|
845
|
+
*/
|
|
846
|
+
readonly segmentedImage: RGBImage;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Per-image instance-segmentation envelope (Ultralytics-style `Results`).
|
|
851
|
+
*
|
|
852
|
+
* Iterating yields per-instance {@link SegmentationResult} entries. `boxes`
|
|
853
|
+
* and `masks` mirror Ultralytics' bulk-array views.
|
|
854
|
+
*/
|
|
855
|
+
export declare class SegmentationResults implements Iterable<SegmentationResult> {
|
|
856
|
+
readonly boxes: Boxes;
|
|
857
|
+
readonly masks: Masks;
|
|
858
|
+
readonly detections: readonly SegmentationResult[];
|
|
859
|
+
readonly names: Readonly<Record<number, string>>;
|
|
860
|
+
readonly origImg: RGBImage;
|
|
861
|
+
readonly origShape: readonly [number, number];
|
|
862
|
+
readonly path: string | null;
|
|
863
|
+
readonly speed: Readonly<Record<string, number>>;
|
|
864
|
+
constructor(boxes: Boxes, masks: Masks, detections: readonly SegmentationResult[], names: Readonly<Record<number, string>>, origImg: RGBImage, origShape: readonly [number, number], path?: string | null, speed?: Readonly<Record<string, number>>);
|
|
865
|
+
/** Number of surviving instances. */
|
|
866
|
+
get length(): number;
|
|
867
|
+
/** Index into the per-instance results. */
|
|
868
|
+
get(index: number): SegmentationResult | undefined;
|
|
869
|
+
[Symbol.iterator](): Iterator<SegmentationResult>;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* Instance segmenter for YOLO seg ONNX models (v8-seg / v11-seg / ...).
|
|
874
|
+
*
|
|
875
|
+
* The model is expected to expose two outputs:
|
|
876
|
+
*
|
|
877
|
+
* 1. `output0`: `(1, 4 + numClasses + numMaskCoefs, numAnchors)` — per-anchor
|
|
878
|
+
* predictions (boxes, class scores, mask coefficients).
|
|
879
|
+
* 2. `output1`: `(1, numMaskCoefs, maskH, maskW)` — prototype masks.
|
|
880
|
+
*
|
|
881
|
+
* `predict()` returns `Promise<SegmentationResults[]>` (length 1 for a
|
|
882
|
+
* single image), mirroring Ultralytics' API. The envelope exposes:
|
|
883
|
+
*
|
|
884
|
+
* - `boxes`: bulk numpy view (`xyxy`, `xywh`, `xyxyn`, `xywhn`, `cls`, `conf`).
|
|
885
|
+
* - `masks`: per-instance binary masks cropped to each box.
|
|
886
|
+
* - per-instance {@link SegmentationResult} via iteration.
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* const seg = await Segmenter.create("/models/yolov8n-seg.onnx");
|
|
891
|
+
* const r = (await seg.predict("/images/street.jpg"))[0];
|
|
892
|
+
* for (const inst of r) {
|
|
893
|
+
* console.log(inst.cls, inst.conf, inst.box.xyxy);
|
|
894
|
+
* }
|
|
895
|
+
* ```
|
|
896
|
+
*/
|
|
897
|
+
export declare class Segmenter extends VisionTask {
|
|
898
|
+
private readonly _head;
|
|
899
|
+
private readonly _labels;
|
|
900
|
+
private readonly _names;
|
|
901
|
+
private readonly _inputSize;
|
|
902
|
+
private readonly _confThreshold;
|
|
903
|
+
private readonly _iouThreshold;
|
|
904
|
+
private readonly _maxDetections;
|
|
905
|
+
private readonly _maskThreshold;
|
|
906
|
+
private constructor();
|
|
907
|
+
/** Load the model and resolve labels. */
|
|
908
|
+
static create(model: ModelSource, options?: SegmenterOptions): Promise<Segmenter>;
|
|
909
|
+
/** The decoder family used to interpret the model's output. */
|
|
910
|
+
get head(): SegmenterHead;
|
|
911
|
+
/** Class labels indexed by class id. */
|
|
912
|
+
get labels(): readonly string[];
|
|
913
|
+
/** Class id → class name dict (matches Ultralytics' `model.names`). */
|
|
914
|
+
get names(): Readonly<Record<number, string>>;
|
|
915
|
+
/** Number of classes the model predicts. */
|
|
916
|
+
get numClasses(): number;
|
|
917
|
+
/** Alias for {@link predict} (parity with PyTorch `nn.Module.__call__`). */
|
|
918
|
+
call(image: ImageInput, options?: SegmenterPredictOptions): Promise<SegmentationResults[]>;
|
|
919
|
+
/** Run instance segmentation on a single image. */
|
|
920
|
+
predict(image: ImageInput, options?: SegmenterPredictOptions): Promise<SegmentationResults[]>;
|
|
921
|
+
private _preprocess;
|
|
922
|
+
private _splitOutputs;
|
|
923
|
+
private _buildResult;
|
|
924
|
+
private _buildBoxes;
|
|
925
|
+
private _buildMasks;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Decoder family for the segmentation head.
|
|
930
|
+
*
|
|
931
|
+
* - `"yolo-seg"`: YOLO instance-segmentation head with two outputs —
|
|
932
|
+
* `[1, 4 + nc + nm, N]` per-anchor predictions plus `[1, nm, mh, mw]`
|
|
933
|
+
* prototype masks. Covers YOLOv8-seg, v11-seg, v26-seg.
|
|
934
|
+
*
|
|
935
|
+
* The SDK does **not** auto-detect this — the caller is responsible for
|
|
936
|
+
* picking a head that matches their export.
|
|
937
|
+
*/
|
|
938
|
+
export declare type SegmenterHead = "yolo-seg";
|
|
939
|
+
|
|
940
|
+
export declare interface SegmenterOptions extends OrtSessionOptions {
|
|
941
|
+
/**
|
|
942
|
+
* Decoder family for the segmentation head. Default `"yolo-seg"` covers
|
|
943
|
+
* YOLOv8-seg/v11-seg/v26-seg.
|
|
944
|
+
*/
|
|
945
|
+
readonly head?: SegmenterHead;
|
|
946
|
+
/** Class label spec — see {@link resolveLabels}. Defaults to the COCO 80-class preset. */
|
|
947
|
+
readonly labels?: LabelSpec;
|
|
948
|
+
/** Number of classes — used to validate the supplied labels. */
|
|
949
|
+
readonly numClasses?: number;
|
|
950
|
+
/** Model input `[width, height]` for letterboxing. Defaults to `[640, 640]`. */
|
|
951
|
+
readonly inputSize?: readonly [number, number];
|
|
952
|
+
/** Default minimum class score to keep a candidate. */
|
|
953
|
+
readonly confThreshold?: number;
|
|
954
|
+
/** Default IoU threshold for non-maximum suppression. */
|
|
955
|
+
readonly iouThreshold?: number;
|
|
956
|
+
/** Maximum number of instances per image. */
|
|
957
|
+
readonly maxDetections?: number;
|
|
958
|
+
/** Probability cutoff applied to soft masks. Defaults to `0.5`. */
|
|
959
|
+
readonly maskThreshold?: number;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
export declare interface SegmenterPredictOptions {
|
|
963
|
+
readonly confThreshold?: number;
|
|
964
|
+
readonly iouThreshold?: number;
|
|
965
|
+
/**
|
|
966
|
+
* If set, keep only instances whose `classId` is in this list.
|
|
967
|
+
* Mirrors Ultralytics' `model.predict(img, classes=[0, 16])`.
|
|
968
|
+
*/
|
|
969
|
+
readonly classes?: readonly number[];
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* Classification head postprocessing — softmax + top-k.
|
|
974
|
+
*/
|
|
975
|
+
/** Apply numerically-stable softmax to a 1-D vector of logits. */
|
|
976
|
+
export declare function softmax(logits: Float32Array | readonly number[]): Float32Array;
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Transpose interleaved HWC data to planar CHW layout.
|
|
980
|
+
*
|
|
981
|
+
* @param hwc Source array of length `width * height * channels`.
|
|
982
|
+
*/
|
|
983
|
+
export declare function toCHW(hwc: Float32Array, width: number, height: number, channels?: number): Float32Array;
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).
|
|
987
|
+
*
|
|
988
|
+
* Useful for round-tripping data to a Python OpenCV consumer.
|
|
989
|
+
*/
|
|
990
|
+
export declare function toCv2(image: RGBImage): Uint8Array;
|
|
991
|
+
|
|
992
|
+
/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */
|
|
993
|
+
export declare function toFloat32(image: RGBImage, scale?: number): Float32Array;
|
|
994
|
+
|
|
995
|
+
/** Wrap a Float32 buffer into an `ort.Tensor`. */
|
|
996
|
+
export declare function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor;
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Return the top-k entries of a 1-D probability vector, sorted descending.
|
|
1000
|
+
*
|
|
1001
|
+
* @param k Number of entries to return; `null` returns all entries.
|
|
1002
|
+
*/
|
|
1003
|
+
export declare function topK(probabilities: Float32Array, k: number | null): TopKResult;
|
|
1004
|
+
|
|
1005
|
+
export declare interface TopKResult {
|
|
1006
|
+
readonly indices: Int32Array;
|
|
1007
|
+
readonly values: Float32Array;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.
|
|
1012
|
+
*
|
|
1013
|
+
* Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,
|
|
1014
|
+
* `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that
|
|
1015
|
+
* don't require ImageNet normalization.
|
|
1016
|
+
*
|
|
1017
|
+
* @returns CHW `Float32Array` of length `width * height * 3`.
|
|
1018
|
+
*/
|
|
1019
|
+
export declare function toTensor(image: RGBImage): Float32Array;
|
|
1020
|
+
|
|
1021
|
+
export declare const VERSION: string;
|
|
1022
|
+
|
|
1023
|
+
export declare abstract class VisionTask {
|
|
1024
|
+
protected readonly _session: OrtSession;
|
|
1025
|
+
protected constructor(_session: OrtSession);
|
|
1026
|
+
/** The underlying {@link OrtSession} used to run inference. */
|
|
1027
|
+
get session(): OrtSession;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
export { }
|