@hackbuild/vision 0.1.2 → 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 +257 -178
- 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/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 +3 -0
- package/dist/records.js +13 -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 +69 -29
- package/dist/single/index.js +69 -29
- 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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var HackbuildVision = (function(exports) {
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
// packages/models/src/record.ts
|
|
4
|
-
/** The onnxruntime-web ladder
|
|
4
|
+
/** The onnxruntime-web ladder to walk after one fetch: webgpu at basic, all, disabled, then wasm. */
|
|
5
5
|
function ortLadder() {
|
|
6
6
|
return [
|
|
7
7
|
{
|
|
@@ -35,6 +35,7 @@ var HackbuildVision = (function(exports) {
|
|
|
35
35
|
];
|
|
36
36
|
}
|
|
37
37
|
// packages/models/src/pins.ts
|
|
38
|
+
/** Pinned commits and file hashes per Hugging Face repo, written by tools/pin-weights.mjs. */
|
|
38
39
|
const PINS = {
|
|
39
40
|
"HuggingFaceTB/SmolLM2-135M-Instruct": {
|
|
40
41
|
"revision": "12fd25f77366fa6b3b4b768ec3050bf629380bac",
|
|
@@ -2883,6 +2884,7 @@ var HackbuildVision = (function(exports) {
|
|
|
2883
2884
|
* added to a registry, so every loader resolves `resolve/<commit>/` URLs,
|
|
2884
2885
|
* and pinnedSha gives the SHA-256 for such a URL.
|
|
2885
2886
|
*/
|
|
2887
|
+
/** The pin of a repo, or undefined when the repo is not pinned. */
|
|
2886
2888
|
function pinOf(repo) {
|
|
2887
2889
|
return repo && Object.hasOwn(PINS, repo) ? PINS[repo] : void 0;
|
|
2888
2890
|
}
|
|
@@ -2921,25 +2923,25 @@ var HackbuildVision = (function(exports) {
|
|
|
2921
2923
|
}
|
|
2922
2924
|
return spec.wasmFirst ? [...cpu, ...gpu] : [...gpu, ...cpu];
|
|
2923
2925
|
}
|
|
2924
|
-
/** onnxruntime-web rungs:
|
|
2926
|
+
/** onnxruntime-web rungs: webgpu then wasm, or wasm only when the graph is known to fail on webgpu. */
|
|
2925
2927
|
function ortRungs(opts = {}) {
|
|
2926
2928
|
const all = ortLadder();
|
|
2927
2929
|
return opts.webgpu === false ? all.filter((r) => r.device === "wasm") : all;
|
|
2928
2930
|
}
|
|
2929
|
-
/** Ladders by name,
|
|
2931
|
+
/** Ladders by name, one per model family. Each model record may copy one or write its own. */
|
|
2930
2932
|
const LADDERS = {
|
|
2931
|
-
/**
|
|
2933
|
+
/** Depth: fp16 then fp32 on webgpu, q8 then fp32 on wasm. */
|
|
2932
2934
|
depth: () => tjsLadder({
|
|
2933
2935
|
webgpu: ["fp16", "fp32"],
|
|
2934
2936
|
wasm: ["q8", "fp32"]
|
|
2935
2937
|
}),
|
|
2936
|
-
/**
|
|
2938
|
+
/** Perception models on cpu by default. */
|
|
2937
2939
|
perceptionCpu: () => tjsLadder({
|
|
2938
2940
|
webgpu: ["fp32"],
|
|
2939
2941
|
wasm: ["q8", "fp32"],
|
|
2940
2942
|
wasmFirst: true
|
|
2941
2943
|
}),
|
|
2942
|
-
/** whisper-large-v3-turbo (weights MIT)
|
|
2944
|
+
/** whisper-large-v3-turbo (weights MIT). */
|
|
2943
2945
|
whisperTurbo: () => tjsLadder({
|
|
2944
2946
|
webgpu: [{
|
|
2945
2947
|
encoder_model: "fp16",
|
|
@@ -2964,7 +2966,7 @@ var HackbuildVision = (function(exports) {
|
|
|
2964
2966
|
decoder_model_merged: "q8"
|
|
2965
2967
|
}]
|
|
2966
2968
|
}),
|
|
2967
|
-
/** SmolVLM (weights Apache-2.0): fp16 embed and vision, q4 decoder on webgpu; q8 vision on wasm
|
|
2969
|
+
/** SmolVLM (weights Apache-2.0): fp16 embed and vision, q4 decoder on webgpu; q8 vision on wasm. */
|
|
2968
2970
|
smolvlm: () => tjsLadder({
|
|
2969
2971
|
webgpu: [{
|
|
2970
2972
|
embed_tokens: "fp32",
|
|
@@ -2982,11 +2984,12 @@ var HackbuildVision = (function(exports) {
|
|
|
2982
2984
|
}],
|
|
2983
2985
|
freshHeapRetry: true
|
|
2984
2986
|
}),
|
|
2985
|
-
/**
|
|
2987
|
+
/** q4f16 on webgpu only. */
|
|
2986
2988
|
vlmGpuOnly: () => tjsLadder({ webgpu: ["q4f16", "q4"] }),
|
|
2987
2989
|
ort: ortRungs
|
|
2988
2990
|
};
|
|
2989
2991
|
// packages/ai-env/src/source.ts
|
|
2992
|
+
/** Applies string or RegExp rewrites to source text and reports each one that matched nothing. */
|
|
2990
2993
|
function rewriteSource(src, rewrites) {
|
|
2991
2994
|
let text = src;
|
|
2992
2995
|
const missed = [];
|
|
@@ -3016,7 +3019,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3016
3019
|
const MODULE_WORKER = /new\s+Worker\(\s*([^,()]+(?:\([^()]*\))?[^,()]*)\s*,\s*\{([^{}]*)\}\s*\)/g;
|
|
3017
3020
|
/**
|
|
3018
3021
|
* Turn module workers into classic workers: Chrome refuses module workers
|
|
3019
|
-
* from a file:// page
|
|
3022
|
+
* from a file:// page. Other option keys
|
|
3020
3023
|
* are kept. Returns the text and how many constructors changed.
|
|
3021
3024
|
*/
|
|
3022
3025
|
function toClassicWorker(src) {
|
|
@@ -3061,6 +3064,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3061
3064
|
}
|
|
3062
3065
|
return parts.join("");
|
|
3063
3066
|
}
|
|
3067
|
+
/** Encodes text as a base64 data: URL, JavaScript by default. */
|
|
3064
3068
|
function textToDataUrl(text, mime = "text/javascript") {
|
|
3065
3069
|
return `data:${mime};base64,${bytesToBase64(new TextEncoder().encode(text))}`;
|
|
3066
3070
|
}
|
|
@@ -3070,7 +3074,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3070
3074
|
*
|
|
3071
3075
|
* importEsm: plain import() first when nothing needs patching and the page
|
|
3072
3076
|
* is served over http(s); else fetch the text, patch it, import it from a
|
|
3073
|
-
* Blob URL, then from a base64 data: URL
|
|
3077
|
+
* Blob URL, then from a base64 data: URL. Each URL in
|
|
3074
3078
|
* `urls` is tried in order, so a CDN fallback list is one call.
|
|
3075
3079
|
*
|
|
3076
3080
|
* spawnWorker: the worker ladder, written once. Rungs are module-blob,
|
|
@@ -3089,6 +3093,10 @@ var HackbuildVision = (function(exports) {
|
|
|
3089
3093
|
const d = new Uint8Array(await subtle.digest("SHA-256", new TextEncoder().encode(text)));
|
|
3090
3094
|
return Array.from(d, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
3091
3095
|
}
|
|
3096
|
+
/**
|
|
3097
|
+
* The error importEsm and spawnWorker throw when every URL or rung failed, with one entry per
|
|
3098
|
+
* attempt in `attempts`.
|
|
3099
|
+
*/
|
|
3092
3100
|
var LoadError = class extends Error {
|
|
3093
3101
|
attempts;
|
|
3094
3102
|
constructor(message, attempts) {
|
|
@@ -3104,6 +3112,10 @@ var HackbuildVision = (function(exports) {
|
|
|
3104
3112
|
function errText(e) {
|
|
3105
3113
|
return e instanceof Error ? `${e.name}: ${e.message}` : String(e);
|
|
3106
3114
|
}
|
|
3115
|
+
/**
|
|
3116
|
+
* Imports an ES module from the first URL that loads, through plain import, then a patched Blob
|
|
3117
|
+
* URL, then a base64 data: URL, checking a SHA-256 when you give one.
|
|
3118
|
+
*/
|
|
3107
3119
|
async function importEsm(urls, opts = {}) {
|
|
3108
3120
|
const list = typeof urls === "string" ? [urls] : urls;
|
|
3109
3121
|
const importer = opts.importer ?? nativeImport;
|
|
@@ -3399,6 +3411,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3399
3411
|
* only for a URL the app supplies; YOLOv10 n, s and m, FastSAM and the face
|
|
3400
3412
|
* swap set sit in PICKER_RECORDS.
|
|
3401
3413
|
*/
|
|
3414
|
+
/** The MediaPipe model storage base URL the MediaPipe records load from. */
|
|
3402
3415
|
const MP_MODELS = "https://storage.googleapis.com/mediapipe-models/";
|
|
3403
3416
|
/** MediaPipe ladder: GPU delegate (webgl), then the CPU delegate (wasm). */
|
|
3404
3417
|
const mediapipeLadder = () => [{ device: "webgl" }, { device: "wasm" }];
|
|
@@ -3419,6 +3432,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3419
3432
|
place: "main",
|
|
3420
3433
|
runtime: "@mediapipe/tasks-vision@1.2.3"
|
|
3421
3434
|
});
|
|
3435
|
+
/** The vision records registered by `attachVision`: Depth Anything V2 small, MediaPipe detection, segmentation and landmarks, SlimSAM, SmolVLM 256M and LFM2-VL 450M. */
|
|
3422
3436
|
const VISION_RECORDS = [
|
|
3423
3437
|
{
|
|
3424
3438
|
id: "depth-anything-v2-small",
|
|
@@ -3436,7 +3450,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3436
3450
|
commercial: true,
|
|
3437
3451
|
place: "cpu-worker",
|
|
3438
3452
|
runtime: "@huggingface/transformers@4.3.2",
|
|
3439
|
-
notes: ["
|
|
3453
|
+
notes: ["fp16 on webgpu, fp32 on the same device after a failure"]
|
|
3440
3454
|
},
|
|
3441
3455
|
mp("efficientdet-lite0", "detect", "object_detector/efficientdet_lite0/float16/1/efficientdet_lite0.tflite", 7, "mediapipe efficientdet lite0 (coco, 80 classes)", "light, 80 things"),
|
|
3442
3456
|
mp("selfie-segmenter", "segment.person", "image_segmenter/selfie_segmenter/float16/latest/selfie_segmenter.tflite", 1, "mediapipe selfie segmenter"),
|
|
@@ -3468,7 +3482,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3468
3482
|
commercial: true,
|
|
3469
3483
|
place: "cpu-worker",
|
|
3470
3484
|
runtime: "@huggingface/transformers@4.3.2",
|
|
3471
|
-
notes: ["
|
|
3485
|
+
notes: ["one image embedding, then any number of point prompts"]
|
|
3472
3486
|
},
|
|
3473
3487
|
{
|
|
3474
3488
|
id: "smolvlm-256m-instruct",
|
|
@@ -3511,13 +3525,12 @@ var HackbuildVision = (function(exports) {
|
|
|
3511
3525
|
commercial: "unknown",
|
|
3512
3526
|
place: "gpu",
|
|
3513
3527
|
runtime: "@huggingface/transformers@4.3.2",
|
|
3514
|
-
notes: ["
|
|
3528
|
+
notes: ["q4f16 on webgpu only; the licence limits commercial use by revenue, read it before shipping"]
|
|
3515
3529
|
}
|
|
3516
3530
|
];
|
|
3517
3531
|
/**
|
|
3518
3532
|
* Records listed for pickers and never registered by default, so a hub's
|
|
3519
|
-
* routing by capability keeps its small defaults: larger alternates
|
|
3520
|
-
* earlier tools offered (smolvlm 500m and 2 video, gemma 4 e2b, two more
|
|
3533
|
+
* routing by capability keeps its small defaults: larger alternates (smolvlm 500m and 2 video, gemma 4 e2b, two more
|
|
3521
3534
|
* depth models), and records under licences that limit use (YOLOv10 and
|
|
3522
3535
|
* FastSAM are AGPL-3.0 upstream, Ultralytics; the face swap set is
|
|
3523
3536
|
* InsightFace, non-commercial). A hub that runs one adds it itself. Sizes
|
|
@@ -3551,8 +3564,9 @@ var HackbuildVision = (function(exports) {
|
|
|
3551
3564
|
licence: "AGPL-3.0 (Ultralytics weights, the model card's licence field)",
|
|
3552
3565
|
commercial: false,
|
|
3553
3566
|
place: "gpu",
|
|
3554
|
-
notes: ["output [1, 300, 6] after the model's own selection, so no nms pass (vendor, the yolov10 paper)", "
|
|
3567
|
+
notes: ["output [1, 300, 6] after the model's own selection, so no nms pass (vendor, the yolov10 paper)", "comes in nano, small and medium"]
|
|
3555
3568
|
});
|
|
3569
|
+
/** Records listed for pickers and never registered by default: larger alternates, and records under licences that limit use (YOLOv10, FastSAM, the face swap set). */
|
|
3556
3570
|
const PICKER_RECORDS = [
|
|
3557
3571
|
{
|
|
3558
3572
|
id: "smolvlm-500m-instruct",
|
|
@@ -3595,7 +3609,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3595
3609
|
commercial: true,
|
|
3596
3610
|
place: "cpu-worker",
|
|
3597
3611
|
runtime: "@huggingface/transformers@4.3.2",
|
|
3598
|
-
notes: ["an
|
|
3612
|
+
notes: ["an option beside smolvlm 256m and 500m"]
|
|
3599
3613
|
},
|
|
3600
3614
|
{
|
|
3601
3615
|
id: "depth-anything-v2-base",
|
|
@@ -3652,7 +3666,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3652
3666
|
commercial: true,
|
|
3653
3667
|
place: "gpu",
|
|
3654
3668
|
runtime: "@huggingface/transformers@4.3.2",
|
|
3655
|
-
notes: ["
|
|
3669
|
+
notes: ["about 3.3 GB (vendor)"]
|
|
3656
3670
|
},
|
|
3657
3671
|
yolo10("n", 9, "instant", "nano, quick"),
|
|
3658
3672
|
yolo10("s", 28, "fast", "small, sharper"),
|
|
@@ -3681,7 +3695,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3681
3695
|
licence: "AGPL-3.0 upstream (Ultralytics FastSAM); the export's card says apache-2.0",
|
|
3682
3696
|
commercial: false,
|
|
3683
3697
|
place: "cpu-worker",
|
|
3684
|
-
notes: ["
|
|
3698
|
+
notes: ["finds every mask in one pass on onnxruntime-web (93 MB, vendor)", "segment everything: prototype masks, nms, then a pick by click"]
|
|
3685
3699
|
},
|
|
3686
3700
|
{
|
|
3687
3701
|
id: "scrfd-10g",
|
|
@@ -3754,7 +3768,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3754
3768
|
licence: "InsightFace: non-commercial research only; the mirror's card says gpl-3.0",
|
|
3755
3769
|
commercial: false,
|
|
3756
3770
|
place: "gpu",
|
|
3757
|
-
notes: ["128 px faces;
|
|
3771
|
+
notes: ["128 px faces; paste the result back with a feathered mask"]
|
|
3758
3772
|
}
|
|
3759
3773
|
];
|
|
3760
3774
|
/** A face landmark fallback record over face-api.js tiny nets (code MIT; weights MIT from @vladmandic/face-api). */
|
|
@@ -3802,7 +3816,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3802
3816
|
licence: o.licence ?? "AGPL-3.0 (Ultralytics YOLOv8 weights), supplied by the app",
|
|
3803
3817
|
commercial: false,
|
|
3804
3818
|
place: "gpu",
|
|
3805
|
-
notes: ["
|
|
3819
|
+
notes: ["640 letterbox, per class nms at iou 0.45, at most 13 boxes"]
|
|
3806
3820
|
};
|
|
3807
3821
|
}
|
|
3808
3822
|
// packages/vision/src/segment.ts
|
|
@@ -3887,7 +3901,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3887
3901
|
const R = tjs.RawImage;
|
|
3888
3902
|
return R?.read ? R.read(img) : img;
|
|
3889
3903
|
}
|
|
3890
|
-
/** Normalize a depth pipeline result. predicted_depth keeps the raw range
|
|
3904
|
+
/** Normalize a depth pipeline result. predicted_depth keeps the raw range; depth is the 8 bit image. */
|
|
3891
3905
|
function depthFromOutput(out) {
|
|
3892
3906
|
const pd = out.predicted_depth;
|
|
3893
3907
|
if (pd && pd.data && pd.dims?.length >= 2) {
|
|
@@ -3922,6 +3936,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3922
3936
|
range: [0, 255]
|
|
3923
3937
|
};
|
|
3924
3938
|
}
|
|
3939
|
+
/** The transformers.js loader for depth records: a depth-estimation pipeline wrapped as a {@link DepthModel}. */
|
|
3925
3940
|
async function depthLoader(ctx) {
|
|
3926
3941
|
const pipe = await ctx.tjs.pipeline("depth-estimation", ctx.record.source.modelId, ctx.options);
|
|
3927
3942
|
return {
|
|
@@ -3929,6 +3944,7 @@ var HackbuildVision = (function(exports) {
|
|
|
3929
3944
|
dispose: () => pipe.dispose?.()
|
|
3930
3945
|
};
|
|
3931
3946
|
}
|
|
3947
|
+
/** The transformers.js loader for caption and question records: the VLM's processor and model wrapped as a {@link Vlm}. */
|
|
3932
3948
|
async function vlmLoader(ctx) {
|
|
3933
3949
|
const id = ctx.record.source.modelId;
|
|
3934
3950
|
const processor = await cls(ctx.tjs, "AutoProcessor").from_pretrained(id);
|
|
@@ -4016,7 +4032,9 @@ var HackbuildVision = (function(exports) {
|
|
|
4016
4032
|
});
|
|
4017
4033
|
}
|
|
4018
4034
|
// packages/vision/src/yolo.ts
|
|
4035
|
+
/** The 80 COCO class names, in YOLO class index order. */
|
|
4019
4036
|
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,elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase,frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove,skateboard,surfboard,tennis racket,bottle,wine glass,cup,fork,knife,spoon,bowl,banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,chair,couch,potted plant,bed,dining table,toilet,tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster,sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier,toothbrush".split(",");
|
|
4037
|
+
/** The scale and padding that fit a `w` by `h` image into a `size` square (640 by default), centred. */
|
|
4020
4038
|
function letterboxParams(w, h, size = 640) {
|
|
4021
4039
|
const scale = Math.min(size / w, size / h);
|
|
4022
4040
|
return {
|
|
@@ -4049,6 +4067,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4049
4067
|
lb
|
|
4050
4068
|
};
|
|
4051
4069
|
}
|
|
4070
|
+
/** Intersection over union of two boxes, 0 to 1. */
|
|
4052
4071
|
function iou(a, b) {
|
|
4053
4072
|
const x1 = Math.max(a.x, b.x);
|
|
4054
4073
|
const y1 = Math.max(a.y, b.y);
|
|
@@ -4132,7 +4151,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4132
4151
|
dispose: () => m.dispose()
|
|
4133
4152
|
};
|
|
4134
4153
|
}
|
|
4135
|
-
/** Detection cadence
|
|
4154
|
+
/** Detection cadence: wait max(0.45 s, 4 times the last latency) between runs. */
|
|
4136
4155
|
function detectCadenceMs(lastLatencyMs) {
|
|
4137
4156
|
return Math.max(450, 4 * lastLatencyMs);
|
|
4138
4157
|
}
|
|
@@ -4162,6 +4181,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4162
4181
|
}
|
|
4163
4182
|
}
|
|
4164
4183
|
const target = (cap, o) => o.model ? { id: o.model } : cap;
|
|
4184
|
+
/** A depth map of an image from the routed depth model (Depth Anything V2 small by default), loaded and released through the hub. */
|
|
4165
4185
|
function depth(hub, image, o = {}) {
|
|
4166
4186
|
return withModel(hub, target("depth", o), (m) => m.estimate(image), o);
|
|
4167
4187
|
}
|
|
@@ -4169,6 +4189,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4169
4189
|
if (v && typeof v === "object" && "sessions" in v) return yoloDetector(v, toPixels ? { toPixels } : {});
|
|
4170
4190
|
return v;
|
|
4171
4191
|
}
|
|
4192
|
+
/** Objects in an image from the routed detector (MediaPipe EfficientDet by default, or a YOLO ort model), loaded and released through the hub. */
|
|
4172
4193
|
function detect(hub, image, o = {}) {
|
|
4173
4194
|
const opts = {};
|
|
4174
4195
|
if (o.scoreThreshold !== void 0) opts.scoreThreshold = o.scoreThreshold;
|
|
@@ -4226,6 +4247,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4226
4247
|
function caption(hub, image, o = {}) {
|
|
4227
4248
|
return hub.chain("vlm")(() => withModel(hub, target("vision.caption", o), (m) => m.ask(image, void 0, o.maxTokens ? { maxTokens: o.maxTokens } : {}), o));
|
|
4228
4249
|
}
|
|
4250
|
+
/** Answer a question about an image with the routed vision language model. VLM calls are serialized on one chain. */
|
|
4229
4251
|
function ask(hub, image, question, o = {}) {
|
|
4230
4252
|
return hub.chain("vlm")(() => withModel(hub, target("vision.ask", o), (m) => m.ask(image, question, o.maxTokens ? { maxTokens: o.maxTokens } : {}), o));
|
|
4231
4253
|
}
|
|
@@ -4234,17 +4256,21 @@ var HackbuildVision = (function(exports) {
|
|
|
4234
4256
|
* A MediaPipe tasks-vision provider for the models hub. One wasm fileset
|
|
4235
4257
|
* per page; one task per record, created with the GPU delegate on a
|
|
4236
4258
|
* "webgl" rung and the CPU delegate on a "wasm" rung, so a failed GPU
|
|
4237
|
-
* delegate falls to the CPU one
|
|
4259
|
+
* delegate falls to the CPU one.
|
|
4238
4260
|
* Results are turned into the package's Detector, Segmenter and
|
|
4239
4261
|
* Landmarker shapes.
|
|
4240
4262
|
*/
|
|
4263
|
+
/** The pinned @mediapipe/tasks-vision version, 1.2.3. */
|
|
4241
4264
|
const TASKS_VISION = "1.2.3";
|
|
4265
|
+
/** The URL @mediapipe/tasks-vision is imported from, on jsDelivr. */
|
|
4242
4266
|
function tasksVisionUrls(version = TASKS_VISION) {
|
|
4243
4267
|
return [`https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@${version}/vision_bundle.mjs`];
|
|
4244
4268
|
}
|
|
4269
|
+
/** The directory of the tasks-vision wasm fileset, on jsDelivr. */
|
|
4245
4270
|
function tasksVisionWasm(version = TASKS_VISION) {
|
|
4246
4271
|
return `https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@${version}/wasm`;
|
|
4247
4272
|
}
|
|
4273
|
+
/** A loader that imports the pinned @mediapipe/tasks-vision at run time; pass it as `runtime` to {@link mediapipeProvider}. */
|
|
4248
4274
|
const loadTasksVision = (version = TASKS_VISION) => () => importEsm(tasksVisionUrls(version));
|
|
4249
4275
|
const CLASS = {
|
|
4250
4276
|
detect: "ObjectDetector",
|
|
@@ -4333,6 +4359,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4333
4359
|
dispose: () => call(t, "close")
|
|
4334
4360
|
};
|
|
4335
4361
|
}
|
|
4362
|
+
/** The MediaPipe tasks-vision provider for the models hub: one task per record, the GPU delegate on a webgl rung and the CPU delegate on a wasm rung. */
|
|
4336
4363
|
function mediapipeProvider(opts) {
|
|
4337
4364
|
let fileset = null;
|
|
4338
4365
|
return async ({ record, rung, signal }) => {
|
|
@@ -4370,6 +4397,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4370
4397
|
};
|
|
4371
4398
|
}
|
|
4372
4399
|
// packages/vision/src/faceapi.ts
|
|
4400
|
+
/** The face-api.js provider for the models hub: the tiny face detector and 68 point tiny landmark net, as a face {@link Landmarker}. */
|
|
4373
4401
|
function faceApiProvider(opts) {
|
|
4374
4402
|
return async ({ record }) => {
|
|
4375
4403
|
const fa = await opts.runtime();
|
|
@@ -4389,6 +4417,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4389
4417
|
};
|
|
4390
4418
|
}
|
|
4391
4419
|
// packages/vision/src/loop.ts
|
|
4420
|
+
/** Run a perception step beside a render loop, one at a time, each started no sooner than `interval(lastLatency)` after the previous one began. */
|
|
4392
4421
|
function perceptionLoop(o) {
|
|
4393
4422
|
const now = o.now ?? (() => globalThis.performance ? performance.now() : Date.now());
|
|
4394
4423
|
const g = globalThis;
|
|
@@ -4482,7 +4511,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4482
4511
|
}
|
|
4483
4512
|
};
|
|
4484
4513
|
}
|
|
4485
|
-
/** A small LRU of results by key
|
|
4514
|
+
/** A small LRU of results by key, so a depth map is computed once per image. */
|
|
4486
4515
|
var ResultCache = class {
|
|
4487
4516
|
max;
|
|
4488
4517
|
m = /* @__PURE__ */ new Map();
|
|
@@ -4513,7 +4542,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4513
4542
|
return this.m.size;
|
|
4514
4543
|
}
|
|
4515
4544
|
};
|
|
4516
|
-
/** A depth map as an 8 bit grey RGBA buffer, for a canvas or a PNG (
|
|
4545
|
+
/** A depth map as an 8 bit grey RGBA buffer, for a canvas or a PNG (store it as a PNG data URL to keep it with a design). */
|
|
4517
4546
|
function depthToRgba(d) {
|
|
4518
4547
|
const out = new Uint8ClampedArray(d.width * d.height * 4);
|
|
4519
4548
|
for (let i = 0; i < d.width * d.height; i++) {
|
|
@@ -4523,7 +4552,7 @@ var HackbuildVision = (function(exports) {
|
|
|
4523
4552
|
}
|
|
4524
4553
|
return out;
|
|
4525
4554
|
}
|
|
4526
|
-
/** Scale so the longest side is at most maxPx
|
|
4555
|
+
/** Scale so the longest side is at most maxPx. */
|
|
4527
4556
|
function fitSize(w, h, maxPx = 1024) {
|
|
4528
4557
|
const s = Math.min(1, maxPx / Math.max(w, h));
|
|
4529
4558
|
return {
|
|
@@ -4665,6 +4694,7 @@ self.onmessage = async (ev) => {
|
|
|
4665
4694
|
height: o?.height ?? 48
|
|
4666
4695
|
};
|
|
4667
4696
|
};
|
|
4697
|
+
/** A stub depth model with no weights: a left to right ramp at the image's size. */
|
|
4668
4698
|
function rampDepth() {
|
|
4669
4699
|
return { async estimate(img) {
|
|
4670
4700
|
const { width, height } = dims(img);
|
|
@@ -4678,6 +4708,7 @@ self.onmessage = async (ev) => {
|
|
|
4678
4708
|
};
|
|
4679
4709
|
} };
|
|
4680
4710
|
}
|
|
4711
|
+
/** A stub detector with no weights: one centred box with the given label. */
|
|
4681
4712
|
function centreDetector(label = "person") {
|
|
4682
4713
|
return { async detect(img) {
|
|
4683
4714
|
const { width, height } = dims(img);
|
|
@@ -4743,6 +4774,7 @@ self.onmessage = async (ev) => {
|
|
|
4743
4774
|
h: 2 * r
|
|
4744
4775
|
};
|
|
4745
4776
|
};
|
|
4777
|
+
/** A stub landmarker with no weights: one list of `count` fixed points. */
|
|
4746
4778
|
function fixedLandmarker(count, source = "stub") {
|
|
4747
4779
|
return { async detect() {
|
|
4748
4780
|
return {
|
|
@@ -4755,6 +4787,7 @@ self.onmessage = async (ev) => {
|
|
|
4755
4787
|
};
|
|
4756
4788
|
} };
|
|
4757
4789
|
}
|
|
4790
|
+
/** A stub vision language model with no weights: a fixed caption, or a fixed answer that repeats the question. */
|
|
4758
4791
|
function fixedVlm(text = "a stub caption.") {
|
|
4759
4792
|
return { ask: async (_img, q) => q ? `stub answer to: ${q}` : text };
|
|
4760
4793
|
}
|
|
@@ -4800,19 +4833,22 @@ self.onmessage = async (ev) => {
|
|
|
4800
4833
|
function maskThreshold(m, t = ON) {
|
|
4801
4834
|
return make(m, m.data.map((v) => v >= t ? 255 : 0));
|
|
4802
4835
|
}
|
|
4836
|
+
/** Pixels inside either mask, the larger value of each. Throws when the sizes differ. */
|
|
4803
4837
|
function maskUnion(a, b) {
|
|
4804
4838
|
same("maskUnion", a, b);
|
|
4805
4839
|
return make(a, a.data.map((v, i) => Math.max(v, b.data[i])));
|
|
4806
4840
|
}
|
|
4841
|
+
/** Pixels inside both masks, the smaller value of each. Throws when the sizes differ. */
|
|
4807
4842
|
function maskIntersect(a, b) {
|
|
4808
4843
|
same("maskIntersect", a, b);
|
|
4809
4844
|
return make(a, a.data.map((v, i) => Math.min(v, b.data[i])));
|
|
4810
4845
|
}
|
|
4811
|
-
/** `a` with `b` taken out (shift with a selection tool in
|
|
4846
|
+
/** `a` with `b` taken out (shift with a selection tool in a mask editor). */
|
|
4812
4847
|
function maskSubtract(a, b) {
|
|
4813
4848
|
same("maskSubtract", a, b);
|
|
4814
4849
|
return make(a, a.data.map((v, i) => Math.min(v, 255 - b.data[i])));
|
|
4815
4850
|
}
|
|
4851
|
+
/** The mask with inside and outside swapped, 255 minus each value. */
|
|
4816
4852
|
function maskInvert(m) {
|
|
4817
4853
|
return make(m, m.data.map((v) => 255 - v));
|
|
4818
4854
|
}
|
|
@@ -4866,7 +4902,7 @@ self.onmessage = async (ev) => {
|
|
|
4866
4902
|
const d = distance2((i) => m.data[i] < ON, m.width, m.height);
|
|
4867
4903
|
return make(m, Uint8Array.from(d, (v) => v > r * r ? 255 : 0));
|
|
4868
4904
|
}
|
|
4869
|
-
/** Soft edges: a gaussian of radius `r` with sigma r / 2 (
|
|
4905
|
+
/** Soft edges: a gaussian of radius `r` with sigma r / 2 (a smooth without a rethreshold), edges clamped. */
|
|
4870
4906
|
function maskFeather(m, r) {
|
|
4871
4907
|
if (r <= 0) return make(m, m.data.slice());
|
|
4872
4908
|
const { width: w, height: h } = m;
|
|
@@ -5107,6 +5143,7 @@ self.onmessage = async (ev) => {
|
|
|
5107
5143
|
const d = maskOutline(m).map((l) => `M${l.map((p) => `${p[0]} ${p[1]}`).join("L")}Z`).join("");
|
|
5108
5144
|
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${m.width} ${m.height}" width="${m.width}" height="${m.height}"><path d="${d}" fill="${o.fill ?? "#000"}" fill-rule="evenodd"/></svg>`;
|
|
5109
5145
|
}
|
|
5146
|
+
/** The mask as COCO style uncompressed run length, column major, 128 and up inside. */
|
|
5110
5147
|
function maskToRle(m) {
|
|
5111
5148
|
const { width: w, height: h } = m;
|
|
5112
5149
|
const counts = [];
|
|
@@ -5127,6 +5164,7 @@ self.onmessage = async (ev) => {
|
|
|
5127
5164
|
counts
|
|
5128
5165
|
};
|
|
5129
5166
|
}
|
|
5167
|
+
/** A mask from COCO style run length, inside 255. Throws when the counts do not cover the image. */
|
|
5130
5168
|
function maskFromRle(r) {
|
|
5131
5169
|
const [h, w] = r.size;
|
|
5132
5170
|
const data = new Uint8Array(w * h);
|
|
@@ -5178,6 +5216,7 @@ self.onmessage = async (ev) => {
|
|
|
5178
5216
|
frown: 0,
|
|
5179
5217
|
gesture: null
|
|
5180
5218
|
});
|
|
5219
|
+
/** Turns raw face and hand frames into steady signals and discrete events, smoothed or held so one noisy frame never fires a reaction. */
|
|
5181
5220
|
var SignalTracker = class {
|
|
5182
5221
|
p = absent();
|
|
5183
5222
|
lastSeen = -Infinity;
|
|
@@ -5438,6 +5477,7 @@ self.onmessage = async (ev) => {
|
|
|
5438
5477
|
};
|
|
5439
5478
|
/** Questions that call for an on demand look rather than the steady signals. Replace it to suit a persona. */
|
|
5440
5479
|
const LOOK_PATTERN = /\b(what (am i|is this|'?s this|do you see|can you see)|can you see|do you see|look at (this|me|my)|what('?s| is) in my hand|holding|wearing|show(ing)? you)\b/i;
|
|
5480
|
+
/** True when a message asks the character to look, by {@link LOOK_PATTERN} or the pattern you pass. */
|
|
5441
5481
|
const wantsLook = (text, pattern = LOOK_PATTERN) => pattern.test(text);
|
|
5442
5482
|
/** Default reactions (vendor): greet on arrival, smile back after 600 ms at 0.7 chance, answer waves and a few gestures. */
|
|
5443
5483
|
const DEFAULT_REACTIONS = [
|