@mauriciobenjamin700/ort-vision-sdk-web 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/LICENSE +21 -0
  3. package/README.md +90 -0
  4. package/dist/core/canvas.d.ts +21 -0
  5. package/dist/core/canvas.d.ts.map +1 -0
  6. package/dist/core/canvas.js +58 -0
  7. package/dist/core/canvas.js.map +1 -0
  8. package/dist/core/exceptions.d.ts +25 -0
  9. package/dist/core/exceptions.d.ts.map +1 -0
  10. package/dist/core/exceptions.js +28 -0
  11. package/dist/core/exceptions.js.map +1 -0
  12. package/dist/core/providers.d.ts +21 -0
  13. package/dist/core/providers.d.ts.map +1 -0
  14. package/dist/core/providers.js +29 -0
  15. package/dist/core/providers.js.map +1 -0
  16. package/dist/core/session.d.ts +47 -0
  17. package/dist/core/session.d.ts.map +1 -0
  18. package/dist/core/session.js +86 -0
  19. package/dist/core/session.js.map +1 -0
  20. package/dist/index.d.ts +21 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +21 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/io/image.d.ts +17 -0
  25. package/dist/io/image.d.ts.map +1 -0
  26. package/dist/io/image.js +104 -0
  27. package/dist/io/image.js.map +1 -0
  28. package/dist/labels.d.ts +38 -0
  29. package/dist/labels.d.ts.map +1 -0
  30. package/dist/labels.js +71 -0
  31. package/dist/labels.js.map +1 -0
  32. package/dist/postprocess/classification.d.ts +16 -0
  33. package/dist/postprocess/classification.d.ts.map +1 -0
  34. package/dist/postprocess/classification.js +46 -0
  35. package/dist/postprocess/classification.js.map +1 -0
  36. package/dist/postprocess/detection.d.ts +112 -0
  37. package/dist/postprocess/detection.d.ts.map +1 -0
  38. package/dist/postprocess/detection.js +283 -0
  39. package/dist/postprocess/detection.js.map +1 -0
  40. package/dist/postprocess/segmentation.d.ts +61 -0
  41. package/dist/postprocess/segmentation.d.ts.map +1 -0
  42. package/dist/postprocess/segmentation.js +184 -0
  43. package/dist/postprocess/segmentation.js.map +1 -0
  44. package/dist/preprocess/image.d.ts +71 -0
  45. package/dist/preprocess/image.d.ts.map +1 -0
  46. package/dist/preprocess/image.js +171 -0
  47. package/dist/preprocess/image.js.map +1 -0
  48. package/dist/results.d.ts +182 -0
  49. package/dist/results.d.ts.map +1 -0
  50. package/dist/results.js +321 -0
  51. package/dist/results.js.map +1 -0
  52. package/dist/tasks/base.d.ts +14 -0
  53. package/dist/tasks/base.d.ts.map +1 -0
  54. package/dist/tasks/base.js +17 -0
  55. package/dist/tasks/base.js.map +1 -0
  56. package/dist/tasks/classifier.d.ts +82 -0
  57. package/dist/tasks/classifier.d.ts.map +1 -0
  58. package/dist/tasks/classifier.js +135 -0
  59. package/dist/tasks/classifier.js.map +1 -0
  60. package/dist/tasks/detector.d.ts +102 -0
  61. package/dist/tasks/detector.d.ts.map +1 -0
  62. package/dist/tasks/detector.js +189 -0
  63. package/dist/tasks/detector.js.map +1 -0
  64. package/dist/tasks/segmenter.d.ts +105 -0
  65. package/dist/tasks/segmenter.d.ts.map +1 -0
  66. package/dist/tasks/segmenter.js +242 -0
  67. package/dist/tasks/segmenter.js.map +1 -0
  68. package/dist/types.d.ts +182 -0
  69. package/dist/types.d.ts.map +1 -0
  70. package/dist/types.js +147 -0
  71. package/dist/types.js.map +1 -0
  72. package/package.json +71 -0
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Composable image preprocessing primitives (resize, normalize, letterbox, layout).
3
+ *
4
+ * Mirrors the Python `preprocess.image` module. Operates on the canonical
5
+ * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers
6
+ * depending on the operation.
7
+ */
8
+ import * as ortRuntime from "onnxruntime-web";
9
+ import { createCanvas, get2DContext, imageDataToRGB, rgbToImageData, } from "../core/canvas.js";
10
+ import { RGBImage } from "../types.js";
11
+ /** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */
12
+ export function resize(image, targetWidth, targetHeight) {
13
+ if (targetWidth <= 0 || targetHeight <= 0) {
14
+ throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);
15
+ }
16
+ if (targetWidth === image.width && targetHeight === image.height) {
17
+ return image;
18
+ }
19
+ const srcCanvas = createCanvas(image.width, image.height);
20
+ const srcCtx = get2DContext(srcCanvas);
21
+ srcCtx.putImageData(rgbToImageData(image), 0, 0);
22
+ const dstCanvas = createCanvas(targetWidth, targetHeight);
23
+ const dstCtx = get2DContext(dstCanvas);
24
+ dstCtx.imageSmoothingEnabled = true;
25
+ dstCtx.imageSmoothingQuality = "high";
26
+ dstCtx.drawImage(srcCanvas, 0, 0, targetWidth, targetHeight);
27
+ const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);
28
+ return imageDataToRGB(imageData);
29
+ }
30
+ /**
31
+ * Convert a uint8 image to a normalized float32 array (HWC layout preserved).
32
+ *
33
+ * Applies `(pixel * scale - mean) / std` channel-wise.
34
+ */
35
+ export function normalize(image, mean, std, scale = 1 / 255) {
36
+ const out = new Float32Array(image.data.length);
37
+ const data = image.data;
38
+ const m0 = mean[0];
39
+ const m1 = mean[1];
40
+ const m2 = mean[2];
41
+ const s0 = std[0];
42
+ const s1 = std[1];
43
+ const s2 = std[2];
44
+ for (let i = 0; i < data.length; i += 3) {
45
+ out[i] = (data[i] * scale - m0) / s0;
46
+ out[i + 1] = (data[i + 1] * scale - m1) / s1;
47
+ out[i + 2] = (data[i + 2] * scale - m2) / s2;
48
+ }
49
+ return out;
50
+ }
51
+ /** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */
52
+ export function toFloat32(image, scale = 1 / 255) {
53
+ const out = new Float32Array(image.data.length);
54
+ const data = image.data;
55
+ for (let i = 0; i < data.length; i++) {
56
+ out[i] = data[i] * scale;
57
+ }
58
+ return out;
59
+ }
60
+ /**
61
+ * Transpose interleaved HWC data to planar CHW layout.
62
+ *
63
+ * @param hwc Source array of length `width * height * channels`.
64
+ */
65
+ export function toCHW(hwc, width, height, channels = 3) {
66
+ const expected = width * height * channels;
67
+ if (hwc.length !== expected) {
68
+ throw new Error(`toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`);
69
+ }
70
+ const chw = new Float32Array(expected);
71
+ const plane = width * height;
72
+ for (let y = 0; y < height; y++) {
73
+ for (let x = 0; x < width; x++) {
74
+ const hwcBase = (y * width + x) * channels;
75
+ const planeIdx = y * width + x;
76
+ for (let c = 0; c < channels; c++) {
77
+ chw[c * plane + planeIdx] = hwc[hwcBase + c];
78
+ }
79
+ }
80
+ }
81
+ return chw;
82
+ }
83
+ /** Wrap a Float32 buffer into an `ort.Tensor`. */
84
+ export function toFloat32Tensor(data, dims) {
85
+ return new ortRuntime.Tensor("float32", data, dims);
86
+ }
87
+ /**
88
+ * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.
89
+ *
90
+ * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,
91
+ * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that
92
+ * don't require ImageNet normalization.
93
+ *
94
+ * @returns CHW `Float32Array` of length `width * height * 3`.
95
+ */
96
+ export function toTensor(image) {
97
+ const f32 = toFloat32(image);
98
+ return toCHW(f32, image.width, image.height, 3);
99
+ }
100
+ /**
101
+ * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.
102
+ *
103
+ * Use when you receive image bytes from `cv2.imencode` over the wire and
104
+ * want to feed them to the SDK without going through a canvas decode.
105
+ *
106
+ * @param bgr Flat BGR Uint8Array of length `width * height * 3`.
107
+ */
108
+ export function fromCv2(bgr, width, height) {
109
+ if (bgr.length !== width * height * 3) {
110
+ throw new Error(`fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`);
111
+ }
112
+ const rgb = new Uint8Array(bgr.length);
113
+ for (let i = 0; i < bgr.length; i += 3) {
114
+ rgb[i] = bgr[i + 2];
115
+ rgb[i + 1] = bgr[i + 1];
116
+ rgb[i + 2] = bgr[i];
117
+ }
118
+ return new RGBImage(rgb, width, height);
119
+ }
120
+ /**
121
+ * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).
122
+ *
123
+ * Useful for round-tripping data to a Python OpenCV consumer.
124
+ */
125
+ export function toCv2(image) {
126
+ const rgb = image.data;
127
+ const bgr = new Uint8Array(rgb.length);
128
+ for (let i = 0; i < rgb.length; i += 3) {
129
+ bgr[i] = rgb[i + 2];
130
+ bgr[i + 1] = rgb[i + 1];
131
+ bgr[i + 2] = rgb[i];
132
+ }
133
+ return bgr;
134
+ }
135
+ /**
136
+ * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`
137
+ * with a constant fill color.
138
+ *
139
+ * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`
140
+ * lets callers map detections back to the original image coordinates.
141
+ */
142
+ export function letterbox(image, targetWidth, targetHeight, fill = [114, 114, 114]) {
143
+ const scale = Math.min(targetWidth / image.width, targetHeight / image.height);
144
+ const newW = Math.round(image.width * scale);
145
+ const newH = Math.round(image.height * scale);
146
+ const resized = resize(image, newW, newH);
147
+ const out = new Uint8Array(targetWidth * targetHeight * 3);
148
+ const f0 = fill[0];
149
+ const f1 = fill[1];
150
+ const f2 = fill[2];
151
+ for (let i = 0; i < out.length; i += 3) {
152
+ out[i] = f0;
153
+ out[i + 1] = f1;
154
+ out[i + 2] = f2;
155
+ }
156
+ const padLeft = Math.floor((targetWidth - newW) / 2);
157
+ const padTop = Math.floor((targetHeight - newH) / 2);
158
+ const rowBytes = newW * 3;
159
+ for (let y = 0; y < newH; y++) {
160
+ const srcOffset = y * rowBytes;
161
+ const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;
162
+ out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);
163
+ }
164
+ return {
165
+ image: new RGBImage(out, targetWidth, targetHeight),
166
+ scale,
167
+ padLeft,
168
+ padTop,
169
+ };
170
+ }
171
+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Q,CAAC,EAAE,SAAS,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO;QACL,KAAK,EAAE,IAAI,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,YAAY,CAAC;QACnD,KAAK;QACL,OAAO;QACP,MAAM;KACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Per-image result envelopes — Ultralytics-style `Results` for the SDK.
3
+ *
4
+ * Each `predict()` call returns a 1-element array of these envelopes,
5
+ * mirroring `YOLO("img.jpg")`. Each envelope holds:
6
+ *
7
+ * - A bulk-array view of the predictions (`Boxes`, `Probs`, `Masks`) with
8
+ * the exact attribute names Ultralytics uses (`xyxy`, `xywh`, `xyxyn`,
9
+ * `xywhn`, `cls`, `conf`, `data`, `top1`, `top5`).
10
+ * - Per-instance dataclasses (`DetectionResult`, `SegmentationResult`,
11
+ * `ClassProbability`) for callers who prefer the OO interface.
12
+ * - `names`: `Record<number, string>` matching Ultralytics' `model.names`.
13
+ * - `origImg` / `origShape` / `path`: provenance for the original input.
14
+ */
15
+ import { type ClassificationResult, type DetectionResult, type RGBImage, type SegmentationResult } from "./types.js";
16
+ /**
17
+ * Bulk numpy-style view of detected boxes for a single image.
18
+ *
19
+ * Mirrors Ultralytics' `Boxes` interface. Coordinates in {@link xyxy} and
20
+ * {@link xywh} are absolute pixels in the original image; the `*n` variants
21
+ * are normalized to `[0, 1]` using `origShape`.
22
+ */
23
+ export declare class Boxes {
24
+ readonly xyxy: Float32Array;
25
+ readonly cls: Int32Array;
26
+ readonly conf: Float32Array;
27
+ readonly origShape: readonly [number, number];
28
+ /**
29
+ * @param xyxy Flat array of length `4 * N` in `[x1, y1, x2, y2, ...]` order.
30
+ * @param cls One class index per box, length `N`.
31
+ * @param conf One confidence per box, length `N`.
32
+ * @param origShape `[height, width]` of the original image.
33
+ */
34
+ constructor(xyxy: Float32Array, cls: Int32Array, conf: Float32Array, origShape: readonly [number, number]);
35
+ /** Number of detected boxes. */
36
+ get length(): number;
37
+ /** `[N, 4]` shape of the `xyxy` view. */
38
+ get shape(): readonly [number, number];
39
+ /** Boxes as `[N, 4]` `[cx, cy, w, h]` flat array in absolute pixels. */
40
+ get xywh(): Float32Array;
41
+ /** Boxes as `[N, 4]` `[x1, y1, x2, y2]` normalized to `[0, 1]`. */
42
+ get xyxyn(): Float32Array;
43
+ /** Boxes as `[N, 4]` `[cx, cy, w, h]` normalized to `[0, 1]`. */
44
+ get xywhn(): Float32Array;
45
+ /**
46
+ * Concatenated `[N, 6]` array of `[x1, y1, x2, y2, conf, cls]`.
47
+ *
48
+ * Matches Ultralytics' `boxes.data`.
49
+ */
50
+ get data(): Float32Array;
51
+ }
52
+ /**
53
+ * Top-k classification probabilities for a single image.
54
+ *
55
+ * Mirrors Ultralytics' `Probs` interface.
56
+ */
57
+ export declare class Probs {
58
+ readonly data: Float32Array;
59
+ /** @param data `[numClasses]` per-class probabilities, indexed by class id. */
60
+ constructor(data: Float32Array);
61
+ /** Number of classes. */
62
+ get length(): number;
63
+ /** `[numClasses]` shape of the underlying vector. */
64
+ get shape(): readonly [number];
65
+ /** Index of the most probable class. */
66
+ get top1(): number;
67
+ /** Probability of the top-1 class. */
68
+ get top1conf(): number;
69
+ /** Indices of the top-5 most probable classes, descending. */
70
+ get top5(): Int32Array;
71
+ /** Probabilities of the top-5 classes, descending. */
72
+ get top5conf(): Float32Array;
73
+ private _topK;
74
+ }
75
+ /**
76
+ * Per-instance binary masks for a single image.
77
+ *
78
+ * Each mask is cropped to its instance's bounding box. To paint masks onto
79
+ * a full-image canvas, use `xyxy[i]` as the top-left target.
80
+ */
81
+ export declare class Masks {
82
+ readonly data: ReadonlyArray<{
83
+ readonly data: Uint8Array;
84
+ readonly width: number;
85
+ readonly height: number;
86
+ }>;
87
+ readonly xyxy: Float32Array;
88
+ readonly origShape: readonly [number, number];
89
+ /**
90
+ * @param data Per-instance binary masks (`Mask` objects from `types.ts`).
91
+ * @param xyxy Flat `[N, 4]` of bounding-box coordinates in original pixels.
92
+ * @param origShape `[height, width]` of the original image.
93
+ */
94
+ constructor(data: ReadonlyArray<{
95
+ readonly data: Uint8Array;
96
+ readonly width: number;
97
+ readonly height: number;
98
+ }>, xyxy: Float32Array, origShape: readonly [number, number]);
99
+ /** Number of instance masks. */
100
+ get length(): number;
101
+ /** `[N]` shape of the masks collection. */
102
+ get shape(): readonly [number];
103
+ [Symbol.iterator](): Iterator<{
104
+ readonly data: Uint8Array;
105
+ readonly width: number;
106
+ readonly height: number;
107
+ }>;
108
+ }
109
+ /**
110
+ * Per-image detection envelope (Ultralytics-style `Results`).
111
+ *
112
+ * Iterating yields per-instance {@link DetectionResult} entries, so legacy
113
+ * code that did `for (const d of detector.predict(img))` only needs an
114
+ * extra `[0]` to bridge:
115
+ *
116
+ * ```typescript
117
+ * for (const d of (await detector.predict(img))[0]) {
118
+ * console.log(d.cls, d.conf, d.box.xyxy);
119
+ * }
120
+ * ```
121
+ *
122
+ * For numpy-style bulk access, use the `boxes` collection.
123
+ */
124
+ export declare class DetectionResults implements Iterable<DetectionResult> {
125
+ readonly boxes: Boxes;
126
+ readonly detections: readonly DetectionResult[];
127
+ readonly names: Readonly<Record<number, string>>;
128
+ readonly origImg: RGBImage;
129
+ readonly origShape: readonly [number, number];
130
+ readonly path: string | null;
131
+ readonly speed: Readonly<Record<string, number>>;
132
+ 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>>);
133
+ /** Number of surviving detections. */
134
+ get length(): number;
135
+ /** Index into the per-instance detections. */
136
+ get(index: number): DetectionResult | undefined;
137
+ [Symbol.iterator](): Iterator<DetectionResult>;
138
+ }
139
+ /**
140
+ * Per-image classification envelope (Ultralytics-style `Results`).
141
+ */
142
+ export declare class ClassificationResults {
143
+ readonly probs: Probs;
144
+ readonly result: ClassificationResult;
145
+ readonly names: Readonly<Record<number, string>>;
146
+ readonly origImg: RGBImage;
147
+ readonly origShape: readonly [number, number];
148
+ readonly path: string | null;
149
+ readonly speed: Readonly<Record<string, number>>;
150
+ constructor(probs: Probs, result: ClassificationResult, names: Readonly<Record<number, string>>, origImg: RGBImage, origShape: readonly [number, number], path?: string | null, speed?: Readonly<Record<string, number>>);
151
+ /** Top-1 class index (Ultralytics-style alias). */
152
+ get cls(): number;
153
+ /** Top-1 confidence (Ultralytics-style alias). */
154
+ get conf(): number;
155
+ /** Top-1 class name. */
156
+ get name(): string;
157
+ /** Per-class probability list, sorted descending (legacy field). */
158
+ get probabilities(): readonly ClassificationResult["probabilities"][number][];
159
+ }
160
+ /**
161
+ * Per-image instance-segmentation envelope (Ultralytics-style `Results`).
162
+ *
163
+ * Iterating yields per-instance {@link SegmentationResult} entries. `boxes`
164
+ * and `masks` mirror Ultralytics' bulk-array views.
165
+ */
166
+ export declare class SegmentationResults implements Iterable<SegmentationResult> {
167
+ readonly boxes: Boxes;
168
+ readonly masks: Masks;
169
+ readonly detections: readonly SegmentationResult[];
170
+ readonly names: Readonly<Record<number, string>>;
171
+ readonly origImg: RGBImage;
172
+ readonly origShape: readonly [number, number];
173
+ readonly path: string | null;
174
+ readonly speed: Readonly<Record<string, number>>;
175
+ 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>>);
176
+ /** Number of surviving instances. */
177
+ get length(): number;
178
+ /** Index into the per-instance results. */
179
+ get(index: number): SegmentationResult | undefined;
180
+ [Symbol.iterator](): Iterator<SegmentationResult>;
181
+ }
182
+ //# sourceMappingURL=results.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"results.d.ts","sourceRoot":"","sources":["../src/results.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EACL,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,QAAQ,EACb,KAAK,kBAAkB,EACxB,MAAM,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