@tradalab/mediakit 2.6.0 → 2.7.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 +212 -2
- package/dist/{chunk-RWUGOTNJ.js → chunk-4YCBTZRB.js} +4 -1
- package/dist/chunk-4YCBTZRB.js.map +1 -0
- package/dist/detect-worker.js +3696 -670
- package/dist/detect-worker.js.map +1 -1
- package/dist/face_cover.onnx +0 -0
- package/dist/index.d.ts +61 -3
- package/dist/index.js +578 -54
- package/dist/index.js.map +1 -1
- package/dist/mediakit.wasm +0 -0
- package/dist/recipe.d.ts +2 -0
- package/dist/recipe.js +1 -1
- package/dist/worker.js +579 -53
- package/dist/worker.js.map +1 -1
- package/package.json +17 -12
- package/dist/chunk-RWUGOTNJ.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
normalizeEffects,
|
|
13
13
|
touchUp,
|
|
14
14
|
touchUpAmount
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-4YCBTZRB.js";
|
|
16
16
|
import {
|
|
17
17
|
BLEND_MODES,
|
|
18
18
|
TRANSITIONS,
|
|
@@ -89,6 +89,22 @@ async function preloadDetectors(parts = { person: true, classes: true, face: tru
|
|
|
89
89
|
);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
// src/mp-clock.ts
|
|
93
|
+
var SOURCE_SWITCH_GAP_MS = 33;
|
|
94
|
+
function monotonicClock() {
|
|
95
|
+
let offset = 0;
|
|
96
|
+
let last = -Infinity;
|
|
97
|
+
return (timestampMs) => {
|
|
98
|
+
let t = timestampMs + offset;
|
|
99
|
+
if (t <= last) {
|
|
100
|
+
offset += last + SOURCE_SWITCH_GAP_MS - t;
|
|
101
|
+
t = last + SOURCE_SWITCH_GAP_MS;
|
|
102
|
+
}
|
|
103
|
+
last = t;
|
|
104
|
+
return t;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
92
108
|
// src/segmenter.ts
|
|
93
109
|
async function createSegmenter(canvas, model = "person") {
|
|
94
110
|
const fileset2 = await visionFileset();
|
|
@@ -108,6 +124,7 @@ async function createSegmenter(canvas, model = "person") {
|
|
|
108
124
|
outputCategoryMask: true,
|
|
109
125
|
outputConfidenceMasks: false
|
|
110
126
|
});
|
|
127
|
+
const clock = monotonicClock();
|
|
111
128
|
let pending = null;
|
|
112
129
|
const release = () => {
|
|
113
130
|
pending?.close();
|
|
@@ -117,7 +134,7 @@ async function createSegmenter(canvas, model = "person") {
|
|
|
117
134
|
segment(frame, timestampMs) {
|
|
118
135
|
release();
|
|
119
136
|
let texture = null;
|
|
120
|
-
segmenter.segmentForVideo(frame, timestampMs, (result) => {
|
|
137
|
+
segmenter.segmentForVideo(frame, clock(timestampMs), (result) => {
|
|
121
138
|
const mask = result.categoryMask;
|
|
122
139
|
if (mask) texture = mask.getAsWebGLTexture();
|
|
123
140
|
pending = result;
|
|
@@ -135,12 +152,26 @@ async function createSegmenter(canvas, model = "person") {
|
|
|
135
152
|
// src/face.ts
|
|
136
153
|
import { FaceLandmarker } from "@mediapipe/tasks-vision";
|
|
137
154
|
var FACE_FLOATS = 478 * 3;
|
|
138
|
-
|
|
155
|
+
var COVER_SIZE = 96;
|
|
156
|
+
function faceBox(lm, at) {
|
|
157
|
+
let x0 = 1;
|
|
158
|
+
let y0 = 1;
|
|
159
|
+
let x1 = 0;
|
|
160
|
+
let y1 = 0;
|
|
161
|
+
for (let i = at; i < at + FACE_FLOATS; i += 3) {
|
|
162
|
+
x0 = Math.min(x0, lm[i]);
|
|
163
|
+
x1 = Math.max(x1, lm[i]);
|
|
164
|
+
y0 = Math.min(y0, lm[i + 1]);
|
|
165
|
+
y1 = Math.max(y1, lm[i + 1]);
|
|
166
|
+
}
|
|
167
|
+
return [x0, y0, x1 - x0, y1 - y0];
|
|
168
|
+
}
|
|
169
|
+
async function createFaceDetector(canvas, maxFaces, delegate = "GPU", mode = "VIDEO") {
|
|
139
170
|
const fileset2 = await visionFileset();
|
|
140
171
|
const landmarker = await FaceLandmarker.createFromOptions(fileset2, {
|
|
141
172
|
baseOptions: { modelAssetPath: MODEL_URL.face, delegate },
|
|
142
173
|
canvas,
|
|
143
|
-
runningMode:
|
|
174
|
+
runningMode: mode,
|
|
144
175
|
// As many as the core tracks: one landmark pass returns them all, so the cost of a second
|
|
145
176
|
// face is the copy below and its own shader passes, not a second inference.
|
|
146
177
|
numFaces: maxFaces,
|
|
@@ -148,9 +179,10 @@ async function createFaceDetector(canvas, maxFaces, delegate = "GPU") {
|
|
|
148
179
|
outputFaceBlendshapes: false,
|
|
149
180
|
outputFacialTransformationMatrixes: false
|
|
150
181
|
});
|
|
182
|
+
const clock = monotonicClock();
|
|
151
183
|
return {
|
|
152
184
|
detect(frame, timestampMs, out) {
|
|
153
|
-
const res = landmarker.detectForVideo(frame, timestampMs);
|
|
185
|
+
const res = mode === "VIDEO" ? landmarker.detectForVideo(frame, clock(timestampMs)) : landmarker.detect(frame);
|
|
154
186
|
let n = 0;
|
|
155
187
|
for (const lm of res.faceLandmarks ?? []) {
|
|
156
188
|
if (lm.length * 3 !== FACE_FLOATS || n + FACE_FLOATS > out.length) break;
|
|
@@ -195,13 +227,13 @@ var DetectWorker = class {
|
|
|
195
227
|
return this.worker !== null && this.up.has(kind) && !this.busy.has(kind);
|
|
196
228
|
}
|
|
197
229
|
/** Transfers the frame. False when that kind is busy or not up; the frame is then closed. */
|
|
198
|
-
request(kind, frame, tsUs) {
|
|
230
|
+
request(kind, frame, tsUs, cover = false, retry = null) {
|
|
199
231
|
if (!this.accepts(kind)) {
|
|
200
232
|
frame.close();
|
|
201
233
|
return false;
|
|
202
234
|
}
|
|
203
235
|
this.busy.add(kind);
|
|
204
|
-
this.send({ t: kind, frame, tsUs }, [frame]);
|
|
236
|
+
this.send(kind === "face" ? { t: kind, frame, tsUs, cover, retry } : { t: kind, frame, tsUs }, [frame]);
|
|
205
237
|
return true;
|
|
206
238
|
}
|
|
207
239
|
isUp(kind) {
|
|
@@ -237,7 +269,7 @@ var DetectWorker = class {
|
|
|
237
269
|
break;
|
|
238
270
|
case "face":
|
|
239
271
|
this.busy.delete("face");
|
|
240
|
-
if (this.up.has("face")) this.on.face(m.tsUs, m.n, m.lm, m.ms);
|
|
272
|
+
if (this.up.has("face")) this.on.face(m.tsUs, m.n, m.lm, m.ms, m.cover, m.rects);
|
|
241
273
|
break;
|
|
242
274
|
case "mask":
|
|
243
275
|
this.busy.delete("mask");
|
|
@@ -410,6 +442,75 @@ var GpuLostError = class extends Error {
|
|
|
410
442
|
detail;
|
|
411
443
|
};
|
|
412
444
|
|
|
445
|
+
// src/mask-merge.ts
|
|
446
|
+
var CLASS_BACKGROUND = 0;
|
|
447
|
+
var CLASS_BODY_SKIN = 2;
|
|
448
|
+
var CLASS_CLOTHES = 4;
|
|
449
|
+
var CLASS_OTHERS = 5;
|
|
450
|
+
var CLASS_STALE_OCCLUDER = 6;
|
|
451
|
+
var THUMB_W = 64;
|
|
452
|
+
var THUMB_H = 36;
|
|
453
|
+
var MOVED_LUMA = 12;
|
|
454
|
+
function lumaThumb(rgba) {
|
|
455
|
+
const y = new Uint8Array(THUMB_W * THUMB_H);
|
|
456
|
+
for (let i = 0; i < y.length; i++) y[i] = rgba[i * 4] * 77 + rgba[i * 4 + 1] * 150 + rgba[i * 4 + 2] * 29 >> 8;
|
|
457
|
+
return y;
|
|
458
|
+
}
|
|
459
|
+
function movedBetween(a, b) {
|
|
460
|
+
const w = THUMB_W;
|
|
461
|
+
const h = THUMB_H;
|
|
462
|
+
const hit = new Uint8Array(w * h);
|
|
463
|
+
for (let i = 0; i < hit.length; i++) hit[i] = Math.abs(a[i] - b[i]) > MOVED_LUMA ? 1 : 0;
|
|
464
|
+
const data = new Uint8Array(w * h);
|
|
465
|
+
for (let y = 0; y < h; y++) {
|
|
466
|
+
for (let x = 0; x < w; x++) {
|
|
467
|
+
let v = 0;
|
|
468
|
+
for (let dy = -1; dy <= 1 && !v; dy++) {
|
|
469
|
+
const yy = y + dy;
|
|
470
|
+
if (yy < 0 || yy >= h) continue;
|
|
471
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
472
|
+
const xx = x + dx;
|
|
473
|
+
if (xx >= 0 && xx < w && hit[yy * w + xx]) {
|
|
474
|
+
v = 1;
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
data[y * w + x] = v;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { width: w, height: h, data };
|
|
483
|
+
}
|
|
484
|
+
function mergeMasks(person, classes, out, moved = null) {
|
|
485
|
+
const { width: w, height: h } = person;
|
|
486
|
+
const merged = out && out.length === w * h ? out : new Uint8Array(w * h);
|
|
487
|
+
const cw = classes.width;
|
|
488
|
+
const ch = classes.height;
|
|
489
|
+
const sx = cw / w;
|
|
490
|
+
const sy = ch / h;
|
|
491
|
+
const mw = moved?.width ?? 0;
|
|
492
|
+
const mh = moved?.height ?? 0;
|
|
493
|
+
for (let y = 0; y < h; y++) {
|
|
494
|
+
const crow = Math.min(ch - 1, Math.floor((y + 0.5) * sy)) * cw;
|
|
495
|
+
const mrow = moved ? Math.min(mh - 1, Math.floor((y + 0.5) * mh / h)) * mw : 0;
|
|
496
|
+
const prow = y * w;
|
|
497
|
+
for (let x = 0; x < w; x++) {
|
|
498
|
+
if (person.data[prow + x] !== 0) {
|
|
499
|
+
merged[prow + x] = CLASS_BACKGROUND;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
const c = classes.data[crow + Math.min(cw - 1, Math.floor((x + 0.5) * sx))];
|
|
503
|
+
if (c === CLASS_BACKGROUND) {
|
|
504
|
+
merged[prow + x] = CLASS_CLOTHES;
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
const stale = moved !== null && (c === CLASS_BODY_SKIN || c === CLASS_OTHERS) && moved.data[mrow + Math.min(mw - 1, Math.floor((x + 0.5) * mw / w))] !== 0;
|
|
508
|
+
merged[prow + x] = stale ? CLASS_STALE_OCCLUDER : c;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return merged;
|
|
512
|
+
}
|
|
513
|
+
|
|
413
514
|
// src/engine.ts
|
|
414
515
|
var ZERO_STATS = {
|
|
415
516
|
totalMs: 0,
|
|
@@ -433,9 +534,30 @@ var ZERO_STATS = {
|
|
|
433
534
|
maskAgeMs: 0,
|
|
434
535
|
effectError: ""
|
|
435
536
|
};
|
|
436
|
-
var MAX_HOLD =
|
|
537
|
+
var MAX_HOLD = 3;
|
|
538
|
+
var COVER_FRAME_W = 256;
|
|
539
|
+
var COVER_HOLD = 0.6;
|
|
540
|
+
var COVER_MISSES = 2;
|
|
541
|
+
var FOLLOW_SIDE = 384;
|
|
542
|
+
var FOLLOW_MARGIN = 2.6;
|
|
543
|
+
var FOLLOW_RETRY = 2;
|
|
544
|
+
var FOLLOW_MIN_SIDE = 64;
|
|
545
|
+
var FOLLOW_SHIFT = 0.06;
|
|
546
|
+
var FOLLOW_RESIZE = 0.12;
|
|
547
|
+
var FOLLOW_STALE_US = 5e5;
|
|
548
|
+
var FOLLOW_KEPT_US = 1e6;
|
|
549
|
+
var SCAN_SHARE = 0.5;
|
|
550
|
+
var SCAN_STEP = 0.75;
|
|
551
|
+
var FOLLOW_REFRESH = 30;
|
|
552
|
+
var FOLLOW_WIDEN = 2;
|
|
553
|
+
var FOLLOW_GROW = 3;
|
|
554
|
+
var SAME_FACE = 0.3;
|
|
555
|
+
var FACE_RESULTS_KEPT = 8;
|
|
556
|
+
var THUMBS_KEPT = 12;
|
|
437
557
|
var LINK_LOSS_WINDOW_MS = 3e3;
|
|
438
|
-
var
|
|
558
|
+
var FACE_JUMP = 0.15;
|
|
559
|
+
var CATCH_UP_EVERY_US = 1e6;
|
|
560
|
+
var MASK_RESULTS_KEPT = 8;
|
|
439
561
|
var DETECT_DUTY = 0.6;
|
|
440
562
|
function gpuName(gl) {
|
|
441
563
|
const info = gl.getExtension("WEBGL_debug_renderer_info");
|
|
@@ -448,6 +570,18 @@ function sourceSize(source) {
|
|
|
448
570
|
const s = source;
|
|
449
571
|
return [s.width, s.height];
|
|
450
572
|
}
|
|
573
|
+
function tileGrid(sw, sh) {
|
|
574
|
+
const side = Math.max(FOLLOW_MIN_SIDE, Math.round(Math.min(sw, sh) * SCAN_SHARE));
|
|
575
|
+
const step = side * SCAN_STEP;
|
|
576
|
+
return { side, step, nx: Math.max(1, Math.ceil((sw - side) / step) + 1), ny: Math.max(1, Math.ceil((sh - side) / step) + 1) };
|
|
577
|
+
}
|
|
578
|
+
function overlap(a, b) {
|
|
579
|
+
const w = Math.min(a[0] + a[2], b[0] + b[2]) - Math.max(a[0], b[0]);
|
|
580
|
+
const h = Math.min(a[1] + a[3], b[1] + b[3]) - Math.max(a[1], b[1]);
|
|
581
|
+
if (w <= 0 || h <= 0) return 0;
|
|
582
|
+
const i = w * h;
|
|
583
|
+
return i / (a[2] * a[3] + b[2] * b[3] - i);
|
|
584
|
+
}
|
|
451
585
|
var FACE_COLOR_SKIPPED = "face colour shaders skipped: linking one lost the WebGL context on this GPU before";
|
|
452
586
|
var Engine = class _Engine {
|
|
453
587
|
canvas;
|
|
@@ -501,13 +635,14 @@ var Engine = class _Engine {
|
|
|
501
635
|
classRemote = false;
|
|
502
636
|
classMaskTex = null;
|
|
503
637
|
classMaskFresh = false;
|
|
504
|
-
classMaskTs =
|
|
638
|
+
classMaskTs = -1;
|
|
505
639
|
syncFaces = false;
|
|
506
640
|
renderedTs = 0;
|
|
507
641
|
queue = [];
|
|
508
642
|
spare = [];
|
|
509
|
-
|
|
510
|
-
|
|
643
|
+
faceExtraUp = false;
|
|
644
|
+
faceExtraTried = false;
|
|
645
|
+
lerpBuf = null;
|
|
511
646
|
gapUs = 33333;
|
|
512
647
|
prevTs = -1;
|
|
513
648
|
sent = /* @__PURE__ */ new Map();
|
|
@@ -515,6 +650,35 @@ var Engine = class _Engine {
|
|
|
515
650
|
faceLatencyMs = 0;
|
|
516
651
|
maskLatencyMs = /* @__PURE__ */ new Map();
|
|
517
652
|
faceTs = -1;
|
|
653
|
+
faceResults = [];
|
|
654
|
+
coverCanvas = null;
|
|
655
|
+
coverCell = null;
|
|
656
|
+
coverOn = false;
|
|
657
|
+
faceMisses = 0;
|
|
658
|
+
coverHidden = false;
|
|
659
|
+
srcW = 0;
|
|
660
|
+
srcH = 0;
|
|
661
|
+
coverLast = null;
|
|
662
|
+
coverShown = null;
|
|
663
|
+
coverImg = null;
|
|
664
|
+
faceAsked = /* @__PURE__ */ new Set();
|
|
665
|
+
faceCrops = /* @__PURE__ */ new Map();
|
|
666
|
+
followBox = null;
|
|
667
|
+
followTs = -Infinity;
|
|
668
|
+
followCrop = null;
|
|
669
|
+
followCanvas = null;
|
|
670
|
+
followRetry = null;
|
|
671
|
+
faceSeen = false;
|
|
672
|
+
followFloats = 0;
|
|
673
|
+
followAll = false;
|
|
674
|
+
followTurn = 0;
|
|
675
|
+
followMiss = 0;
|
|
676
|
+
followGrow = 0;
|
|
677
|
+
boxFloats = 0;
|
|
678
|
+
scanFrom = 0;
|
|
679
|
+
scanTurn = 0;
|
|
680
|
+
probes = /* @__PURE__ */ new Set();
|
|
681
|
+
maskResults = [];
|
|
518
682
|
maskUsedTs = -1;
|
|
519
683
|
lastHeld = null;
|
|
520
684
|
swallowed = false;
|
|
@@ -524,13 +688,23 @@ var Engine = class _Engine {
|
|
|
524
688
|
personLoading = null;
|
|
525
689
|
personMaskTex = null;
|
|
526
690
|
personMaskFresh = false;
|
|
527
|
-
personMaskTs =
|
|
691
|
+
personMaskTs = -1;
|
|
528
692
|
personWorkerMs = 0;
|
|
693
|
+
personBytes = null;
|
|
694
|
+
classBytes = null;
|
|
695
|
+
mergedBytes = null;
|
|
696
|
+
mergedTex = null;
|
|
697
|
+
mergedFresh = false;
|
|
698
|
+
mergedTs = 0;
|
|
529
699
|
detectCanvas = null;
|
|
530
700
|
detectCanvasTs = -1;
|
|
701
|
+
thumbCanvas = null;
|
|
702
|
+
thumbs = /* @__PURE__ */ new Map();
|
|
531
703
|
// A kind whose worker failed once stays in-thread.
|
|
532
704
|
inThread = /* @__PURE__ */ new Set();
|
|
533
705
|
faceLoading = null;
|
|
706
|
+
faceCount = 1;
|
|
707
|
+
caughtUpTs = -Infinity;
|
|
534
708
|
maxFaces = 3;
|
|
535
709
|
lmBuf = null;
|
|
536
710
|
filter = { kind: "none" };
|
|
@@ -743,6 +917,7 @@ var Engine = class _Engine {
|
|
|
743
917
|
this.faceLoading = this.remote("face", (d) => d.initFace(n)).then(() => {
|
|
744
918
|
this.faceRemote = true;
|
|
745
919
|
this.lmBuf = buf;
|
|
920
|
+
this.faceCount = n;
|
|
746
921
|
}).catch((e) => {
|
|
747
922
|
if (e instanceof LoadTimeout) throw e;
|
|
748
923
|
if (!this.inThread.has("face")) console.warn("mediakit: no detect worker for faces, detecting in this thread", e);
|
|
@@ -765,7 +940,7 @@ var Engine = class _Engine {
|
|
|
765
940
|
}
|
|
766
941
|
/** The detect worker for `kind`, made on first use; rejects when that kind must stay in-thread. */
|
|
767
942
|
personRemoteWanted(filter, effects) {
|
|
768
|
-
return filter.kind === "none" && needsSegmentation(filter, effects)
|
|
943
|
+
return filter.kind === "none" && needsSegmentation(filter, effects);
|
|
769
944
|
}
|
|
770
945
|
ensurePersonRemote() {
|
|
771
946
|
if (this.personRemote || this.failed.has("person") || this.closed) return Promise.resolve();
|
|
@@ -803,14 +978,17 @@ var Engine = class _Engine {
|
|
|
803
978
|
}
|
|
804
979
|
detectHandlers(key) {
|
|
805
980
|
return {
|
|
806
|
-
face: (tsUs,
|
|
807
|
-
if (this.closed || !this.lmBuf) return;
|
|
808
|
-
this.lmBuf.set(lm);
|
|
809
|
-
const [lo, hi] = splitMicros(tsUs);
|
|
810
|
-
this.core.setFace(lo, hi, n);
|
|
981
|
+
face: (tsUs, found, lm, ms, foundCover, foundRects) => {
|
|
982
|
+
if (this.closed || !this.lmBuf || tsUs > this.prevTs) return;
|
|
811
983
|
this.faceWorkerMs = ms;
|
|
812
|
-
this.
|
|
813
|
-
const
|
|
984
|
+
this.uncrop(tsUs, found, lm, foundRects);
|
|
985
|
+
const { n, cover, rects } = this.dedupe(found, lm, foundCover, foundRects);
|
|
986
|
+
this.follow(tsUs, n, lm, this.probes.delete(tsUs));
|
|
987
|
+
if (this.syncing()) {
|
|
988
|
+
this.faceResults.push({ ts: tsUs, n, lm: lm.slice(), cover, rects });
|
|
989
|
+
if (this.faceResults.length > FACE_RESULTS_KEPT) this.faceResults.shift();
|
|
990
|
+
} else this.applyFace(tsUs, n, lm, cover, rects);
|
|
991
|
+
const took = this.took(key === "faceExtra" ? "faceExtra" : "face", tsUs);
|
|
814
992
|
if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
|
|
815
993
|
},
|
|
816
994
|
mask: (tsUs, width, height, data, ms) => {
|
|
@@ -820,24 +998,30 @@ var Engine = class _Engine {
|
|
|
820
998
|
const was = this.maskLatencyMs.get(key);
|
|
821
999
|
this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
|
|
822
1000
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
this.
|
|
826
|
-
this.personMaskTs = tsUs;
|
|
827
|
-
this.personWorkerMs = ms;
|
|
1001
|
+
const r = { person: key === "person", ts: tsUs, width, height, data, ms };
|
|
1002
|
+
if (!this.syncing()) {
|
|
1003
|
+
this.applyMask(r);
|
|
828
1004
|
return;
|
|
829
1005
|
}
|
|
830
|
-
this.
|
|
831
|
-
this.
|
|
832
|
-
this.maskWorkerMs = ms;
|
|
1006
|
+
this.maskResults.push(r);
|
|
1007
|
+
if (this.maskResults.length > MASK_RESULTS_KEPT) this.maskResults.shift();
|
|
833
1008
|
},
|
|
834
1009
|
failed: (k, reason) => {
|
|
1010
|
+
if (key === "faceExtra") {
|
|
1011
|
+
this.faceExtraUp = false;
|
|
1012
|
+
this.detects.get(key)?.close();
|
|
1013
|
+
this.detects.delete(key);
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
835
1016
|
console.warn(`mediakit: ${key} detection moves into this thread:`, reason);
|
|
836
1017
|
this.inThread.add(key);
|
|
837
1018
|
if (key === "person") {
|
|
838
1019
|
this.personRemote = false;
|
|
839
1020
|
} else if (k === "face") {
|
|
840
1021
|
this.faceRemote = false;
|
|
1022
|
+
if (this.coverOn) this.core.setFaceCover(null);
|
|
1023
|
+
this.coverOn = false;
|
|
1024
|
+
this.coverShown = null;
|
|
841
1025
|
void this.ensureFace();
|
|
842
1026
|
} else {
|
|
843
1027
|
this.classRemote = false;
|
|
@@ -846,6 +1030,24 @@ var Engine = class _Engine {
|
|
|
846
1030
|
}
|
|
847
1031
|
};
|
|
848
1032
|
}
|
|
1033
|
+
noteThumb(timestampUs) {
|
|
1034
|
+
if (this.thumbs.has(timestampUs) || !this.detectCanvas || this.detectCanvasTs !== timestampUs) return;
|
|
1035
|
+
this.thumbCanvas ??= new OffscreenCanvas(THUMB_W, THUMB_H);
|
|
1036
|
+
const ctx = this.thumbCanvas.getContext("2d", { willReadFrequently: true });
|
|
1037
|
+
ctx.drawImage(this.detectCanvas, 0, 0, THUMB_W, THUMB_H);
|
|
1038
|
+
this.thumbs.set(timestampUs, lumaThumb(ctx.getImageData(0, 0, THUMB_W, THUMB_H).data));
|
|
1039
|
+
if (this.thumbs.size > THUMBS_KEPT) this.thumbs.delete(this.thumbs.keys().next().value);
|
|
1040
|
+
}
|
|
1041
|
+
merge(tsUs) {
|
|
1042
|
+
if (!this.personBytes || !this.classBytes || !needsSkinClasses(this.filter, this.effects)) return;
|
|
1043
|
+
const then = this.thumbs.get(this.classMaskTs);
|
|
1044
|
+
const now2 = this.thumbs.get(this.personMaskTs);
|
|
1045
|
+
const moved = then && now2 && this.classMaskTs !== this.personMaskTs ? movedBetween(then, now2) : null;
|
|
1046
|
+
this.mergedBytes = mergeMasks(this.personBytes, this.classBytes, this.mergedBytes, moved);
|
|
1047
|
+
this.mergedTex = this.uploadMaskBytes(this.mergedTex, this.personBytes.width, this.personBytes.height, this.mergedBytes);
|
|
1048
|
+
this.mergedFresh = true;
|
|
1049
|
+
this.mergedTs = tsUs;
|
|
1050
|
+
}
|
|
849
1051
|
uploadClassMask(width, height, data) {
|
|
850
1052
|
this.classMaskTex = this.uploadMaskBytes(this.classMaskTex, width, height, data);
|
|
851
1053
|
this.classMaskFresh = true;
|
|
@@ -872,24 +1074,187 @@ var Engine = class _Engine {
|
|
|
872
1074
|
* Sends the detect worker a downscaled copy, never a clone: a camera frame held through a slow
|
|
873
1075
|
* detection drains Chrome's few capture buffers and throttles the camera itself.
|
|
874
1076
|
*/
|
|
875
|
-
requestRemote(
|
|
1077
|
+
requestRemote(want, source, timestampUs) {
|
|
1078
|
+
const lockstep = want === "face" && this.syncing();
|
|
1079
|
+
const kind = lockstep ? this.freeFaceWorker() : want;
|
|
1080
|
+
if (!kind) return;
|
|
876
1081
|
const detect = this.detects.get(kind);
|
|
877
|
-
if (!detect?.accepts(kind === "face" ? "face" : "mask")) return;
|
|
878
|
-
if (!this.dutyAllows(kind)) return;
|
|
1082
|
+
if (!detect?.accepts(kind === "face" || kind === "faceExtra" ? "face" : "mask")) return;
|
|
1083
|
+
if (!lockstep && !this.dutyAllows(kind)) return;
|
|
879
1084
|
let frame = null;
|
|
1085
|
+
let followed = null;
|
|
1086
|
+
let scanned = null;
|
|
880
1087
|
try {
|
|
881
1088
|
const copyable = source instanceof VideoFrame || source instanceof OffscreenCanvas;
|
|
882
|
-
|
|
1089
|
+
if (copyable) {
|
|
1090
|
+
this.srcW = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
1091
|
+
this.srcH = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
1092
|
+
}
|
|
1093
|
+
if (copyable && (kind === "face" || kind === "faceExtra")) {
|
|
1094
|
+
followed = this.followCopy(source, timestampUs);
|
|
1095
|
+
if (!followed) scanned = this.scanCopy(source, timestampUs);
|
|
1096
|
+
}
|
|
1097
|
+
frame = new VideoFrame(followed ?? scanned ?? (copyable ? this.detectCopy(source, timestampUs) : source), {
|
|
883
1098
|
timestamp: timestampUs
|
|
884
1099
|
});
|
|
885
1100
|
} catch {
|
|
886
1101
|
}
|
|
887
|
-
if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
|
|
1102
|
+
if (frame && detect.request(kind === "face" || kind === "faceExtra" ? "face" : "mask", frame, timestampUs, this.effects.occluders ?? false, followed ? this.followRetry : null)) {
|
|
888
1103
|
const now2 = performance.now();
|
|
889
1104
|
this.sent.set(kind, { ts: timestampUs, at: now2 });
|
|
890
1105
|
this.lastSentAt.set(kind, now2);
|
|
1106
|
+
if (lockstep) this.faceAsked.add(timestampUs);
|
|
1107
|
+
if (kind !== "face" && kind !== "faceExtra" && this.remoteMaskKind() === "merged") this.noteThumb(timestampUs);
|
|
1108
|
+
} else if (followed || scanned) this.faceCrops.delete(timestampUs);
|
|
1109
|
+
}
|
|
1110
|
+
followCopy(source, timestampUs) {
|
|
1111
|
+
const box = this.followBox;
|
|
1112
|
+
if (!box || timestampUs - this.followTs > FOLLOW_STALE_US) return null;
|
|
1113
|
+
const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
1114
|
+
const sh = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
1115
|
+
const want = Math.max(FOLLOW_MIN_SIDE, FOLLOW_MARGIN * (1 + this.followMiss) * Math.max(box[2] * sw, box[3] * sh));
|
|
1116
|
+
if (want > Math.min(sw, sh) && this.followFloats > FACE_FLOATS) return null;
|
|
1117
|
+
if (!this.followAll && this.followTurn++ % FOLLOW_REFRESH === FOLLOW_REFRESH - 1) {
|
|
1118
|
+
this.probes.add(timestampUs);
|
|
1119
|
+
return null;
|
|
1120
|
+
}
|
|
1121
|
+
let side = Math.round(Math.min(sw, sh, want));
|
|
1122
|
+
let x = Math.round(Math.min(sw - side, Math.max(0, (box[0] + box[2] / 2) * sw - side / 2)));
|
|
1123
|
+
let y = Math.round(Math.min(sh - side, Math.max(0, (box[1] + box[3] / 2) * sh - side / 2)));
|
|
1124
|
+
const kept = this.followCrop;
|
|
1125
|
+
if (kept && kept[0] + kept[2] <= sw && kept[1] + kept[2] <= sh && Math.abs(kept[2] - side) < FOLLOW_RESIZE * side && Math.abs(kept[0] - x) < FOLLOW_SHIFT * side && Math.abs(kept[1] - y) < FOLLOW_SHIFT * side) {
|
|
1126
|
+
;
|
|
1127
|
+
[x, y, side] = kept;
|
|
1128
|
+
} else this.followCrop = [x, y, side];
|
|
1129
|
+
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
1130
|
+
this.followCanvas.getContext("2d").drawImage(source, x, y, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
1131
|
+
const tight = Math.min(side, Math.max(FOLLOW_MIN_SIDE, FOLLOW_RETRY * Math.max(box[2] * sw, box[3] * sh)));
|
|
1132
|
+
this.followRetry = [((box[0] + box[2] / 2) * sw - x) / side, ((box[1] + box[3] / 2) * sh - y) / side, tight / side];
|
|
1133
|
+
this.faceCrops.set(timestampUs, [x / sw, y / sh, side / sw, side / sh]);
|
|
1134
|
+
return this.followCanvas;
|
|
1135
|
+
}
|
|
1136
|
+
scanCopy(source, timestampUs) {
|
|
1137
|
+
if (this.faceSeen) return null;
|
|
1138
|
+
const turn = this.scanTurn++;
|
|
1139
|
+
if (turn % 2 === 0) return null;
|
|
1140
|
+
const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
1141
|
+
const sh = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
1142
|
+
const { side, step, nx, ny } = tileGrid(sw, sh);
|
|
1143
|
+
const at = (this.scanFrom + (turn >> 1)) % (nx * ny);
|
|
1144
|
+
const x = Math.max(0, Math.min(sw - side, Math.round(at % nx * step)));
|
|
1145
|
+
const y = Math.max(0, Math.min(sh - side, Math.round(Math.floor(at / nx) * step)));
|
|
1146
|
+
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
1147
|
+
this.followCanvas.getContext("2d").drawImage(source, x, y, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
1148
|
+
this.faceCrops.set(timestampUs, [x / sw, y / sh, side / sw, side / sh]);
|
|
1149
|
+
this.probes.add(timestampUs);
|
|
1150
|
+
return this.followCanvas;
|
|
1151
|
+
}
|
|
1152
|
+
scanTile(cx, cy) {
|
|
1153
|
+
const sw = this.srcW || this.canvas.width;
|
|
1154
|
+
const sh = this.srcH || this.canvas.height;
|
|
1155
|
+
const { side, step, nx, ny } = tileGrid(sw, sh);
|
|
1156
|
+
const ix = Math.min(nx - 1, Math.max(0, Math.round((cx * sw - side / 2) / step)));
|
|
1157
|
+
const iy = Math.min(ny - 1, Math.max(0, Math.round((cy * sh - side / 2) / step)));
|
|
1158
|
+
return iy * nx + ix;
|
|
1159
|
+
}
|
|
1160
|
+
uncrop(tsUs, n, lm, rects) {
|
|
1161
|
+
const crop = this.faceCrops.get(tsUs);
|
|
1162
|
+
for (const ts of this.faceCrops.keys()) if (ts === tsUs || ts < tsUs - FOLLOW_KEPT_US) this.faceCrops.delete(ts);
|
|
1163
|
+
if (!crop) return;
|
|
1164
|
+
for (let i = 0; i < n; i += 3) {
|
|
1165
|
+
lm[i] = crop[0] + lm[i] * crop[2];
|
|
1166
|
+
lm[i + 1] = crop[1] + lm[i + 1] * crop[3];
|
|
1167
|
+
lm[i + 2] = lm[i + 2] * crop[2];
|
|
1168
|
+
}
|
|
1169
|
+
for (let i = 0; rects && i < rects.length; i += 3) {
|
|
1170
|
+
rects[i] = crop[0] + rects[i] * crop[2];
|
|
1171
|
+
rects[i + 1] = crop[1] + rects[i + 1] * crop[3];
|
|
1172
|
+
rects[i + 2] = rects[i + 2] * crop[2];
|
|
891
1173
|
}
|
|
892
1174
|
}
|
|
1175
|
+
dedupe(n, lm, cover, rects) {
|
|
1176
|
+
const faces = n / FACE_FLOATS;
|
|
1177
|
+
if (faces < 2) return { n, cover, rects };
|
|
1178
|
+
const boxes = Array.from({ length: faces }, (_, f) => faceBox(lm, f * FACE_FLOATS));
|
|
1179
|
+
const ref = this.followBox;
|
|
1180
|
+
const score = (b) => (ref ? overlap(b, ref) : 0) + b[2] * b[3] * 1e-3;
|
|
1181
|
+
const keep = boxes.map(() => true);
|
|
1182
|
+
for (let a = 0; a < faces; a++) {
|
|
1183
|
+
for (let b = a + 1; b < faces; b++) {
|
|
1184
|
+
if (!keep[a] || !keep[b] || overlap(boxes[a], boxes[b]) <= SAME_FACE) continue;
|
|
1185
|
+
keep[score(boxes[a]) >= score(boxes[b]) ? b : a] = false;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (keep.every(Boolean)) return { n, cover, rects };
|
|
1189
|
+
const cells = COVER_SIZE * COVER_SIZE;
|
|
1190
|
+
const outCover = cover ? new Uint8Array(cover.length) : null;
|
|
1191
|
+
const outRects = rects ? new Float32Array(rects.length) : null;
|
|
1192
|
+
let k = 0;
|
|
1193
|
+
for (let f = 0; f < faces; f++) {
|
|
1194
|
+
if (!keep[f]) continue;
|
|
1195
|
+
if (k !== f) lm.copyWithin(k * FACE_FLOATS, f * FACE_FLOATS, (f + 1) * FACE_FLOATS);
|
|
1196
|
+
if (cover && outCover) outCover.set(cover.subarray(f * cells, (f + 1) * cells), k * cells);
|
|
1197
|
+
if (rects && outRects) outRects.set(rects.subarray(f * 3, f * 3 + 3), k * 3);
|
|
1198
|
+
k++;
|
|
1199
|
+
}
|
|
1200
|
+
return { n: k * FACE_FLOATS, cover: outCover ? outCover.slice(0, k * cells) : null, rects: outRects ? outRects.slice(0, k * 3) : null };
|
|
1201
|
+
}
|
|
1202
|
+
follow(tsUs, n, lm, probe) {
|
|
1203
|
+
for (const ts of this.probes) if (ts < tsUs - FOLLOW_KEPT_US) this.probes.delete(ts);
|
|
1204
|
+
if (tsUs <= this.followTs || probe && (n === 0 || n < this.followFloats)) return;
|
|
1205
|
+
this.followTs = tsUs;
|
|
1206
|
+
this.faceSeen = n > 0;
|
|
1207
|
+
if (n === 0) {
|
|
1208
|
+
this.followGrow = 0;
|
|
1209
|
+
this.followFloats = 0;
|
|
1210
|
+
this.followAll = false;
|
|
1211
|
+
const box = this.followBox;
|
|
1212
|
+
if (box && this.followMiss < FOLLOW_WIDEN) {
|
|
1213
|
+
this.followMiss++;
|
|
1214
|
+
return;
|
|
1215
|
+
}
|
|
1216
|
+
this.followMiss = 0;
|
|
1217
|
+
if (box) this.scanFrom = this.scanTile(box[0] + box[2] / 2, box[1] + box[3] / 2);
|
|
1218
|
+
this.scanTurn = 1;
|
|
1219
|
+
this.followBox = null;
|
|
1220
|
+
this.followCrop = null;
|
|
1221
|
+
this.boxFloats = 0;
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
this.followMiss = 0;
|
|
1225
|
+
const old = this.followBox;
|
|
1226
|
+
const grow = old !== null && this.boxFloats > 0 && n > this.boxFloats && !probe;
|
|
1227
|
+
this.followGrow = grow ? this.followGrow + 1 : 0;
|
|
1228
|
+
const take = grow && this.followGrow < FOLLOW_GROW ? this.boxFloats : n;
|
|
1229
|
+
const boxes = [];
|
|
1230
|
+
for (let f = 0; f < n; f += FACE_FLOATS) boxes.push(faceBox(lm, f));
|
|
1231
|
+
if (take < n && old) boxes.sort((a, b) => overlap(b, old) - overlap(a, old));
|
|
1232
|
+
const kept = boxes.slice(0, take / FACE_FLOATS);
|
|
1233
|
+
const x0 = Math.min(...kept.map((b) => b[0]));
|
|
1234
|
+
const y0 = Math.min(...kept.map((b) => b[1]));
|
|
1235
|
+
const x1 = Math.max(...kept.map((b) => b[0] + b[2]));
|
|
1236
|
+
const y1 = Math.max(...kept.map((b) => b[1] + b[3]));
|
|
1237
|
+
this.followBox = [x0, y0, x1 - x0, y1 - y0];
|
|
1238
|
+
this.boxFloats = take;
|
|
1239
|
+
this.followFloats = take;
|
|
1240
|
+
this.followAll = take >= this.faceCount * FACE_FLOATS;
|
|
1241
|
+
}
|
|
1242
|
+
freeFaceWorker() {
|
|
1243
|
+
if (this.detects.get("face")?.accepts("face")) return "face";
|
|
1244
|
+
if (this.faceExtraUp && this.detects.get("faceExtra")?.accepts("face")) return "faceExtra";
|
|
1245
|
+
return null;
|
|
1246
|
+
}
|
|
1247
|
+
ensureFaceExtra() {
|
|
1248
|
+
if (this.faceExtraTried || this.closed) return;
|
|
1249
|
+
this.faceExtraTried = true;
|
|
1250
|
+
const n = this.faceCount;
|
|
1251
|
+
this.remote("faceExtra", (d) => d.initFace(n)).then(() => {
|
|
1252
|
+
this.faceExtraUp = true;
|
|
1253
|
+
}).catch(() => {
|
|
1254
|
+
this.detects.get("faceExtra")?.close();
|
|
1255
|
+
this.detects.delete("faceExtra");
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
893
1258
|
/** Face and mask asked on the same frame share one draw. */
|
|
894
1259
|
detectCopy(source, timestampUs) {
|
|
895
1260
|
const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
@@ -969,45 +1334,99 @@ var Engine = class _Engine {
|
|
|
969
1334
|
if (this.prevTs >= 0 && timestampUs > this.prevTs) {
|
|
970
1335
|
this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
|
|
971
1336
|
}
|
|
1337
|
+
if (timestampUs < this.prevTs) {
|
|
1338
|
+
this.faceResults = [];
|
|
1339
|
+
this.maskResults = [];
|
|
1340
|
+
this.faceAsked.clear();
|
|
1341
|
+
this.faceCrops.clear();
|
|
1342
|
+
this.followBox = null;
|
|
1343
|
+
this.followCrop = null;
|
|
1344
|
+
this.followTs = -Infinity;
|
|
1345
|
+
this.faceSeen = false;
|
|
1346
|
+
this.followFloats = 0;
|
|
1347
|
+
this.followAll = false;
|
|
1348
|
+
this.followTurn = 0;
|
|
1349
|
+
this.followMiss = 0;
|
|
1350
|
+
this.followGrow = 0;
|
|
1351
|
+
this.boxFloats = 0;
|
|
1352
|
+
this.scanFrom = 0;
|
|
1353
|
+
this.scanTurn = 0;
|
|
1354
|
+
this.probes.clear();
|
|
1355
|
+
this.caughtUpTs = -Infinity;
|
|
1356
|
+
this.thumbs.clear();
|
|
1357
|
+
}
|
|
972
1358
|
this.prevTs = timestampUs;
|
|
973
1359
|
if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
|
|
974
1360
|
this.releaseHeld();
|
|
975
1361
|
this.renderedTs = timestampUs;
|
|
976
1362
|
return this.draw(source, timestampUs, false);
|
|
977
1363
|
}
|
|
1364
|
+
this.ensureFaceExtra();
|
|
978
1365
|
this.requestRemote("face", source, timestampUs);
|
|
979
1366
|
const remoteMask = this.remoteMaskKind();
|
|
980
|
-
if (remoteMask
|
|
1367
|
+
if (remoteMask === "merged") {
|
|
1368
|
+
this.requestRemote("person", source, timestampUs);
|
|
1369
|
+
this.requestRemote("mask", source, timestampUs);
|
|
1370
|
+
} else if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
|
|
981
1371
|
this.queue.push({ c: this.hold(source), ts: timestampUs });
|
|
982
|
-
this.
|
|
983
|
-
if (this.queue.length <= this.
|
|
1372
|
+
while (this.queue.length > MAX_HOLD + 1) this.faceAsked.delete(this.spareFront());
|
|
1373
|
+
if (this.queue.length <= MAX_HOLD && !this.faceReady(this.queue[0].ts)) {
|
|
984
1374
|
this.swallowed = true;
|
|
985
1375
|
return false;
|
|
986
1376
|
}
|
|
987
|
-
|
|
1377
|
+
if (this.queue.length > 2 && this.faceReady(this.queue[1].ts) && timestampUs - this.caughtUpTs >= CATCH_UP_EVERY_US) {
|
|
1378
|
+
this.caughtUpTs = timestampUs;
|
|
1379
|
+
this.faceAsked.delete(this.spareFront());
|
|
1380
|
+
}
|
|
988
1381
|
const front = this.queue.shift();
|
|
1382
|
+
this.faceAsked.delete(front.ts);
|
|
1383
|
+
this.faceFor(front.ts);
|
|
1384
|
+
this.masksFor(front.ts);
|
|
989
1385
|
this.lastHeld = front.c;
|
|
990
1386
|
this.renderedTs = front.ts;
|
|
991
1387
|
return this.draw(front.c, front.ts, true);
|
|
992
1388
|
}
|
|
993
1389
|
remoteMaskKind() {
|
|
994
|
-
|
|
995
|
-
if (
|
|
996
|
-
return
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
const
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1390
|
+
const person = this.personRemote && this.personRemoteWanted(this.filter, this.effects);
|
|
1391
|
+
if (!needsSkinClasses(this.filter, this.effects)) return person ? "person" : null;
|
|
1392
|
+
if (this.classRemote) return person ? "merged" : "mask";
|
|
1393
|
+
return person && !this.inThread.has("mask") ? "person" : null;
|
|
1394
|
+
}
|
|
1395
|
+
spareFront() {
|
|
1396
|
+
const h = this.queue.shift();
|
|
1397
|
+
this.spare.push(h.c);
|
|
1398
|
+
return h.ts;
|
|
1399
|
+
}
|
|
1400
|
+
applyMask(r) {
|
|
1401
|
+
if (r.person) {
|
|
1402
|
+
this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, r.width, r.height, r.data);
|
|
1403
|
+
this.personMaskFresh = true;
|
|
1404
|
+
this.personMaskTs = r.ts;
|
|
1405
|
+
this.personWorkerMs = r.ms;
|
|
1406
|
+
this.personBytes = { width: r.width, height: r.height, data: r.data };
|
|
1407
|
+
this.merge(r.ts);
|
|
1004
1408
|
return;
|
|
1005
1409
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1410
|
+
this.uploadClassMask(r.width, r.height, r.data);
|
|
1411
|
+
this.classMaskTs = r.ts;
|
|
1412
|
+
this.maskWorkerMs = r.ms;
|
|
1413
|
+
this.classBytes = { width: r.width, height: r.height, data: r.data };
|
|
1414
|
+
this.merge(this.personMaskTs);
|
|
1415
|
+
}
|
|
1416
|
+
masksFor(tsUs) {
|
|
1417
|
+
for (const person of [false, true]) {
|
|
1418
|
+
let pick = null;
|
|
1419
|
+
for (const r of this.maskResults) if (r.person === person && (!pick || Math.abs(r.ts - tsUs) < Math.abs(pick.ts - tsUs))) pick = r;
|
|
1420
|
+
if (!pick) continue;
|
|
1421
|
+
const chosen = pick;
|
|
1422
|
+
this.maskResults = this.maskResults.filter((r) => r.person !== person || r.ts >= chosen.ts);
|
|
1423
|
+
if (chosen.ts !== (person ? this.personMaskTs : this.classMaskTs)) this.applyMask(chosen);
|
|
1009
1424
|
}
|
|
1010
1425
|
}
|
|
1426
|
+
faceReady(tsUs) {
|
|
1427
|
+
const asked = this.faceAsked.has(tsUs);
|
|
1428
|
+
return this.faceResults.some((r) => asked ? r.ts === tsUs : r.ts > tsUs);
|
|
1429
|
+
}
|
|
1011
1430
|
dutyAllows(kind) {
|
|
1012
1431
|
const took = kind === "face" ? this.faceLatencyMs : this.maskLatencyMs.get(kind) ?? 0;
|
|
1013
1432
|
if (took * 1e3 < this.gapUs) return true;
|
|
@@ -1020,6 +1439,89 @@ var Engine = class _Engine {
|
|
|
1020
1439
|
this.sent.delete(kind);
|
|
1021
1440
|
return performance.now() - s.at;
|
|
1022
1441
|
}
|
|
1442
|
+
applyFace(tsUs, n, lm, cover, rects) {
|
|
1443
|
+
this.faceMisses = n === 0 ? this.faceMisses + 1 : 0;
|
|
1444
|
+
if (n > 0) this.showCover(cover, rects);
|
|
1445
|
+
else if (this.faceMisses >= COVER_MISSES) this.hideCover();
|
|
1446
|
+
this.lmBuf.set(lm);
|
|
1447
|
+
const [lo, hi] = splitMicros(tsUs);
|
|
1448
|
+
this.core.setFace(lo, hi, n);
|
|
1449
|
+
this.faceTs = tsUs;
|
|
1450
|
+
}
|
|
1451
|
+
faceFor(tsUs) {
|
|
1452
|
+
this.faceResults.sort((a2, b) => a2.ts - b.ts);
|
|
1453
|
+
let pick = -1;
|
|
1454
|
+
for (let i = 0; i < this.faceResults.length; i++) if (this.faceResults[i].ts <= tsUs) pick = i;
|
|
1455
|
+
if (pick < 0) return;
|
|
1456
|
+
const r = this.faceResults[pick];
|
|
1457
|
+
const next = this.faceResults[pick + 1];
|
|
1458
|
+
this.faceResults.splice(0, pick);
|
|
1459
|
+
if (r.ts === tsUs || !next || !this.sameFaces(r, next)) {
|
|
1460
|
+
if (r.ts !== this.faceTs) this.applyFace(r.ts, r.n, r.lm, r.cover, r.rects);
|
|
1461
|
+
return;
|
|
1462
|
+
}
|
|
1463
|
+
const a = (tsUs - r.ts) / (next.ts - r.ts);
|
|
1464
|
+
if (!this.lerpBuf || this.lerpBuf.length !== r.lm.length) this.lerpBuf = new Float32Array(r.lm.length);
|
|
1465
|
+
for (let i = 0; i < r.n; i++) this.lerpBuf[i] = r.lm[i] + (next.lm[i] - r.lm[i]) * a;
|
|
1466
|
+
const near = a < 0.5 ? r : next;
|
|
1467
|
+
this.applyFace(tsUs, r.n, this.lerpBuf, near.cover, near.rects);
|
|
1468
|
+
}
|
|
1469
|
+
hideCover() {
|
|
1470
|
+
if (!this.coverOn || !this.coverCanvas || this.coverHidden) return;
|
|
1471
|
+
const ctx = this.coverCanvas.getContext("2d");
|
|
1472
|
+
ctx.fillStyle = "#000";
|
|
1473
|
+
ctx.fillRect(0, 0, this.coverCanvas.width, this.coverCanvas.height);
|
|
1474
|
+
this.core.setFaceCover(this.coverCanvas);
|
|
1475
|
+
this.coverShown = null;
|
|
1476
|
+
this.coverHidden = true;
|
|
1477
|
+
}
|
|
1478
|
+
showCover(cover, rects) {
|
|
1479
|
+
this.coverHidden = false;
|
|
1480
|
+
if (!cover || !rects || !(this.effects.occluders ?? false)) {
|
|
1481
|
+
if (this.coverOn) this.core.setFaceCover(null);
|
|
1482
|
+
this.coverOn = false;
|
|
1483
|
+
this.coverShown = null;
|
|
1484
|
+
return;
|
|
1485
|
+
}
|
|
1486
|
+
if (cover === this.coverShown && this.coverOn) return;
|
|
1487
|
+
const w = COVER_FRAME_W;
|
|
1488
|
+
const h = Math.max(1, Math.round(w * (this.srcH || this.canvas.height) / (this.srcW || this.canvas.width)));
|
|
1489
|
+
if (!this.coverCanvas || this.coverCanvas.height !== h) this.coverCanvas = new OffscreenCanvas(w, h);
|
|
1490
|
+
this.coverCell ??= new OffscreenCanvas(COVER_SIZE, COVER_SIZE);
|
|
1491
|
+
const ctx = this.coverCanvas.getContext("2d");
|
|
1492
|
+
const cell = this.coverCell.getContext("2d");
|
|
1493
|
+
ctx.fillStyle = "#fff";
|
|
1494
|
+
ctx.fillRect(0, 0, w, h);
|
|
1495
|
+
this.coverImg ??= cell.createImageData(COVER_SIZE, COVER_SIZE);
|
|
1496
|
+
const img = this.coverImg;
|
|
1497
|
+
const cells = COVER_SIZE * COVER_SIZE;
|
|
1498
|
+
const held = this.coverLast && this.coverLast !== cover && this.coverLast.length === cover.length ? this.coverLast : null;
|
|
1499
|
+
for (let f = 0; f * 3 < rects.length; f++) {
|
|
1500
|
+
for (let i = 0; i < cells; i++) {
|
|
1501
|
+
const now2 = cover[f * cells + i];
|
|
1502
|
+
const v = held ? Math.min(now2, now2 * (1 - COVER_HOLD) + held[f * cells + i] * COVER_HOLD) : now2;
|
|
1503
|
+
img.data[i * 4] = v;
|
|
1504
|
+
img.data[i * 4 + 1] = v;
|
|
1505
|
+
img.data[i * 4 + 2] = v;
|
|
1506
|
+
img.data[i * 4 + 3] = 255;
|
|
1507
|
+
}
|
|
1508
|
+
cell.putImageData(img, 0, 0);
|
|
1509
|
+
const side = rects[f * 3 + 2] * w;
|
|
1510
|
+
ctx.drawImage(this.coverCell, rects[f * 3] * w - side / 2, rects[f * 3 + 1] * h - side / 2, side, side);
|
|
1511
|
+
}
|
|
1512
|
+
this.core.setFaceCover(this.coverCanvas);
|
|
1513
|
+
this.coverOn = true;
|
|
1514
|
+
this.coverLast = cover;
|
|
1515
|
+
this.coverShown = cover;
|
|
1516
|
+
}
|
|
1517
|
+
sameFaces(a, b) {
|
|
1518
|
+
if (a.n === 0 || a.n !== b.n) return false;
|
|
1519
|
+
for (let k = 0; k < a.n; k += FACE_FLOATS) {
|
|
1520
|
+
const nose = k + 3;
|
|
1521
|
+
if (Math.abs(a.lm[nose] - b.lm[nose]) > FACE_JUMP || Math.abs(a.lm[nose + 1] - b.lm[nose + 1]) > FACE_JUMP) return false;
|
|
1522
|
+
}
|
|
1523
|
+
return true;
|
|
1524
|
+
}
|
|
1023
1525
|
releaseHeld() {
|
|
1024
1526
|
for (const h of this.queue) this.spare.push(h.c);
|
|
1025
1527
|
this.queue = [];
|
|
@@ -1053,7 +1555,24 @@ var Engine = class _Engine {
|
|
|
1053
1555
|
this.used = null;
|
|
1054
1556
|
let served = false;
|
|
1055
1557
|
const remoteMask = this.remoteMaskKind();
|
|
1056
|
-
if (remoteMask
|
|
1558
|
+
if (remoteMask === "merged") {
|
|
1559
|
+
if (!faceDone) {
|
|
1560
|
+
this.requestRemote("person", source, timestampUs);
|
|
1561
|
+
this.requestRemote("mask", source, timestampUs);
|
|
1562
|
+
}
|
|
1563
|
+
const tex = this.mergedTex ?? this.personMaskTex;
|
|
1564
|
+
if (tex) {
|
|
1565
|
+
served = true;
|
|
1566
|
+
if (this.mergedTex ? this.mergedFresh : this.personMaskFresh) {
|
|
1567
|
+
this.mergedFresh = false;
|
|
1568
|
+
this.personMaskFresh = false;
|
|
1569
|
+
mask = tex;
|
|
1570
|
+
maskKind = this.mergedTex ? 1 : 0;
|
|
1571
|
+
maskTs = this.mergedTex ? this.mergedTs : this.personMaskTs;
|
|
1572
|
+
segMs = this.personWorkerMs;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
} else if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
|
|
1057
1576
|
if (remoteMask === "person") {
|
|
1058
1577
|
if (this.personMaskTex) {
|
|
1059
1578
|
served = true;
|
|
@@ -1141,7 +1660,7 @@ var Engine = class _Engine {
|
|
|
1141
1660
|
needsFace: s ? s[10] > 0.5 : false,
|
|
1142
1661
|
autoExposure: s ? s[11] : 0,
|
|
1143
1662
|
faces: s ? s[12] : 0,
|
|
1144
|
-
heldFrames: faceDone ? this.
|
|
1663
|
+
heldFrames: faceDone ? this.queue.length : 0,
|
|
1145
1664
|
faceAgeMs: s && s[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
|
|
1146
1665
|
maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0,
|
|
1147
1666
|
effectError: this.programErr || this.loadErr
|
|
@@ -1176,6 +1695,11 @@ var Engine = class _Engine {
|
|
|
1176
1695
|
if (this.classMaskTex) this.gl.deleteTexture(this.classMaskTex);
|
|
1177
1696
|
if (this.personMaskTex) this.gl.deleteTexture(this.personMaskTex);
|
|
1178
1697
|
this.personMaskTex = null;
|
|
1698
|
+
if (this.mergedTex) this.gl.deleteTexture(this.mergedTex);
|
|
1699
|
+
this.mergedTex = null;
|
|
1700
|
+
this.personBytes = null;
|
|
1701
|
+
this.classBytes = null;
|
|
1702
|
+
this.mergedBytes = null;
|
|
1179
1703
|
this.personRemote = false;
|
|
1180
1704
|
this.classMaskTex = null;
|
|
1181
1705
|
this.lmBuf = null;
|