@hackbuild/vision 0.1.1 → 0.1.3
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 +258 -171
- package/dist/api.d.ts +5 -1
- package/dist/api.js +4 -1
- package/dist/faceapi.d.ts +1 -0
- package/dist/faceapi.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/loaders.d.ts +3 -1
- package/dist/loaders.js +3 -1
- package/dist/loop.d.ts +11 -8
- package/dist/loop.js +8 -7
- package/dist/masks.d.ts +9 -4
- package/dist/masks.js +9 -4
- package/dist/mediapipe.d.ts +7 -1
- package/dist/mediapipe.js +6 -1
- package/dist/records.d.ts +9 -3
- package/dist/records.js +241 -11
- package/dist/segment.d.ts +3 -2
- package/dist/segment.js +2 -2
- package/dist/signals.d.ts +8 -2
- package/dist/signals.js +3 -2
- package/dist/single/index.iife.js +336 -29
- package/dist/single/index.js +336 -30
- package/dist/stubs.d.ts +4 -0
- package/dist/stubs.js +4 -0
- package/dist/types.d.ts +13 -1
- package/dist/worker.d.ts +3 -0
- package/dist/yolo.d.ts +5 -1
- package/dist/yolo.js +4 -1
- package/llms.md +982 -0
- package/package.json +6 -5
package/dist/stubs.d.ts
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
import type { ModelRecord } from "@hackbuild/models";
|
|
7
7
|
import { type EmbeddingCache } from "./segment.js";
|
|
8
8
|
import type { DepthModel, Detector, Landmarker, Segmenter, Vlm } from "./types.js";
|
|
9
|
+
/** A stub depth model with no weights: a left to right ramp at the image's size. */
|
|
9
10
|
export declare function rampDepth(): DepthModel;
|
|
11
|
+
/** A stub detector with no weights: one centred box with the given label. */
|
|
10
12
|
export declare function centreDetector(label?: string): Detector;
|
|
11
13
|
/**
|
|
12
14
|
* A disc at the first positive point, or in the middle of the box, with a
|
|
@@ -20,7 +22,9 @@ export declare function discSegmenter(): Segmenter & {
|
|
|
20
22
|
}>;
|
|
21
23
|
encodes: number;
|
|
22
24
|
};
|
|
25
|
+
/** A stub landmarker with no weights: one list of `count` fixed points. */
|
|
23
26
|
export declare function fixedLandmarker(count: number, source?: string): Landmarker;
|
|
27
|
+
/** A stub vision language model with no weights: a fixed caption, or a fixed answer that repeats the question. */
|
|
24
28
|
export declare function fixedVlm(text?: string): Vlm;
|
|
25
29
|
/** Stub builders to merge into `stubFactory({...})`. */
|
|
26
30
|
export declare function visionStubs(): Record<string, (r: ModelRecord) => unknown>;
|
package/dist/stubs.js
CHANGED
|
@@ -8,6 +8,7 @@ const dims = (img) => {
|
|
|
8
8
|
const o = img;
|
|
9
9
|
return { width: o?.width ?? 64, height: o?.height ?? 48 };
|
|
10
10
|
};
|
|
11
|
+
/** A stub depth model with no weights: a left to right ramp at the image's size. */
|
|
11
12
|
export function rampDepth() {
|
|
12
13
|
return {
|
|
13
14
|
async estimate(img) {
|
|
@@ -20,6 +21,7 @@ export function rampDepth() {
|
|
|
20
21
|
},
|
|
21
22
|
};
|
|
22
23
|
}
|
|
24
|
+
/** A stub detector with no weights: one centred box with the given label. */
|
|
23
25
|
export function centreDetector(label = "person") {
|
|
24
26
|
return {
|
|
25
27
|
async detect(img) {
|
|
@@ -65,6 +67,7 @@ const centreBox = (img) => {
|
|
|
65
67
|
const r = Math.min(width, height) / 4;
|
|
66
68
|
return { x: width / 2 - r, y: height / 2 - r, w: 2 * r, h: 2 * r };
|
|
67
69
|
};
|
|
70
|
+
/** A stub landmarker with no weights: one list of `count` fixed points. */
|
|
68
71
|
export function fixedLandmarker(count, source = "stub") {
|
|
69
72
|
return {
|
|
70
73
|
async detect() {
|
|
@@ -72,6 +75,7 @@ export function fixedLandmarker(count, source = "stub") {
|
|
|
72
75
|
},
|
|
73
76
|
};
|
|
74
77
|
}
|
|
78
|
+
/** A stub vision language model with no weights: a fixed caption, or a fixed answer that repeats the question. */
|
|
75
79
|
export function fixedVlm(text = "a stub caption.") {
|
|
76
80
|
return { ask: async (_img, q) => (q ? `stub answer to: ${q}` : text) };
|
|
77
81
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -8,30 +8,35 @@ export interface Pixels {
|
|
|
8
8
|
height: number;
|
|
9
9
|
data: Uint8ClampedArray | Uint8Array;
|
|
10
10
|
}
|
|
11
|
+
/** A depth map: width, height, 0 to 1 per pixel with larger nearer, and the raw output range. */
|
|
11
12
|
export interface DepthMap {
|
|
12
13
|
width: number;
|
|
13
14
|
height: number;
|
|
14
15
|
/** 0 to 1 per pixel, larger is nearer (Depth Anything is relative inverse depth). */
|
|
15
16
|
data: Float32Array;
|
|
16
|
-
/** The raw model output range before normalizing, for stable scale across frames
|
|
17
|
+
/** The raw model output range before normalizing, for stable scale across frames. */
|
|
17
18
|
range: [min: number, max: number];
|
|
18
19
|
}
|
|
20
|
+
/** A loaded depth model: `estimate` returns a {@link DepthMap} for an image. */
|
|
19
21
|
export interface DepthModel {
|
|
20
22
|
estimate(image: ImageInput): Promise<DepthMap>;
|
|
21
23
|
dispose?(): unknown;
|
|
22
24
|
}
|
|
25
|
+
/** A rectangle in image pixels: left, top, width and height. */
|
|
23
26
|
export interface Box {
|
|
24
27
|
x: number;
|
|
25
28
|
y: number;
|
|
26
29
|
w: number;
|
|
27
30
|
h: number;
|
|
28
31
|
}
|
|
32
|
+
/** One detected object: its label, score and box. */
|
|
29
33
|
export interface Detection {
|
|
30
34
|
label: string;
|
|
31
35
|
score: number;
|
|
32
36
|
/** Pixels of the input image. */
|
|
33
37
|
box: Box;
|
|
34
38
|
}
|
|
39
|
+
/** A loaded detector: `detect` returns the objects in an image. */
|
|
35
40
|
export interface Detector {
|
|
36
41
|
detect(image: ImageInput, opts?: {
|
|
37
42
|
scoreThreshold?: number;
|
|
@@ -40,6 +45,7 @@ export interface Detector {
|
|
|
40
45
|
}): Promise<Detection[]>;
|
|
41
46
|
dispose?(): unknown;
|
|
42
47
|
}
|
|
48
|
+
/** A mask the size of its image: 0 or 255 per pixel, or a confidence 0 to 255, with an optional score. */
|
|
43
49
|
export interface Mask {
|
|
44
50
|
width: number;
|
|
45
51
|
height: number;
|
|
@@ -47,6 +53,7 @@ export interface Mask {
|
|
|
47
53
|
data: Uint8Array;
|
|
48
54
|
score?: number;
|
|
49
55
|
}
|
|
56
|
+
/** What to cut out: points (object or background) and a box, in image pixels. */
|
|
50
57
|
export interface SegmentPrompt {
|
|
51
58
|
/** Points in image pixels; label 1 for object, 0 for background (a negative point). */
|
|
52
59
|
points?: {
|
|
@@ -57,18 +64,21 @@ export interface SegmentPrompt {
|
|
|
57
64
|
/** A box around the object, image pixels. */
|
|
58
65
|
box?: Box;
|
|
59
66
|
}
|
|
67
|
+
/** How a segmenter keeps image embeddings: a stable key for the image and the cache to keep them in. */
|
|
60
68
|
export interface SegmentOptions {
|
|
61
69
|
/** A stable key for the image, such as a hash; the image object itself when omitted. */
|
|
62
70
|
key?: string;
|
|
63
71
|
/** The caller's embedding cache; the segmenter's own when omitted. */
|
|
64
72
|
cache?: EmbeddingCache<unknown>;
|
|
65
73
|
}
|
|
74
|
+
/** A loaded segmenter: `segment` returns masks for an image and prompt, best first. */
|
|
66
75
|
export interface Segmenter {
|
|
67
76
|
segment(image: ImageInput, prompt?: SegmentPrompt, o?: SegmentOptions): Promise<Mask[]>;
|
|
68
77
|
dispose?(): unknown;
|
|
69
78
|
}
|
|
70
79
|
/** "gesture" is hand landmarks plus a canned gesture label per hand (MediaPipe gesture recognizer). */
|
|
71
80
|
export type LandmarkKind = "face" | "hand" | "pose" | "gesture";
|
|
81
|
+
/** Landmarks for each face, hand or body, with blendshapes, handedness and gestures where the model gives them, and the record that answered. */
|
|
72
82
|
export interface Landmarks {
|
|
73
83
|
/** One list per face, hand or body; x and y normalized 0 to 1, z relative. */
|
|
74
84
|
points: [number, number, number][][];
|
|
@@ -84,12 +94,14 @@ export interface Landmarks {
|
|
|
84
94
|
/** Which record produced this, for status lines after a fallback. */
|
|
85
95
|
source: string;
|
|
86
96
|
}
|
|
97
|
+
/** A loaded landmark model: `detect` returns {@link Landmarks} for an image. */
|
|
87
98
|
export interface Landmarker {
|
|
88
99
|
detect(image: ImageInput, opts?: {
|
|
89
100
|
timestamp?: number;
|
|
90
101
|
}): Promise<Landmarks>;
|
|
91
102
|
dispose?(): unknown;
|
|
92
103
|
}
|
|
104
|
+
/** A loaded vision language model: `ask` answers a question about an image, or captions it. */
|
|
93
105
|
export interface Vlm {
|
|
94
106
|
/** Answer a question about an image; caption when the question is omitted. */
|
|
95
107
|
ask(image: ImageInput, question?: string, opts?: {
|
package/dist/worker.d.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { type Spawned, type SpawnOptions } from "@hackbuild/ai-env";
|
|
12
12
|
import type { DepthMap } from "./types.js";
|
|
13
13
|
import { type ModelRecord } from "@hackbuild/models";
|
|
14
|
+
/** Options for {@link depthWorker}: the transformers.js URLs, the depth model or record, the licence callback, device, dtype and worker rungs. */
|
|
14
15
|
export interface DepthWorkerOptions {
|
|
15
16
|
/** transformers.js ESM URLs tried in order (transformersUrls() from @hackbuild/devices). */
|
|
16
17
|
urls: readonly string[];
|
|
@@ -32,11 +33,13 @@ export interface DepthWorkerOptions {
|
|
|
32
33
|
/** Worker rungs; module blob first by default. */
|
|
33
34
|
spawn?: Pick<SpawnOptions, "prefer" | "Worker" | "createObjectURL" | "readyMs" | "prelude">;
|
|
34
35
|
}
|
|
36
|
+
/** RGBA pixels the page sends to the depth worker: ImageData, or anything with width, height and data. */
|
|
35
37
|
export interface Pixels {
|
|
36
38
|
width: number;
|
|
37
39
|
height: number;
|
|
38
40
|
data: Uint8ClampedArray | Uint8Array;
|
|
39
41
|
}
|
|
42
|
+
/** A depth model in a worker: the worker rung that started, the device, `estimate` and `dispose`. */
|
|
40
43
|
export interface DepthWorker {
|
|
41
44
|
/** The worker rung that started (module-blob, classic-blob or inline). */
|
|
42
45
|
readonly rung: Spawned["rung"];
|
package/dist/yolo.d.ts
CHANGED
|
@@ -7,19 +7,23 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { OrtModel } from "@hackbuild/devices";
|
|
9
9
|
import type { Box, Detection, Detector, Pixels } from "./types.js";
|
|
10
|
+
/** The 80 COCO class names, in YOLO class index order. */
|
|
10
11
|
export declare const COCO_LABELS: string[];
|
|
12
|
+
/** How an image was fitted into the square input: scale, left and top padding, and the square side. */
|
|
11
13
|
export interface Letterbox {
|
|
12
14
|
scale: number;
|
|
13
15
|
padX: number;
|
|
14
16
|
padY: number;
|
|
15
17
|
size: number;
|
|
16
18
|
}
|
|
19
|
+
/** The scale and padding that fit a `w` by `h` image into a `size` square (640 by default), centred. */
|
|
17
20
|
export declare function letterboxParams(w: number, h: number, size?: number): Letterbox;
|
|
18
21
|
/** RGBA pixels to a planar float tensor [1, 3, size, size] in 0 to 1, nearest neighbour, grey padding. */
|
|
19
22
|
export declare function letterboxTensor(px: Pixels, size?: number): {
|
|
20
23
|
data: Float32Array;
|
|
21
24
|
lb: Letterbox;
|
|
22
25
|
};
|
|
26
|
+
/** Intersection over union of two boxes, 0 to 1. */
|
|
23
27
|
export declare function iou(a: Box, b: Box): number;
|
|
24
28
|
/** Greedy NMS within each label, best first, then the top `max` overall. */
|
|
25
29
|
export declare function nms(dets: Detection[], iouThreshold?: number, max?: number): Detection[];
|
|
@@ -39,5 +43,5 @@ export declare function yoloDetector(m: OrtModel, o?: {
|
|
|
39
43
|
labels?: readonly string[];
|
|
40
44
|
toPixels?: (img: unknown) => Pixels;
|
|
41
45
|
}): Detector;
|
|
42
|
-
/** Detection cadence
|
|
46
|
+
/** Detection cadence: wait max(0.45 s, 4 times the last latency) between runs. */
|
|
43
47
|
export declare function detectCadenceMs(lastLatencyMs: number): number;
|
package/dist/yolo.js
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* suppression per class. Written from the published output layout; the
|
|
6
6
|
* weights are external and AGPL-3.0.
|
|
7
7
|
*/
|
|
8
|
+
/** The 80 COCO class names, in YOLO class index order. */
|
|
8
9
|
export const COCO_LABELS = ("person,bicycle,car,motorcycle,airplane,bus,train,truck,boat,traffic light,fire hydrant,stop sign,parking meter,bench,bird,cat,dog,horse,sheep,cow," +
|
|
9
10
|
"elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase,frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove,skateboard," +
|
|
10
11
|
"surfboard,tennis racket,bottle,wine glass,cup,fork,knife,spoon,bowl,banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,chair,couch," +
|
|
11
12
|
"potted plant,bed,dining table,toilet,tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster,sink,refrigerator,book,clock,vase,scissors," +
|
|
12
13
|
"teddy bear,hair drier,toothbrush").split(",");
|
|
14
|
+
/** The scale and padding that fit a `w` by `h` image into a `size` square (640 by default), centred. */
|
|
13
15
|
export function letterboxParams(w, h, size = 640) {
|
|
14
16
|
const scale = Math.min(size / w, size / h);
|
|
15
17
|
return { scale, padX: Math.floor((size - Math.round(w * scale)) / 2), padY: Math.floor((size - Math.round(h * scale)) / 2), size };
|
|
@@ -34,6 +36,7 @@ export function letterboxTensor(px, size = 640) {
|
|
|
34
36
|
}
|
|
35
37
|
return { data, lb };
|
|
36
38
|
}
|
|
39
|
+
/** Intersection over union of two boxes, 0 to 1. */
|
|
37
40
|
export function iou(a, b) {
|
|
38
41
|
const x1 = Math.max(a.x, b.x);
|
|
39
42
|
const y1 = Math.max(a.y, b.y);
|
|
@@ -107,7 +110,7 @@ export function yoloDetector(m, o = {}) {
|
|
|
107
110
|
dispose: () => m.dispose(),
|
|
108
111
|
};
|
|
109
112
|
}
|
|
110
|
-
/** Detection cadence
|
|
113
|
+
/** Detection cadence: wait max(0.45 s, 4 times the last latency) between runs. */
|
|
111
114
|
export function detectCadenceMs(lastLatencyMs) {
|
|
112
115
|
return Math.max(450, 4 * lastLatencyMs);
|
|
113
116
|
}
|