@hackbuild/ai-views 0.1.19 → 0.1.21
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 -1
- package/dist/single/index.iife.js +138 -31
- package/dist/single/index.js +138 -31
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -257,6 +257,6 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
257
257
|
| `dist/index.js` (main entry, imports external) | 21,695 bytes | 22,779 bytes |
|
|
258
258
|
| `dist/single/elements.iife.js` (single file build) | 104,791 bytes | 110,030 bytes |
|
|
259
259
|
|
|
260
|
-
Measured 2026-12-
|
|
260
|
+
Measured 2026-12-03 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
261
261
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
262
262
|
5 percent from its row.
|
|
@@ -13667,6 +13667,37 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13667
13667
|
}
|
|
13668
13668
|
];
|
|
13669
13669
|
}));
|
|
13670
|
+
// packages/vision/src/release.ts
|
|
13671
|
+
/**
|
|
13672
|
+
* Disposes every tensor among the values: a tensor itself, tensors in an
|
|
13673
|
+
* array, and tensors among the values of a plain record (a processor's or
|
|
13674
|
+
* a session's output), two levels deep. Anything else is left alone, and a
|
|
13675
|
+
* dispose that throws (already freed) is ignored.
|
|
13676
|
+
* @internal
|
|
13677
|
+
*/
|
|
13678
|
+
function release(...values) {
|
|
13679
|
+
const seen = /* @__PURE__ */ new Set();
|
|
13680
|
+
const walk = (v, depth) => {
|
|
13681
|
+
if (!v || typeof v !== "object" || seen.has(v) || depth > 2) return;
|
|
13682
|
+
seen.add(v);
|
|
13683
|
+
if (isTensor(v)) {
|
|
13684
|
+
try {
|
|
13685
|
+
v.dispose();
|
|
13686
|
+
} catch {}
|
|
13687
|
+
return;
|
|
13688
|
+
}
|
|
13689
|
+
if (Array.isArray(v)) {
|
|
13690
|
+
for (const x of v) walk(x, depth + 1);
|
|
13691
|
+
return;
|
|
13692
|
+
}
|
|
13693
|
+
if (Object.getPrototypeOf(v) === Object.prototype) for (const x of Object.values(v)) walk(x, depth + 1);
|
|
13694
|
+
};
|
|
13695
|
+
for (const v of values) walk(v, 0);
|
|
13696
|
+
}
|
|
13697
|
+
var isTensor;
|
|
13698
|
+
var init_release = __esmMin((() => {
|
|
13699
|
+
isTensor = (v) => !!v && typeof v === "object" && typeof v.dispose === "function" && Array.isArray(v.dims);
|
|
13700
|
+
}));
|
|
13670
13701
|
// packages/vision/src/segment.ts
|
|
13671
13702
|
/** True when the prompt has a point or a box. */
|
|
13672
13703
|
function hasPrompt(p) {
|
|
@@ -13674,29 +13705,71 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13674
13705
|
}
|
|
13675
13706
|
/** A segmenter from an encoder and a decoder, with the embedding cache on `cache`. Masks come best first, scores clamped to 0 to 1. */
|
|
13676
13707
|
function promptSegmenter(o) {
|
|
13677
|
-
const
|
|
13708
|
+
const reading = /* @__PURE__ */ new Map();
|
|
13709
|
+
const evicted = /* @__PURE__ */ new Set();
|
|
13710
|
+
const free = (e) => {
|
|
13711
|
+
if (!o.release) return;
|
|
13712
|
+
if (reading.get(e)) evicted.add(e);
|
|
13713
|
+
else o.release(e);
|
|
13714
|
+
};
|
|
13715
|
+
const own = o.cache ?? new EmbeddingCache(4, free);
|
|
13678
13716
|
const seg = {
|
|
13679
13717
|
cache: own,
|
|
13680
13718
|
async segment(image, prompt = {}, so = {}) {
|
|
13681
13719
|
if (!hasPrompt(prompt)) throw new Error("a prompt needs at least one point or a box");
|
|
13682
13720
|
const emb = await (so.cache ?? own).getOr(so.key ?? image, () => o.encode(image));
|
|
13683
|
-
|
|
13721
|
+
reading.set(emb, (reading.get(emb) ?? 0) + 1);
|
|
13722
|
+
let masks;
|
|
13723
|
+
try {
|
|
13724
|
+
masks = await o.decode(emb, prompt, image);
|
|
13725
|
+
} finally {
|
|
13726
|
+
const n = (reading.get(emb) ?? 1) - 1;
|
|
13727
|
+
if (n > 0) reading.set(emb, n);
|
|
13728
|
+
else {
|
|
13729
|
+
reading.delete(emb);
|
|
13730
|
+
if (evicted.delete(emb) && !own.holds(emb)) o.release?.(emb);
|
|
13731
|
+
}
|
|
13732
|
+
}
|
|
13684
13733
|
for (const m of masks) if (m.score !== void 0) m.score = Math.min(1, Math.max(0, m.score));
|
|
13685
13734
|
return masks.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));
|
|
13686
13735
|
}
|
|
13687
13736
|
};
|
|
13688
|
-
|
|
13737
|
+
seg.dispose = () => {
|
|
13738
|
+
if (!o.cache) own.clear();
|
|
13739
|
+
return o.dispose?.();
|
|
13740
|
+
};
|
|
13689
13741
|
return seg;
|
|
13690
13742
|
}
|
|
13691
13743
|
var EmbeddingCache;
|
|
13692
13744
|
var init_segment = __esmMin((() => {
|
|
13693
13745
|
EmbeddingCache = class {
|
|
13694
13746
|
max;
|
|
13747
|
+
onEvict;
|
|
13695
13748
|
m = /* @__PURE__ */ new Map();
|
|
13749
|
+
got = /* @__PURE__ */ new WeakMap();
|
|
13696
13750
|
hits = 0;
|
|
13697
13751
|
misses = 0;
|
|
13698
|
-
|
|
13752
|
+
/** `onEvict` gets each embedding that leaves the cache (pushed out, deleted, cleared or replaced), to free it. */
|
|
13753
|
+
constructor(max = 4, onEvict) {
|
|
13699
13754
|
this.max = max;
|
|
13755
|
+
this.onEvict = onEvict;
|
|
13756
|
+
}
|
|
13757
|
+
out(p) {
|
|
13758
|
+
if (p && this.onEvict) {
|
|
13759
|
+
const f = this.onEvict;
|
|
13760
|
+
p.then((e) => {
|
|
13761
|
+
if (!this.holds(e)) f(e);
|
|
13762
|
+
}, () => {});
|
|
13763
|
+
}
|
|
13764
|
+
}
|
|
13765
|
+
/** True when the embedding is still in the cache under some key. */
|
|
13766
|
+
holds(e) {
|
|
13767
|
+
for (const q of this.m.values()) if (this.got.get(q) === e) return true;
|
|
13768
|
+
return false;
|
|
13769
|
+
}
|
|
13770
|
+
keep(p) {
|
|
13771
|
+
p.then((e) => this.got.set(p, e), () => {});
|
|
13772
|
+
return p;
|
|
13700
13773
|
}
|
|
13701
13774
|
has(key) {
|
|
13702
13775
|
return this.m.has(key);
|
|
@@ -13706,9 +13779,17 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13706
13779
|
return this.m.get(key);
|
|
13707
13780
|
}
|
|
13708
13781
|
set(key, v) {
|
|
13782
|
+
const p = this.keep(Promise.resolve(v));
|
|
13783
|
+
if (!(v instanceof Promise)) this.got.set(p, v);
|
|
13784
|
+
const was = this.m.get(key);
|
|
13709
13785
|
this.m.delete(key);
|
|
13710
|
-
this.m.set(key,
|
|
13711
|
-
|
|
13786
|
+
this.m.set(key, p);
|
|
13787
|
+
if (was !== p) this.out(was);
|
|
13788
|
+
while (this.m.size > this.max) {
|
|
13789
|
+
const k = this.m.keys().next().value;
|
|
13790
|
+
this.out(this.m.get(k));
|
|
13791
|
+
this.m.delete(k);
|
|
13792
|
+
}
|
|
13712
13793
|
}
|
|
13713
13794
|
/** The embedding for `key`, encoding it with `make` on a miss. A failed encode is not kept. */
|
|
13714
13795
|
getOr(key, make) {
|
|
@@ -13720,7 +13801,7 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13720
13801
|
return v;
|
|
13721
13802
|
}
|
|
13722
13803
|
this.misses++;
|
|
13723
|
-
const p = make();
|
|
13804
|
+
const p = this.keep(make());
|
|
13724
13805
|
this.set(key, p);
|
|
13725
13806
|
p.catch(() => {
|
|
13726
13807
|
if (this.m.get(key) === p) this.m.delete(key);
|
|
@@ -13728,9 +13809,11 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13728
13809
|
return p;
|
|
13729
13810
|
}
|
|
13730
13811
|
delete(key) {
|
|
13812
|
+
this.out(this.m.get(key));
|
|
13731
13813
|
return this.m.delete(key);
|
|
13732
13814
|
}
|
|
13733
13815
|
clear() {
|
|
13816
|
+
for (const p of this.m.values()) this.out(p);
|
|
13734
13817
|
this.m.clear();
|
|
13735
13818
|
}
|
|
13736
13819
|
get size() {
|
|
@@ -13821,7 +13904,11 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13821
13904
|
});
|
|
13822
13905
|
const start = inputs.input_ids.dims[inputs.input_ids.dims.length - 1];
|
|
13823
13906
|
const gen = ids.slice ? ids.slice(null, [start, null]) : ids;
|
|
13824
|
-
|
|
13907
|
+
try {
|
|
13908
|
+
return (processor.batch_decode(gen, { skip_special_tokens: true })[0] ?? "").trim();
|
|
13909
|
+
} finally {
|
|
13910
|
+
release(inputs, ids, gen);
|
|
13911
|
+
}
|
|
13825
13912
|
},
|
|
13826
13913
|
dispose: () => model.dispose?.()
|
|
13827
13914
|
};
|
|
@@ -13835,10 +13922,12 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13835
13922
|
async encode(image) {
|
|
13836
13923
|
const raw = await toRaw(ctx.tjs, image);
|
|
13837
13924
|
const inputs = await processor(raw);
|
|
13925
|
+
const emb = await model.get_image_embeddings(inputs);
|
|
13926
|
+
release(inputs);
|
|
13838
13927
|
return {
|
|
13839
13928
|
raw,
|
|
13840
13929
|
inputs,
|
|
13841
|
-
emb
|
|
13930
|
+
emb
|
|
13842
13931
|
};
|
|
13843
13932
|
},
|
|
13844
13933
|
async decode(e, prompt) {
|
|
@@ -13863,26 +13952,33 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13863
13952
|
"input_boxes"
|
|
13864
13953
|
]) if (p[k] !== void 0) feed[k] = p[k];
|
|
13865
13954
|
const out = await model(feed);
|
|
13866
|
-
const
|
|
13867
|
-
|
|
13868
|
-
|
|
13869
|
-
|
|
13870
|
-
const
|
|
13871
|
-
for (let
|
|
13872
|
-
|
|
13873
|
-
|
|
13874
|
-
|
|
13875
|
-
|
|
13876
|
-
|
|
13877
|
-
|
|
13955
|
+
const masks = await processor.post_process_masks(out.pred_masks, e.inputs.original_sizes, e.inputs.reshaped_input_sizes);
|
|
13956
|
+
try {
|
|
13957
|
+
const m = masks[0];
|
|
13958
|
+
const [n, h, w] = m.dims.slice(-3);
|
|
13959
|
+
const res = [];
|
|
13960
|
+
for (let k = 0; k < n; k++) {
|
|
13961
|
+
const data = new Uint8Array(w * h);
|
|
13962
|
+
for (let i = 0; i < w * h; i++) data[i] = m.data[k * w * h + i] ? 255 : 0;
|
|
13963
|
+
res.push({
|
|
13964
|
+
width: w,
|
|
13965
|
+
height: h,
|
|
13966
|
+
data,
|
|
13967
|
+
score: out.iou_scores.data[k] ?? 0
|
|
13968
|
+
});
|
|
13969
|
+
}
|
|
13970
|
+
return res;
|
|
13971
|
+
} finally {
|
|
13972
|
+
release(p, masks, ...Object.values(out));
|
|
13878
13973
|
}
|
|
13879
|
-
return res;
|
|
13880
13974
|
},
|
|
13975
|
+
release: (e) => release(e.emb),
|
|
13881
13976
|
dispose: () => model.dispose?.()
|
|
13882
13977
|
});
|
|
13883
13978
|
}
|
|
13884
13979
|
var cls;
|
|
13885
13980
|
var init_loaders = __esmMin((() => {
|
|
13981
|
+
init_release();
|
|
13886
13982
|
init_segment();
|
|
13887
13983
|
cls = (tjs, ...names) => {
|
|
13888
13984
|
for (const n of names) if (tjs[n]) return tjs[n];
|
|
@@ -13914,13 +14010,18 @@ var HackbuildAiViews = (function(exports) {
|
|
|
13914
14010
|
});
|
|
13915
14011
|
const start = inputs.input_ids.dims[inputs.input_ids.dims.length - 1];
|
|
13916
14012
|
const gen = ids.slice ? ids.slice(null, [start, null]) : ids;
|
|
13917
|
-
|
|
14013
|
+
try {
|
|
14014
|
+
return (processor.batch_decode(gen, { skip_special_tokens: true })[0] ?? "").trim();
|
|
14015
|
+
} finally {
|
|
14016
|
+
release(inputs, ids, gen);
|
|
14017
|
+
}
|
|
13918
14018
|
},
|
|
13919
14019
|
dispose: () => model.dispose?.()
|
|
13920
14020
|
};
|
|
13921
14021
|
}
|
|
13922
14022
|
var init_janus = __esmMin((() => {
|
|
13923
14023
|
init_loaders();
|
|
14024
|
+
init_release();
|
|
13924
14025
|
}));
|
|
13925
14026
|
// packages/vision/src/yolo.ts
|
|
13926
14027
|
/** The scale and padding that fit a `w` by `h` image into a `size` square (640 by default), centred. */
|
|
@@ -14070,14 +14171,19 @@ var HackbuildAiViews = (function(exports) {
|
|
|
14070
14171
|
size,
|
|
14071
14172
|
size
|
|
14072
14173
|
]) };
|
|
14073
|
-
const
|
|
14074
|
-
|
|
14075
|
-
|
|
14076
|
-
|
|
14077
|
-
|
|
14078
|
-
|
|
14079
|
-
|
|
14080
|
-
|
|
14174
|
+
const res = await m.session.run(feeds);
|
|
14175
|
+
try {
|
|
14176
|
+
const t = res[m.session.outputNames[0]];
|
|
14177
|
+
const d = {
|
|
14178
|
+
...opts.scoreThreshold !== void 0 ? { scoreThreshold: opts.scoreThreshold } : {},
|
|
14179
|
+
...opts.maxResults !== void 0 ? { max: opts.maxResults } : {},
|
|
14180
|
+
...o.labels ? { labels: o.labels } : {}
|
|
14181
|
+
};
|
|
14182
|
+
const out = t.data;
|
|
14183
|
+
return isEnd2End(t.dims) ? decodeYoloEnd2End(out, t.dims, lb, px, d) : decodeYolo(out, t.dims, lb, px, d);
|
|
14184
|
+
} finally {
|
|
14185
|
+
release(feeds, res);
|
|
14186
|
+
}
|
|
14081
14187
|
},
|
|
14082
14188
|
dispose: () => m.dispose()
|
|
14083
14189
|
};
|
|
@@ -14088,6 +14194,7 @@ var HackbuildAiViews = (function(exports) {
|
|
|
14088
14194
|
}
|
|
14089
14195
|
var COCO_LABELS, DETECT_SIZES;
|
|
14090
14196
|
var init_yolo = __esmMin((() => {
|
|
14197
|
+
init_release();
|
|
14091
14198
|
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(",");
|
|
14092
14199
|
DETECT_SIZES = {
|
|
14093
14200
|
n: "yolov10n",
|
package/dist/single/index.js
CHANGED
|
@@ -13665,6 +13665,37 @@ var init_records = __esmMin((() => {
|
|
|
13665
13665
|
}
|
|
13666
13666
|
];
|
|
13667
13667
|
}));
|
|
13668
|
+
// packages/vision/src/release.ts
|
|
13669
|
+
/**
|
|
13670
|
+
* Disposes every tensor among the values: a tensor itself, tensors in an
|
|
13671
|
+
* array, and tensors among the values of a plain record (a processor's or
|
|
13672
|
+
* a session's output), two levels deep. Anything else is left alone, and a
|
|
13673
|
+
* dispose that throws (already freed) is ignored.
|
|
13674
|
+
* @internal
|
|
13675
|
+
*/
|
|
13676
|
+
function release(...values) {
|
|
13677
|
+
const seen = /* @__PURE__ */ new Set();
|
|
13678
|
+
const walk = (v, depth) => {
|
|
13679
|
+
if (!v || typeof v !== "object" || seen.has(v) || depth > 2) return;
|
|
13680
|
+
seen.add(v);
|
|
13681
|
+
if (isTensor(v)) {
|
|
13682
|
+
try {
|
|
13683
|
+
v.dispose();
|
|
13684
|
+
} catch {}
|
|
13685
|
+
return;
|
|
13686
|
+
}
|
|
13687
|
+
if (Array.isArray(v)) {
|
|
13688
|
+
for (const x of v) walk(x, depth + 1);
|
|
13689
|
+
return;
|
|
13690
|
+
}
|
|
13691
|
+
if (Object.getPrototypeOf(v) === Object.prototype) for (const x of Object.values(v)) walk(x, depth + 1);
|
|
13692
|
+
};
|
|
13693
|
+
for (const v of values) walk(v, 0);
|
|
13694
|
+
}
|
|
13695
|
+
var isTensor;
|
|
13696
|
+
var init_release = __esmMin((() => {
|
|
13697
|
+
isTensor = (v) => !!v && typeof v === "object" && typeof v.dispose === "function" && Array.isArray(v.dims);
|
|
13698
|
+
}));
|
|
13668
13699
|
// packages/vision/src/segment.ts
|
|
13669
13700
|
/** True when the prompt has a point or a box. */
|
|
13670
13701
|
function hasPrompt(p) {
|
|
@@ -13672,29 +13703,71 @@ function hasPrompt(p) {
|
|
|
13672
13703
|
}
|
|
13673
13704
|
/** A segmenter from an encoder and a decoder, with the embedding cache on `cache`. Masks come best first, scores clamped to 0 to 1. */
|
|
13674
13705
|
function promptSegmenter(o) {
|
|
13675
|
-
const
|
|
13706
|
+
const reading = /* @__PURE__ */ new Map();
|
|
13707
|
+
const evicted = /* @__PURE__ */ new Set();
|
|
13708
|
+
const free = (e) => {
|
|
13709
|
+
if (!o.release) return;
|
|
13710
|
+
if (reading.get(e)) evicted.add(e);
|
|
13711
|
+
else o.release(e);
|
|
13712
|
+
};
|
|
13713
|
+
const own = o.cache ?? new EmbeddingCache(4, free);
|
|
13676
13714
|
const seg = {
|
|
13677
13715
|
cache: own,
|
|
13678
13716
|
async segment(image, prompt = {}, so = {}) {
|
|
13679
13717
|
if (!hasPrompt(prompt)) throw new Error("a prompt needs at least one point or a box");
|
|
13680
13718
|
const emb = await (so.cache ?? own).getOr(so.key ?? image, () => o.encode(image));
|
|
13681
|
-
|
|
13719
|
+
reading.set(emb, (reading.get(emb) ?? 0) + 1);
|
|
13720
|
+
let masks;
|
|
13721
|
+
try {
|
|
13722
|
+
masks = await o.decode(emb, prompt, image);
|
|
13723
|
+
} finally {
|
|
13724
|
+
const n = (reading.get(emb) ?? 1) - 1;
|
|
13725
|
+
if (n > 0) reading.set(emb, n);
|
|
13726
|
+
else {
|
|
13727
|
+
reading.delete(emb);
|
|
13728
|
+
if (evicted.delete(emb) && !own.holds(emb)) o.release?.(emb);
|
|
13729
|
+
}
|
|
13730
|
+
}
|
|
13682
13731
|
for (const m of masks) if (m.score !== void 0) m.score = Math.min(1, Math.max(0, m.score));
|
|
13683
13732
|
return masks.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));
|
|
13684
13733
|
}
|
|
13685
13734
|
};
|
|
13686
|
-
|
|
13735
|
+
seg.dispose = () => {
|
|
13736
|
+
if (!o.cache) own.clear();
|
|
13737
|
+
return o.dispose?.();
|
|
13738
|
+
};
|
|
13687
13739
|
return seg;
|
|
13688
13740
|
}
|
|
13689
13741
|
var EmbeddingCache;
|
|
13690
13742
|
var init_segment = __esmMin((() => {
|
|
13691
13743
|
EmbeddingCache = class {
|
|
13692
13744
|
max;
|
|
13745
|
+
onEvict;
|
|
13693
13746
|
m = /* @__PURE__ */ new Map();
|
|
13747
|
+
got = /* @__PURE__ */ new WeakMap();
|
|
13694
13748
|
hits = 0;
|
|
13695
13749
|
misses = 0;
|
|
13696
|
-
|
|
13750
|
+
/** `onEvict` gets each embedding that leaves the cache (pushed out, deleted, cleared or replaced), to free it. */
|
|
13751
|
+
constructor(max = 4, onEvict) {
|
|
13697
13752
|
this.max = max;
|
|
13753
|
+
this.onEvict = onEvict;
|
|
13754
|
+
}
|
|
13755
|
+
out(p) {
|
|
13756
|
+
if (p && this.onEvict) {
|
|
13757
|
+
const f = this.onEvict;
|
|
13758
|
+
p.then((e) => {
|
|
13759
|
+
if (!this.holds(e)) f(e);
|
|
13760
|
+
}, () => {});
|
|
13761
|
+
}
|
|
13762
|
+
}
|
|
13763
|
+
/** True when the embedding is still in the cache under some key. */
|
|
13764
|
+
holds(e) {
|
|
13765
|
+
for (const q of this.m.values()) if (this.got.get(q) === e) return true;
|
|
13766
|
+
return false;
|
|
13767
|
+
}
|
|
13768
|
+
keep(p) {
|
|
13769
|
+
p.then((e) => this.got.set(p, e), () => {});
|
|
13770
|
+
return p;
|
|
13698
13771
|
}
|
|
13699
13772
|
has(key) {
|
|
13700
13773
|
return this.m.has(key);
|
|
@@ -13704,9 +13777,17 @@ var init_segment = __esmMin((() => {
|
|
|
13704
13777
|
return this.m.get(key);
|
|
13705
13778
|
}
|
|
13706
13779
|
set(key, v) {
|
|
13780
|
+
const p = this.keep(Promise.resolve(v));
|
|
13781
|
+
if (!(v instanceof Promise)) this.got.set(p, v);
|
|
13782
|
+
const was = this.m.get(key);
|
|
13707
13783
|
this.m.delete(key);
|
|
13708
|
-
this.m.set(key,
|
|
13709
|
-
|
|
13784
|
+
this.m.set(key, p);
|
|
13785
|
+
if (was !== p) this.out(was);
|
|
13786
|
+
while (this.m.size > this.max) {
|
|
13787
|
+
const k = this.m.keys().next().value;
|
|
13788
|
+
this.out(this.m.get(k));
|
|
13789
|
+
this.m.delete(k);
|
|
13790
|
+
}
|
|
13710
13791
|
}
|
|
13711
13792
|
/** The embedding for `key`, encoding it with `make` on a miss. A failed encode is not kept. */
|
|
13712
13793
|
getOr(key, make) {
|
|
@@ -13718,7 +13799,7 @@ var init_segment = __esmMin((() => {
|
|
|
13718
13799
|
return v;
|
|
13719
13800
|
}
|
|
13720
13801
|
this.misses++;
|
|
13721
|
-
const p = make();
|
|
13802
|
+
const p = this.keep(make());
|
|
13722
13803
|
this.set(key, p);
|
|
13723
13804
|
p.catch(() => {
|
|
13724
13805
|
if (this.m.get(key) === p) this.m.delete(key);
|
|
@@ -13726,9 +13807,11 @@ var init_segment = __esmMin((() => {
|
|
|
13726
13807
|
return p;
|
|
13727
13808
|
}
|
|
13728
13809
|
delete(key) {
|
|
13810
|
+
this.out(this.m.get(key));
|
|
13729
13811
|
return this.m.delete(key);
|
|
13730
13812
|
}
|
|
13731
13813
|
clear() {
|
|
13814
|
+
for (const p of this.m.values()) this.out(p);
|
|
13732
13815
|
this.m.clear();
|
|
13733
13816
|
}
|
|
13734
13817
|
get size() {
|
|
@@ -13819,7 +13902,11 @@ async function vlmLoader(ctx) {
|
|
|
13819
13902
|
});
|
|
13820
13903
|
const start = inputs.input_ids.dims[inputs.input_ids.dims.length - 1];
|
|
13821
13904
|
const gen = ids.slice ? ids.slice(null, [start, null]) : ids;
|
|
13822
|
-
|
|
13905
|
+
try {
|
|
13906
|
+
return (processor.batch_decode(gen, { skip_special_tokens: true })[0] ?? "").trim();
|
|
13907
|
+
} finally {
|
|
13908
|
+
release(inputs, ids, gen);
|
|
13909
|
+
}
|
|
13823
13910
|
},
|
|
13824
13911
|
dispose: () => model.dispose?.()
|
|
13825
13912
|
};
|
|
@@ -13833,10 +13920,12 @@ async function samLoader(ctx) {
|
|
|
13833
13920
|
async encode(image) {
|
|
13834
13921
|
const raw = await toRaw(ctx.tjs, image);
|
|
13835
13922
|
const inputs = await processor(raw);
|
|
13923
|
+
const emb = await model.get_image_embeddings(inputs);
|
|
13924
|
+
release(inputs);
|
|
13836
13925
|
return {
|
|
13837
13926
|
raw,
|
|
13838
13927
|
inputs,
|
|
13839
|
-
emb
|
|
13928
|
+
emb
|
|
13840
13929
|
};
|
|
13841
13930
|
},
|
|
13842
13931
|
async decode(e, prompt) {
|
|
@@ -13861,26 +13950,33 @@ async function samLoader(ctx) {
|
|
|
13861
13950
|
"input_boxes"
|
|
13862
13951
|
]) if (p[k] !== void 0) feed[k] = p[k];
|
|
13863
13952
|
const out = await model(feed);
|
|
13864
|
-
const
|
|
13865
|
-
|
|
13866
|
-
|
|
13867
|
-
|
|
13868
|
-
const
|
|
13869
|
-
for (let
|
|
13870
|
-
|
|
13871
|
-
|
|
13872
|
-
|
|
13873
|
-
|
|
13874
|
-
|
|
13875
|
-
|
|
13953
|
+
const masks = await processor.post_process_masks(out.pred_masks, e.inputs.original_sizes, e.inputs.reshaped_input_sizes);
|
|
13954
|
+
try {
|
|
13955
|
+
const m = masks[0];
|
|
13956
|
+
const [n, h, w] = m.dims.slice(-3);
|
|
13957
|
+
const res = [];
|
|
13958
|
+
for (let k = 0; k < n; k++) {
|
|
13959
|
+
const data = new Uint8Array(w * h);
|
|
13960
|
+
for (let i = 0; i < w * h; i++) data[i] = m.data[k * w * h + i] ? 255 : 0;
|
|
13961
|
+
res.push({
|
|
13962
|
+
width: w,
|
|
13963
|
+
height: h,
|
|
13964
|
+
data,
|
|
13965
|
+
score: out.iou_scores.data[k] ?? 0
|
|
13966
|
+
});
|
|
13967
|
+
}
|
|
13968
|
+
return res;
|
|
13969
|
+
} finally {
|
|
13970
|
+
release(p, masks, ...Object.values(out));
|
|
13876
13971
|
}
|
|
13877
|
-
return res;
|
|
13878
13972
|
},
|
|
13973
|
+
release: (e) => release(e.emb),
|
|
13879
13974
|
dispose: () => model.dispose?.()
|
|
13880
13975
|
});
|
|
13881
13976
|
}
|
|
13882
13977
|
var cls;
|
|
13883
13978
|
var init_loaders = __esmMin((() => {
|
|
13979
|
+
init_release();
|
|
13884
13980
|
init_segment();
|
|
13885
13981
|
cls = (tjs, ...names) => {
|
|
13886
13982
|
for (const n of names) if (tjs[n]) return tjs[n];
|
|
@@ -13912,13 +14008,18 @@ async function janusLoader(ctx) {
|
|
|
13912
14008
|
});
|
|
13913
14009
|
const start = inputs.input_ids.dims[inputs.input_ids.dims.length - 1];
|
|
13914
14010
|
const gen = ids.slice ? ids.slice(null, [start, null]) : ids;
|
|
13915
|
-
|
|
14011
|
+
try {
|
|
14012
|
+
return (processor.batch_decode(gen, { skip_special_tokens: true })[0] ?? "").trim();
|
|
14013
|
+
} finally {
|
|
14014
|
+
release(inputs, ids, gen);
|
|
14015
|
+
}
|
|
13916
14016
|
},
|
|
13917
14017
|
dispose: () => model.dispose?.()
|
|
13918
14018
|
};
|
|
13919
14019
|
}
|
|
13920
14020
|
var init_janus = __esmMin((() => {
|
|
13921
14021
|
init_loaders();
|
|
14022
|
+
init_release();
|
|
13922
14023
|
}));
|
|
13923
14024
|
// packages/vision/src/yolo.ts
|
|
13924
14025
|
/** The scale and padding that fit a `w` by `h` image into a `size` square (640 by default), centred. */
|
|
@@ -14068,14 +14169,19 @@ function yoloDetector(m, o = {}) {
|
|
|
14068
14169
|
size,
|
|
14069
14170
|
size
|
|
14070
14171
|
]) };
|
|
14071
|
-
const
|
|
14072
|
-
|
|
14073
|
-
|
|
14074
|
-
|
|
14075
|
-
|
|
14076
|
-
|
|
14077
|
-
|
|
14078
|
-
|
|
14172
|
+
const res = await m.session.run(feeds);
|
|
14173
|
+
try {
|
|
14174
|
+
const t = res[m.session.outputNames[0]];
|
|
14175
|
+
const d = {
|
|
14176
|
+
...opts.scoreThreshold !== void 0 ? { scoreThreshold: opts.scoreThreshold } : {},
|
|
14177
|
+
...opts.maxResults !== void 0 ? { max: opts.maxResults } : {},
|
|
14178
|
+
...o.labels ? { labels: o.labels } : {}
|
|
14179
|
+
};
|
|
14180
|
+
const out = t.data;
|
|
14181
|
+
return isEnd2End(t.dims) ? decodeYoloEnd2End(out, t.dims, lb, px, d) : decodeYolo(out, t.dims, lb, px, d);
|
|
14182
|
+
} finally {
|
|
14183
|
+
release(feeds, res);
|
|
14184
|
+
}
|
|
14079
14185
|
},
|
|
14080
14186
|
dispose: () => m.dispose()
|
|
14081
14187
|
};
|
|
@@ -14086,6 +14192,7 @@ function detectCadenceMs(lastLatencyMs) {
|
|
|
14086
14192
|
}
|
|
14087
14193
|
var COCO_LABELS, DETECT_SIZES;
|
|
14088
14194
|
var init_yolo = __esmMin((() => {
|
|
14195
|
+
init_release();
|
|
14089
14196
|
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(",");
|
|
14090
14197
|
DETECT_SIZES = {
|
|
14091
14198
|
n: "yolov10n",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/ai-views",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "Views that bind the browser AI packages to hackbuild-ui: a model picker per job, a chat pane and composer, a drop first transcriber, a mask studio, a frame view and a camera feed. Each ships as a Vue component and a custom element.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@hackbuild/models": "0.2.5",
|
|
52
52
|
"@hackbuild/speak": "0.1.7",
|
|
53
53
|
"@hackbuild/transcribe": "0.1.7",
|
|
54
|
-
"@hackbuild/vision": "0.3.
|
|
54
|
+
"@hackbuild/vision": "0.3.2",
|
|
55
55
|
"@hackbuild/weights": "0.1.4"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|