@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/worker.js
CHANGED
|
@@ -4723,6 +4723,22 @@
|
|
|
4723
4723
|
});
|
|
4724
4724
|
}
|
|
4725
4725
|
|
|
4726
|
+
// src/mp-clock.ts
|
|
4727
|
+
var SOURCE_SWITCH_GAP_MS = 33;
|
|
4728
|
+
function monotonicClock() {
|
|
4729
|
+
let offset = 0;
|
|
4730
|
+
let last = -Infinity;
|
|
4731
|
+
return (timestampMs) => {
|
|
4732
|
+
let t2 = timestampMs + offset;
|
|
4733
|
+
if (t2 <= last) {
|
|
4734
|
+
offset += last + SOURCE_SWITCH_GAP_MS - t2;
|
|
4735
|
+
t2 = last + SOURCE_SWITCH_GAP_MS;
|
|
4736
|
+
}
|
|
4737
|
+
last = t2;
|
|
4738
|
+
return t2;
|
|
4739
|
+
};
|
|
4740
|
+
}
|
|
4741
|
+
|
|
4726
4742
|
// src/segmenter.ts
|
|
4727
4743
|
async function createSegmenter(canvas, model = "person") {
|
|
4728
4744
|
const fileset2 = await visionFileset();
|
|
@@ -4742,6 +4758,7 @@
|
|
|
4742
4758
|
outputCategoryMask: true,
|
|
4743
4759
|
outputConfidenceMasks: false
|
|
4744
4760
|
});
|
|
4761
|
+
const clock = monotonicClock();
|
|
4745
4762
|
let pending = null;
|
|
4746
4763
|
const release = () => {
|
|
4747
4764
|
pending?.close();
|
|
@@ -4751,7 +4768,7 @@
|
|
|
4751
4768
|
segment(frame, timestampMs) {
|
|
4752
4769
|
release();
|
|
4753
4770
|
let texture = null;
|
|
4754
|
-
segmenter.segmentForVideo(frame, timestampMs, (result) => {
|
|
4771
|
+
segmenter.segmentForVideo(frame, clock(timestampMs), (result) => {
|
|
4755
4772
|
const mask = result.categoryMask;
|
|
4756
4773
|
if (mask) texture = mask.getAsWebGLTexture();
|
|
4757
4774
|
pending = result;
|
|
@@ -4768,12 +4785,26 @@
|
|
|
4768
4785
|
|
|
4769
4786
|
// src/face.ts
|
|
4770
4787
|
var FACE_FLOATS = 478 * 3;
|
|
4771
|
-
|
|
4788
|
+
var COVER_SIZE = 96;
|
|
4789
|
+
function faceBox(lm, at2) {
|
|
4790
|
+
let x0 = 1;
|
|
4791
|
+
let y0 = 1;
|
|
4792
|
+
let x1 = 0;
|
|
4793
|
+
let y1 = 0;
|
|
4794
|
+
for (let i2 = at2; i2 < at2 + FACE_FLOATS; i2 += 3) {
|
|
4795
|
+
x0 = Math.min(x0, lm[i2]);
|
|
4796
|
+
x1 = Math.max(x1, lm[i2]);
|
|
4797
|
+
y0 = Math.min(y0, lm[i2 + 1]);
|
|
4798
|
+
y1 = Math.max(y1, lm[i2 + 1]);
|
|
4799
|
+
}
|
|
4800
|
+
return [x0, y0, x1 - x0, y1 - y0];
|
|
4801
|
+
}
|
|
4802
|
+
async function createFaceDetector(canvas, maxFaces, delegate = "GPU", mode = "VIDEO") {
|
|
4772
4803
|
const fileset2 = await visionFileset();
|
|
4773
4804
|
const landmarker = await yc.createFromOptions(fileset2, {
|
|
4774
4805
|
baseOptions: { modelAssetPath: MODEL_URL.face, delegate },
|
|
4775
4806
|
canvas,
|
|
4776
|
-
runningMode:
|
|
4807
|
+
runningMode: mode,
|
|
4777
4808
|
// As many as the core tracks: one landmark pass returns them all, so the cost of a second
|
|
4778
4809
|
// face is the copy below and its own shader passes, not a second inference.
|
|
4779
4810
|
numFaces: maxFaces,
|
|
@@ -4781,9 +4812,10 @@
|
|
|
4781
4812
|
outputFaceBlendshapes: false,
|
|
4782
4813
|
outputFacialTransformationMatrixes: false
|
|
4783
4814
|
});
|
|
4815
|
+
const clock = monotonicClock();
|
|
4784
4816
|
return {
|
|
4785
4817
|
detect(frame, timestampMs, out) {
|
|
4786
|
-
const res = landmarker.detectForVideo(frame, timestampMs);
|
|
4818
|
+
const res = mode === "VIDEO" ? landmarker.detectForVideo(frame, clock(timestampMs)) : landmarker.detect(frame);
|
|
4787
4819
|
let n2 = 0;
|
|
4788
4820
|
for (const lm of res.faceLandmarks ?? []) {
|
|
4789
4821
|
if (lm.length * 3 !== FACE_FLOATS || n2 + FACE_FLOATS > out.length) break;
|
|
@@ -4828,13 +4860,13 @@
|
|
|
4828
4860
|
return this.worker !== null && this.up.has(kind) && !this.busy.has(kind);
|
|
4829
4861
|
}
|
|
4830
4862
|
/** Transfers the frame. False when that kind is busy or not up; the frame is then closed. */
|
|
4831
|
-
request(kind, frame, tsUs) {
|
|
4863
|
+
request(kind, frame, tsUs, cover = false, retry = null) {
|
|
4832
4864
|
if (!this.accepts(kind)) {
|
|
4833
4865
|
frame.close();
|
|
4834
4866
|
return false;
|
|
4835
4867
|
}
|
|
4836
4868
|
this.busy.add(kind);
|
|
4837
|
-
this.send({ t: kind, frame, tsUs }, [frame]);
|
|
4869
|
+
this.send(kind === "face" ? { t: kind, frame, tsUs, cover, retry } : { t: kind, frame, tsUs }, [frame]);
|
|
4838
4870
|
return true;
|
|
4839
4871
|
}
|
|
4840
4872
|
isUp(kind) {
|
|
@@ -4870,7 +4902,7 @@
|
|
|
4870
4902
|
break;
|
|
4871
4903
|
case "face":
|
|
4872
4904
|
this.busy.delete("face");
|
|
4873
|
-
if (this.up.has("face")) this.on.face(m2.tsUs, m2.n, m2.lm, m2.ms);
|
|
4905
|
+
if (this.up.has("face")) this.on.face(m2.tsUs, m2.n, m2.lm, m2.ms, m2.cover, m2.rects);
|
|
4874
4906
|
break;
|
|
4875
4907
|
case "mask":
|
|
4876
4908
|
this.busy.delete("mask");
|
|
@@ -5036,6 +5068,75 @@
|
|
|
5036
5068
|
detail;
|
|
5037
5069
|
};
|
|
5038
5070
|
|
|
5071
|
+
// src/mask-merge.ts
|
|
5072
|
+
var CLASS_BACKGROUND = 0;
|
|
5073
|
+
var CLASS_BODY_SKIN = 2;
|
|
5074
|
+
var CLASS_CLOTHES = 4;
|
|
5075
|
+
var CLASS_OTHERS = 5;
|
|
5076
|
+
var CLASS_STALE_OCCLUDER = 6;
|
|
5077
|
+
var THUMB_W = 64;
|
|
5078
|
+
var THUMB_H = 36;
|
|
5079
|
+
var MOVED_LUMA = 12;
|
|
5080
|
+
function lumaThumb(rgba) {
|
|
5081
|
+
const y2 = new Uint8Array(THUMB_W * THUMB_H);
|
|
5082
|
+
for (let i2 = 0; i2 < y2.length; i2++) y2[i2] = rgba[i2 * 4] * 77 + rgba[i2 * 4 + 1] * 150 + rgba[i2 * 4 + 2] * 29 >> 8;
|
|
5083
|
+
return y2;
|
|
5084
|
+
}
|
|
5085
|
+
function movedBetween(a2, b2) {
|
|
5086
|
+
const w2 = THUMB_W;
|
|
5087
|
+
const h2 = THUMB_H;
|
|
5088
|
+
const hit = new Uint8Array(w2 * h2);
|
|
5089
|
+
for (let i2 = 0; i2 < hit.length; i2++) hit[i2] = Math.abs(a2[i2] - b2[i2]) > MOVED_LUMA ? 1 : 0;
|
|
5090
|
+
const data = new Uint8Array(w2 * h2);
|
|
5091
|
+
for (let y2 = 0; y2 < h2; y2++) {
|
|
5092
|
+
for (let x2 = 0; x2 < w2; x2++) {
|
|
5093
|
+
let v2 = 0;
|
|
5094
|
+
for (let dy = -1; dy <= 1 && !v2; dy++) {
|
|
5095
|
+
const yy = y2 + dy;
|
|
5096
|
+
if (yy < 0 || yy >= h2) continue;
|
|
5097
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
5098
|
+
const xx = x2 + dx;
|
|
5099
|
+
if (xx >= 0 && xx < w2 && hit[yy * w2 + xx]) {
|
|
5100
|
+
v2 = 1;
|
|
5101
|
+
break;
|
|
5102
|
+
}
|
|
5103
|
+
}
|
|
5104
|
+
}
|
|
5105
|
+
data[y2 * w2 + x2] = v2;
|
|
5106
|
+
}
|
|
5107
|
+
}
|
|
5108
|
+
return { width: w2, height: h2, data };
|
|
5109
|
+
}
|
|
5110
|
+
function mergeMasks(person, classes, out, moved = null) {
|
|
5111
|
+
const { width: w2, height: h2 } = person;
|
|
5112
|
+
const merged = out && out.length === w2 * h2 ? out : new Uint8Array(w2 * h2);
|
|
5113
|
+
const cw = classes.width;
|
|
5114
|
+
const ch2 = classes.height;
|
|
5115
|
+
const sx = cw / w2;
|
|
5116
|
+
const sy = ch2 / h2;
|
|
5117
|
+
const mw = moved?.width ?? 0;
|
|
5118
|
+
const mh2 = moved?.height ?? 0;
|
|
5119
|
+
for (let y2 = 0; y2 < h2; y2++) {
|
|
5120
|
+
const crow = Math.min(ch2 - 1, Math.floor((y2 + 0.5) * sy)) * cw;
|
|
5121
|
+
const mrow = moved ? Math.min(mh2 - 1, Math.floor((y2 + 0.5) * mh2 / h2)) * mw : 0;
|
|
5122
|
+
const prow = y2 * w2;
|
|
5123
|
+
for (let x2 = 0; x2 < w2; x2++) {
|
|
5124
|
+
if (person.data[prow + x2] !== 0) {
|
|
5125
|
+
merged[prow + x2] = CLASS_BACKGROUND;
|
|
5126
|
+
continue;
|
|
5127
|
+
}
|
|
5128
|
+
const c2 = classes.data[crow + Math.min(cw - 1, Math.floor((x2 + 0.5) * sx))];
|
|
5129
|
+
if (c2 === CLASS_BACKGROUND) {
|
|
5130
|
+
merged[prow + x2] = CLASS_CLOTHES;
|
|
5131
|
+
continue;
|
|
5132
|
+
}
|
|
5133
|
+
const stale = moved !== null && (c2 === CLASS_BODY_SKIN || c2 === CLASS_OTHERS) && moved.data[mrow + Math.min(mw - 1, Math.floor((x2 + 0.5) * mw / w2))] !== 0;
|
|
5134
|
+
merged[prow + x2] = stale ? CLASS_STALE_OCCLUDER : c2;
|
|
5135
|
+
}
|
|
5136
|
+
}
|
|
5137
|
+
return merged;
|
|
5138
|
+
}
|
|
5139
|
+
|
|
5039
5140
|
// src/recipe.ts
|
|
5040
5141
|
var DEBUG_VIEWS = ["off", "skin", "mask", "face"];
|
|
5041
5142
|
function debugCode(view) {
|
|
@@ -5080,6 +5181,7 @@
|
|
|
5080
5181
|
keyColor: [0, 0, 0],
|
|
5081
5182
|
keyTolerance: 0,
|
|
5082
5183
|
autoLight: false,
|
|
5184
|
+
occluders: false,
|
|
5083
5185
|
mirror: false,
|
|
5084
5186
|
slimFace: 0,
|
|
5085
5187
|
enlargeEyes: 0,
|
|
@@ -5173,6 +5275,7 @@
|
|
|
5173
5275
|
keyColor: e2.keyColor,
|
|
5174
5276
|
keyTolerance: e2.keyTolerance,
|
|
5175
5277
|
autoLight: e2.autoLight,
|
|
5278
|
+
occluders: e2.occluders ?? false,
|
|
5176
5279
|
mirror: e2.mirror,
|
|
5177
5280
|
slimFace: e2.slimFace,
|
|
5178
5281
|
enlargeEyes: e2.enlargeEyes,
|
|
@@ -5214,9 +5317,30 @@
|
|
|
5214
5317
|
maskAgeMs: 0,
|
|
5215
5318
|
effectError: ""
|
|
5216
5319
|
};
|
|
5217
|
-
var MAX_HOLD =
|
|
5320
|
+
var MAX_HOLD = 3;
|
|
5321
|
+
var COVER_FRAME_W = 256;
|
|
5322
|
+
var COVER_HOLD = 0.6;
|
|
5323
|
+
var COVER_MISSES = 2;
|
|
5324
|
+
var FOLLOW_SIDE = 384;
|
|
5325
|
+
var FOLLOW_MARGIN = 2.6;
|
|
5326
|
+
var FOLLOW_RETRY = 2;
|
|
5327
|
+
var FOLLOW_MIN_SIDE = 64;
|
|
5328
|
+
var FOLLOW_SHIFT = 0.06;
|
|
5329
|
+
var FOLLOW_RESIZE = 0.12;
|
|
5330
|
+
var FOLLOW_STALE_US = 5e5;
|
|
5331
|
+
var FOLLOW_KEPT_US = 1e6;
|
|
5332
|
+
var SCAN_SHARE = 0.5;
|
|
5333
|
+
var SCAN_STEP = 0.75;
|
|
5334
|
+
var FOLLOW_REFRESH = 30;
|
|
5335
|
+
var FOLLOW_WIDEN = 2;
|
|
5336
|
+
var FOLLOW_GROW = 3;
|
|
5337
|
+
var SAME_FACE = 0.3;
|
|
5338
|
+
var FACE_RESULTS_KEPT = 8;
|
|
5339
|
+
var THUMBS_KEPT = 12;
|
|
5218
5340
|
var LINK_LOSS_WINDOW_MS = 3e3;
|
|
5219
|
-
var
|
|
5341
|
+
var FACE_JUMP = 0.15;
|
|
5342
|
+
var CATCH_UP_EVERY_US = 1e6;
|
|
5343
|
+
var MASK_RESULTS_KEPT = 8;
|
|
5220
5344
|
var DETECT_DUTY = 0.6;
|
|
5221
5345
|
function gpuName(gl) {
|
|
5222
5346
|
const info = gl.getExtension("WEBGL_debug_renderer_info");
|
|
@@ -5229,6 +5353,18 @@
|
|
|
5229
5353
|
const s2 = source;
|
|
5230
5354
|
return [s2.width, s2.height];
|
|
5231
5355
|
}
|
|
5356
|
+
function tileGrid(sw, sh2) {
|
|
5357
|
+
const side = Math.max(FOLLOW_MIN_SIDE, Math.round(Math.min(sw, sh2) * SCAN_SHARE));
|
|
5358
|
+
const step = side * SCAN_STEP;
|
|
5359
|
+
return { side, step, nx: Math.max(1, Math.ceil((sw - side) / step) + 1), ny: Math.max(1, Math.ceil((sh2 - side) / step) + 1) };
|
|
5360
|
+
}
|
|
5361
|
+
function overlap(a2, b2) {
|
|
5362
|
+
const w2 = Math.min(a2[0] + a2[2], b2[0] + b2[2]) - Math.max(a2[0], b2[0]);
|
|
5363
|
+
const h2 = Math.min(a2[1] + a2[3], b2[1] + b2[3]) - Math.max(a2[1], b2[1]);
|
|
5364
|
+
if (w2 <= 0 || h2 <= 0) return 0;
|
|
5365
|
+
const i2 = w2 * h2;
|
|
5366
|
+
return i2 / (a2[2] * a2[3] + b2[2] * b2[3] - i2);
|
|
5367
|
+
}
|
|
5232
5368
|
var FACE_COLOR_SKIPPED = "face colour shaders skipped: linking one lost the WebGL context on this GPU before";
|
|
5233
5369
|
var Engine = class _Engine {
|
|
5234
5370
|
canvas;
|
|
@@ -5282,13 +5418,14 @@
|
|
|
5282
5418
|
classRemote = false;
|
|
5283
5419
|
classMaskTex = null;
|
|
5284
5420
|
classMaskFresh = false;
|
|
5285
|
-
classMaskTs =
|
|
5421
|
+
classMaskTs = -1;
|
|
5286
5422
|
syncFaces = false;
|
|
5287
5423
|
renderedTs = 0;
|
|
5288
5424
|
queue = [];
|
|
5289
5425
|
spare = [];
|
|
5290
|
-
|
|
5291
|
-
|
|
5426
|
+
faceExtraUp = false;
|
|
5427
|
+
faceExtraTried = false;
|
|
5428
|
+
lerpBuf = null;
|
|
5292
5429
|
gapUs = 33333;
|
|
5293
5430
|
prevTs = -1;
|
|
5294
5431
|
sent = /* @__PURE__ */ new Map();
|
|
@@ -5296,6 +5433,35 @@
|
|
|
5296
5433
|
faceLatencyMs = 0;
|
|
5297
5434
|
maskLatencyMs = /* @__PURE__ */ new Map();
|
|
5298
5435
|
faceTs = -1;
|
|
5436
|
+
faceResults = [];
|
|
5437
|
+
coverCanvas = null;
|
|
5438
|
+
coverCell = null;
|
|
5439
|
+
coverOn = false;
|
|
5440
|
+
faceMisses = 0;
|
|
5441
|
+
coverHidden = false;
|
|
5442
|
+
srcW = 0;
|
|
5443
|
+
srcH = 0;
|
|
5444
|
+
coverLast = null;
|
|
5445
|
+
coverShown = null;
|
|
5446
|
+
coverImg = null;
|
|
5447
|
+
faceAsked = /* @__PURE__ */ new Set();
|
|
5448
|
+
faceCrops = /* @__PURE__ */ new Map();
|
|
5449
|
+
followBox = null;
|
|
5450
|
+
followTs = -Infinity;
|
|
5451
|
+
followCrop = null;
|
|
5452
|
+
followCanvas = null;
|
|
5453
|
+
followRetry = null;
|
|
5454
|
+
faceSeen = false;
|
|
5455
|
+
followFloats = 0;
|
|
5456
|
+
followAll = false;
|
|
5457
|
+
followTurn = 0;
|
|
5458
|
+
followMiss = 0;
|
|
5459
|
+
followGrow = 0;
|
|
5460
|
+
boxFloats = 0;
|
|
5461
|
+
scanFrom = 0;
|
|
5462
|
+
scanTurn = 0;
|
|
5463
|
+
probes = /* @__PURE__ */ new Set();
|
|
5464
|
+
maskResults = [];
|
|
5299
5465
|
maskUsedTs = -1;
|
|
5300
5466
|
lastHeld = null;
|
|
5301
5467
|
swallowed = false;
|
|
@@ -5305,13 +5471,23 @@
|
|
|
5305
5471
|
personLoading = null;
|
|
5306
5472
|
personMaskTex = null;
|
|
5307
5473
|
personMaskFresh = false;
|
|
5308
|
-
personMaskTs =
|
|
5474
|
+
personMaskTs = -1;
|
|
5309
5475
|
personWorkerMs = 0;
|
|
5476
|
+
personBytes = null;
|
|
5477
|
+
classBytes = null;
|
|
5478
|
+
mergedBytes = null;
|
|
5479
|
+
mergedTex = null;
|
|
5480
|
+
mergedFresh = false;
|
|
5481
|
+
mergedTs = 0;
|
|
5310
5482
|
detectCanvas = null;
|
|
5311
5483
|
detectCanvasTs = -1;
|
|
5484
|
+
thumbCanvas = null;
|
|
5485
|
+
thumbs = /* @__PURE__ */ new Map();
|
|
5312
5486
|
// A kind whose worker failed once stays in-thread.
|
|
5313
5487
|
inThread = /* @__PURE__ */ new Set();
|
|
5314
5488
|
faceLoading = null;
|
|
5489
|
+
faceCount = 1;
|
|
5490
|
+
caughtUpTs = -Infinity;
|
|
5315
5491
|
maxFaces = 3;
|
|
5316
5492
|
lmBuf = null;
|
|
5317
5493
|
filter = { kind: "none" };
|
|
@@ -5524,6 +5700,7 @@
|
|
|
5524
5700
|
this.faceLoading = this.remote("face", (d2) => d2.initFace(n2)).then(() => {
|
|
5525
5701
|
this.faceRemote = true;
|
|
5526
5702
|
this.lmBuf = buf;
|
|
5703
|
+
this.faceCount = n2;
|
|
5527
5704
|
}).catch((e2) => {
|
|
5528
5705
|
if (e2 instanceof LoadTimeout) throw e2;
|
|
5529
5706
|
if (!this.inThread.has("face")) console.warn("mediakit: no detect worker for faces, detecting in this thread", e2);
|
|
@@ -5546,7 +5723,7 @@
|
|
|
5546
5723
|
}
|
|
5547
5724
|
/** The detect worker for `kind`, made on first use; rejects when that kind must stay in-thread. */
|
|
5548
5725
|
personRemoteWanted(filter, effects) {
|
|
5549
|
-
return filter.kind === "none" && needsSegmentation(filter, effects)
|
|
5726
|
+
return filter.kind === "none" && needsSegmentation(filter, effects);
|
|
5550
5727
|
}
|
|
5551
5728
|
ensurePersonRemote() {
|
|
5552
5729
|
if (this.personRemote || this.failed.has("person") || this.closed) return Promise.resolve();
|
|
@@ -5584,14 +5761,17 @@
|
|
|
5584
5761
|
}
|
|
5585
5762
|
detectHandlers(key) {
|
|
5586
5763
|
return {
|
|
5587
|
-
face: (tsUs,
|
|
5588
|
-
if (this.closed || !this.lmBuf) return;
|
|
5589
|
-
this.lmBuf.set(lm);
|
|
5590
|
-
const [lo2, hi2] = splitMicros(tsUs);
|
|
5591
|
-
this.core.setFace(lo2, hi2, n2);
|
|
5764
|
+
face: (tsUs, found, lm, ms2, foundCover, foundRects) => {
|
|
5765
|
+
if (this.closed || !this.lmBuf || tsUs > this.prevTs) return;
|
|
5592
5766
|
this.faceWorkerMs = ms2;
|
|
5593
|
-
this.
|
|
5594
|
-
const
|
|
5767
|
+
this.uncrop(tsUs, found, lm, foundRects);
|
|
5768
|
+
const { n: n2, cover, rects } = this.dedupe(found, lm, foundCover, foundRects);
|
|
5769
|
+
this.follow(tsUs, n2, lm, this.probes.delete(tsUs));
|
|
5770
|
+
if (this.syncing()) {
|
|
5771
|
+
this.faceResults.push({ ts: tsUs, n: n2, lm: lm.slice(), cover, rects });
|
|
5772
|
+
if (this.faceResults.length > FACE_RESULTS_KEPT) this.faceResults.shift();
|
|
5773
|
+
} else this.applyFace(tsUs, n2, lm, cover, rects);
|
|
5774
|
+
const took = this.took(key === "faceExtra" ? "faceExtra" : "face", tsUs);
|
|
5595
5775
|
if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
|
|
5596
5776
|
},
|
|
5597
5777
|
mask: (tsUs, width, height, data, ms2) => {
|
|
@@ -5601,24 +5781,30 @@
|
|
|
5601
5781
|
const was = this.maskLatencyMs.get(key);
|
|
5602
5782
|
this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
|
|
5603
5783
|
}
|
|
5604
|
-
|
|
5605
|
-
|
|
5606
|
-
this.
|
|
5607
|
-
this.personMaskTs = tsUs;
|
|
5608
|
-
this.personWorkerMs = ms2;
|
|
5784
|
+
const r2 = { person: key === "person", ts: tsUs, width, height, data, ms: ms2 };
|
|
5785
|
+
if (!this.syncing()) {
|
|
5786
|
+
this.applyMask(r2);
|
|
5609
5787
|
return;
|
|
5610
5788
|
}
|
|
5611
|
-
this.
|
|
5612
|
-
this.
|
|
5613
|
-
this.maskWorkerMs = ms2;
|
|
5789
|
+
this.maskResults.push(r2);
|
|
5790
|
+
if (this.maskResults.length > MASK_RESULTS_KEPT) this.maskResults.shift();
|
|
5614
5791
|
},
|
|
5615
5792
|
failed: (k2, reason) => {
|
|
5793
|
+
if (key === "faceExtra") {
|
|
5794
|
+
this.faceExtraUp = false;
|
|
5795
|
+
this.detects.get(key)?.close();
|
|
5796
|
+
this.detects.delete(key);
|
|
5797
|
+
return;
|
|
5798
|
+
}
|
|
5616
5799
|
console.warn(`mediakit: ${key} detection moves into this thread:`, reason);
|
|
5617
5800
|
this.inThread.add(key);
|
|
5618
5801
|
if (key === "person") {
|
|
5619
5802
|
this.personRemote = false;
|
|
5620
5803
|
} else if (k2 === "face") {
|
|
5621
5804
|
this.faceRemote = false;
|
|
5805
|
+
if (this.coverOn) this.core.setFaceCover(null);
|
|
5806
|
+
this.coverOn = false;
|
|
5807
|
+
this.coverShown = null;
|
|
5622
5808
|
void this.ensureFace();
|
|
5623
5809
|
} else {
|
|
5624
5810
|
this.classRemote = false;
|
|
@@ -5627,6 +5813,24 @@
|
|
|
5627
5813
|
}
|
|
5628
5814
|
};
|
|
5629
5815
|
}
|
|
5816
|
+
noteThumb(timestampUs) {
|
|
5817
|
+
if (this.thumbs.has(timestampUs) || !this.detectCanvas || this.detectCanvasTs !== timestampUs) return;
|
|
5818
|
+
this.thumbCanvas ??= new OffscreenCanvas(THUMB_W, THUMB_H);
|
|
5819
|
+
const ctx = this.thumbCanvas.getContext("2d", { willReadFrequently: true });
|
|
5820
|
+
ctx.drawImage(this.detectCanvas, 0, 0, THUMB_W, THUMB_H);
|
|
5821
|
+
this.thumbs.set(timestampUs, lumaThumb(ctx.getImageData(0, 0, THUMB_W, THUMB_H).data));
|
|
5822
|
+
if (this.thumbs.size > THUMBS_KEPT) this.thumbs.delete(this.thumbs.keys().next().value);
|
|
5823
|
+
}
|
|
5824
|
+
merge(tsUs) {
|
|
5825
|
+
if (!this.personBytes || !this.classBytes || !needsSkinClasses(this.filter, this.effects)) return;
|
|
5826
|
+
const then = this.thumbs.get(this.classMaskTs);
|
|
5827
|
+
const now = this.thumbs.get(this.personMaskTs);
|
|
5828
|
+
const moved = then && now && this.classMaskTs !== this.personMaskTs ? movedBetween(then, now) : null;
|
|
5829
|
+
this.mergedBytes = mergeMasks(this.personBytes, this.classBytes, this.mergedBytes, moved);
|
|
5830
|
+
this.mergedTex = this.uploadMaskBytes(this.mergedTex, this.personBytes.width, this.personBytes.height, this.mergedBytes);
|
|
5831
|
+
this.mergedFresh = true;
|
|
5832
|
+
this.mergedTs = tsUs;
|
|
5833
|
+
}
|
|
5630
5834
|
uploadClassMask(width, height, data) {
|
|
5631
5835
|
this.classMaskTex = this.uploadMaskBytes(this.classMaskTex, width, height, data);
|
|
5632
5836
|
this.classMaskFresh = true;
|
|
@@ -5653,23 +5857,186 @@
|
|
|
5653
5857
|
* Sends the detect worker a downscaled copy, never a clone: a camera frame held through a slow
|
|
5654
5858
|
* detection drains Chrome's few capture buffers and throttles the camera itself.
|
|
5655
5859
|
*/
|
|
5656
|
-
requestRemote(
|
|
5860
|
+
requestRemote(want, source, timestampUs) {
|
|
5861
|
+
const lockstep = want === "face" && this.syncing();
|
|
5862
|
+
const kind = lockstep ? this.freeFaceWorker() : want;
|
|
5863
|
+
if (!kind) return;
|
|
5657
5864
|
const detect = this.detects.get(kind);
|
|
5658
|
-
if (!detect?.accepts(kind === "face" ? "face" : "mask")) return;
|
|
5659
|
-
if (!this.dutyAllows(kind)) return;
|
|
5865
|
+
if (!detect?.accepts(kind === "face" || kind === "faceExtra" ? "face" : "mask")) return;
|
|
5866
|
+
if (!lockstep && !this.dutyAllows(kind)) return;
|
|
5660
5867
|
let frame = null;
|
|
5868
|
+
let followed = null;
|
|
5869
|
+
let scanned = null;
|
|
5661
5870
|
try {
|
|
5662
5871
|
const copyable = source instanceof VideoFrame || source instanceof OffscreenCanvas;
|
|
5663
|
-
|
|
5872
|
+
if (copyable) {
|
|
5873
|
+
this.srcW = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
5874
|
+
this.srcH = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
5875
|
+
}
|
|
5876
|
+
if (copyable && (kind === "face" || kind === "faceExtra")) {
|
|
5877
|
+
followed = this.followCopy(source, timestampUs);
|
|
5878
|
+
if (!followed) scanned = this.scanCopy(source, timestampUs);
|
|
5879
|
+
}
|
|
5880
|
+
frame = new VideoFrame(followed ?? scanned ?? (copyable ? this.detectCopy(source, timestampUs) : source), {
|
|
5664
5881
|
timestamp: timestampUs
|
|
5665
5882
|
});
|
|
5666
5883
|
} catch {
|
|
5667
5884
|
}
|
|
5668
|
-
if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
|
|
5885
|
+
if (frame && detect.request(kind === "face" || kind === "faceExtra" ? "face" : "mask", frame, timestampUs, this.effects.occluders ?? false, followed ? this.followRetry : null)) {
|
|
5669
5886
|
const now = performance.now();
|
|
5670
5887
|
this.sent.set(kind, { ts: timestampUs, at: now });
|
|
5671
5888
|
this.lastSentAt.set(kind, now);
|
|
5889
|
+
if (lockstep) this.faceAsked.add(timestampUs);
|
|
5890
|
+
if (kind !== "face" && kind !== "faceExtra" && this.remoteMaskKind() === "merged") this.noteThumb(timestampUs);
|
|
5891
|
+
} else if (followed || scanned) this.faceCrops.delete(timestampUs);
|
|
5892
|
+
}
|
|
5893
|
+
followCopy(source, timestampUs) {
|
|
5894
|
+
const box = this.followBox;
|
|
5895
|
+
if (!box || timestampUs - this.followTs > FOLLOW_STALE_US) return null;
|
|
5896
|
+
const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
5897
|
+
const sh2 = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
5898
|
+
const want = Math.max(FOLLOW_MIN_SIDE, FOLLOW_MARGIN * (1 + this.followMiss) * Math.max(box[2] * sw, box[3] * sh2));
|
|
5899
|
+
if (want > Math.min(sw, sh2) && this.followFloats > FACE_FLOATS) return null;
|
|
5900
|
+
if (!this.followAll && this.followTurn++ % FOLLOW_REFRESH === FOLLOW_REFRESH - 1) {
|
|
5901
|
+
this.probes.add(timestampUs);
|
|
5902
|
+
return null;
|
|
5903
|
+
}
|
|
5904
|
+
let side = Math.round(Math.min(sw, sh2, want));
|
|
5905
|
+
let x2 = Math.round(Math.min(sw - side, Math.max(0, (box[0] + box[2] / 2) * sw - side / 2)));
|
|
5906
|
+
let y2 = Math.round(Math.min(sh2 - side, Math.max(0, (box[1] + box[3] / 2) * sh2 - side / 2)));
|
|
5907
|
+
const kept = this.followCrop;
|
|
5908
|
+
if (kept && kept[0] + kept[2] <= sw && kept[1] + kept[2] <= sh2 && Math.abs(kept[2] - side) < FOLLOW_RESIZE * side && Math.abs(kept[0] - x2) < FOLLOW_SHIFT * side && Math.abs(kept[1] - y2) < FOLLOW_SHIFT * side) {
|
|
5909
|
+
;
|
|
5910
|
+
[x2, y2, side] = kept;
|
|
5911
|
+
} else this.followCrop = [x2, y2, side];
|
|
5912
|
+
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5913
|
+
this.followCanvas.getContext("2d").drawImage(source, x2, y2, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5914
|
+
const tight = Math.min(side, Math.max(FOLLOW_MIN_SIDE, FOLLOW_RETRY * Math.max(box[2] * sw, box[3] * sh2)));
|
|
5915
|
+
this.followRetry = [((box[0] + box[2] / 2) * sw - x2) / side, ((box[1] + box[3] / 2) * sh2 - y2) / side, tight / side];
|
|
5916
|
+
this.faceCrops.set(timestampUs, [x2 / sw, y2 / sh2, side / sw, side / sh2]);
|
|
5917
|
+
return this.followCanvas;
|
|
5918
|
+
}
|
|
5919
|
+
scanCopy(source, timestampUs) {
|
|
5920
|
+
if (this.faceSeen) return null;
|
|
5921
|
+
const turn = this.scanTurn++;
|
|
5922
|
+
if (turn % 2 === 0) return null;
|
|
5923
|
+
const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
|
|
5924
|
+
const sh2 = source instanceof VideoFrame ? source.displayHeight : source.height;
|
|
5925
|
+
const { side, step, nx, ny } = tileGrid(sw, sh2);
|
|
5926
|
+
const at2 = (this.scanFrom + (turn >> 1)) % (nx * ny);
|
|
5927
|
+
const x2 = Math.max(0, Math.min(sw - side, Math.round(at2 % nx * step)));
|
|
5928
|
+
const y2 = Math.max(0, Math.min(sh2 - side, Math.round(Math.floor(at2 / nx) * step)));
|
|
5929
|
+
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5930
|
+
this.followCanvas.getContext("2d").drawImage(source, x2, y2, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5931
|
+
this.faceCrops.set(timestampUs, [x2 / sw, y2 / sh2, side / sw, side / sh2]);
|
|
5932
|
+
this.probes.add(timestampUs);
|
|
5933
|
+
return this.followCanvas;
|
|
5934
|
+
}
|
|
5935
|
+
scanTile(cx, cy) {
|
|
5936
|
+
const sw = this.srcW || this.canvas.width;
|
|
5937
|
+
const sh2 = this.srcH || this.canvas.height;
|
|
5938
|
+
const { side, step, nx, ny } = tileGrid(sw, sh2);
|
|
5939
|
+
const ix = Math.min(nx - 1, Math.max(0, Math.round((cx * sw - side / 2) / step)));
|
|
5940
|
+
const iy = Math.min(ny - 1, Math.max(0, Math.round((cy * sh2 - side / 2) / step)));
|
|
5941
|
+
return iy * nx + ix;
|
|
5942
|
+
}
|
|
5943
|
+
uncrop(tsUs, n2, lm, rects) {
|
|
5944
|
+
const crop = this.faceCrops.get(tsUs);
|
|
5945
|
+
for (const ts2 of this.faceCrops.keys()) if (ts2 === tsUs || ts2 < tsUs - FOLLOW_KEPT_US) this.faceCrops.delete(ts2);
|
|
5946
|
+
if (!crop) return;
|
|
5947
|
+
for (let i2 = 0; i2 < n2; i2 += 3) {
|
|
5948
|
+
lm[i2] = crop[0] + lm[i2] * crop[2];
|
|
5949
|
+
lm[i2 + 1] = crop[1] + lm[i2 + 1] * crop[3];
|
|
5950
|
+
lm[i2 + 2] = lm[i2 + 2] * crop[2];
|
|
5672
5951
|
}
|
|
5952
|
+
for (let i2 = 0; rects && i2 < rects.length; i2 += 3) {
|
|
5953
|
+
rects[i2] = crop[0] + rects[i2] * crop[2];
|
|
5954
|
+
rects[i2 + 1] = crop[1] + rects[i2 + 1] * crop[3];
|
|
5955
|
+
rects[i2 + 2] = rects[i2 + 2] * crop[2];
|
|
5956
|
+
}
|
|
5957
|
+
}
|
|
5958
|
+
dedupe(n2, lm, cover, rects) {
|
|
5959
|
+
const faces = n2 / FACE_FLOATS;
|
|
5960
|
+
if (faces < 2) return { n: n2, cover, rects };
|
|
5961
|
+
const boxes = Array.from({ length: faces }, (_2, f2) => faceBox(lm, f2 * FACE_FLOATS));
|
|
5962
|
+
const ref = this.followBox;
|
|
5963
|
+
const score = (b2) => (ref ? overlap(b2, ref) : 0) + b2[2] * b2[3] * 1e-3;
|
|
5964
|
+
const keep = boxes.map(() => true);
|
|
5965
|
+
for (let a2 = 0; a2 < faces; a2++) {
|
|
5966
|
+
for (let b2 = a2 + 1; b2 < faces; b2++) {
|
|
5967
|
+
if (!keep[a2] || !keep[b2] || overlap(boxes[a2], boxes[b2]) <= SAME_FACE) continue;
|
|
5968
|
+
keep[score(boxes[a2]) >= score(boxes[b2]) ? b2 : a2] = false;
|
|
5969
|
+
}
|
|
5970
|
+
}
|
|
5971
|
+
if (keep.every(Boolean)) return { n: n2, cover, rects };
|
|
5972
|
+
const cells = COVER_SIZE * COVER_SIZE;
|
|
5973
|
+
const outCover = cover ? new Uint8Array(cover.length) : null;
|
|
5974
|
+
const outRects = rects ? new Float32Array(rects.length) : null;
|
|
5975
|
+
let k2 = 0;
|
|
5976
|
+
for (let f2 = 0; f2 < faces; f2++) {
|
|
5977
|
+
if (!keep[f2]) continue;
|
|
5978
|
+
if (k2 !== f2) lm.copyWithin(k2 * FACE_FLOATS, f2 * FACE_FLOATS, (f2 + 1) * FACE_FLOATS);
|
|
5979
|
+
if (cover && outCover) outCover.set(cover.subarray(f2 * cells, (f2 + 1) * cells), k2 * cells);
|
|
5980
|
+
if (rects && outRects) outRects.set(rects.subarray(f2 * 3, f2 * 3 + 3), k2 * 3);
|
|
5981
|
+
k2++;
|
|
5982
|
+
}
|
|
5983
|
+
return { n: k2 * FACE_FLOATS, cover: outCover ? outCover.slice(0, k2 * cells) : null, rects: outRects ? outRects.slice(0, k2 * 3) : null };
|
|
5984
|
+
}
|
|
5985
|
+
follow(tsUs, n2, lm, probe) {
|
|
5986
|
+
for (const ts2 of this.probes) if (ts2 < tsUs - FOLLOW_KEPT_US) this.probes.delete(ts2);
|
|
5987
|
+
if (tsUs <= this.followTs || probe && (n2 === 0 || n2 < this.followFloats)) return;
|
|
5988
|
+
this.followTs = tsUs;
|
|
5989
|
+
this.faceSeen = n2 > 0;
|
|
5990
|
+
if (n2 === 0) {
|
|
5991
|
+
this.followGrow = 0;
|
|
5992
|
+
this.followFloats = 0;
|
|
5993
|
+
this.followAll = false;
|
|
5994
|
+
const box = this.followBox;
|
|
5995
|
+
if (box && this.followMiss < FOLLOW_WIDEN) {
|
|
5996
|
+
this.followMiss++;
|
|
5997
|
+
return;
|
|
5998
|
+
}
|
|
5999
|
+
this.followMiss = 0;
|
|
6000
|
+
if (box) this.scanFrom = this.scanTile(box[0] + box[2] / 2, box[1] + box[3] / 2);
|
|
6001
|
+
this.scanTurn = 1;
|
|
6002
|
+
this.followBox = null;
|
|
6003
|
+
this.followCrop = null;
|
|
6004
|
+
this.boxFloats = 0;
|
|
6005
|
+
return;
|
|
6006
|
+
}
|
|
6007
|
+
this.followMiss = 0;
|
|
6008
|
+
const old = this.followBox;
|
|
6009
|
+
const grow = old !== null && this.boxFloats > 0 && n2 > this.boxFloats && !probe;
|
|
6010
|
+
this.followGrow = grow ? this.followGrow + 1 : 0;
|
|
6011
|
+
const take = grow && this.followGrow < FOLLOW_GROW ? this.boxFloats : n2;
|
|
6012
|
+
const boxes = [];
|
|
6013
|
+
for (let f2 = 0; f2 < n2; f2 += FACE_FLOATS) boxes.push(faceBox(lm, f2));
|
|
6014
|
+
if (take < n2 && old) boxes.sort((a2, b2) => overlap(b2, old) - overlap(a2, old));
|
|
6015
|
+
const kept = boxes.slice(0, take / FACE_FLOATS);
|
|
6016
|
+
const x0 = Math.min(...kept.map((b2) => b2[0]));
|
|
6017
|
+
const y0 = Math.min(...kept.map((b2) => b2[1]));
|
|
6018
|
+
const x1 = Math.max(...kept.map((b2) => b2[0] + b2[2]));
|
|
6019
|
+
const y1 = Math.max(...kept.map((b2) => b2[1] + b2[3]));
|
|
6020
|
+
this.followBox = [x0, y0, x1 - x0, y1 - y0];
|
|
6021
|
+
this.boxFloats = take;
|
|
6022
|
+
this.followFloats = take;
|
|
6023
|
+
this.followAll = take >= this.faceCount * FACE_FLOATS;
|
|
6024
|
+
}
|
|
6025
|
+
freeFaceWorker() {
|
|
6026
|
+
if (this.detects.get("face")?.accepts("face")) return "face";
|
|
6027
|
+
if (this.faceExtraUp && this.detects.get("faceExtra")?.accepts("face")) return "faceExtra";
|
|
6028
|
+
return null;
|
|
6029
|
+
}
|
|
6030
|
+
ensureFaceExtra() {
|
|
6031
|
+
if (this.faceExtraTried || this.closed) return;
|
|
6032
|
+
this.faceExtraTried = true;
|
|
6033
|
+
const n2 = this.faceCount;
|
|
6034
|
+
this.remote("faceExtra", (d2) => d2.initFace(n2)).then(() => {
|
|
6035
|
+
this.faceExtraUp = true;
|
|
6036
|
+
}).catch(() => {
|
|
6037
|
+
this.detects.get("faceExtra")?.close();
|
|
6038
|
+
this.detects.delete("faceExtra");
|
|
6039
|
+
});
|
|
5673
6040
|
}
|
|
5674
6041
|
/** Face and mask asked on the same frame share one draw. */
|
|
5675
6042
|
detectCopy(source, timestampUs) {
|
|
@@ -5750,45 +6117,99 @@
|
|
|
5750
6117
|
if (this.prevTs >= 0 && timestampUs > this.prevTs) {
|
|
5751
6118
|
this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
|
|
5752
6119
|
}
|
|
6120
|
+
if (timestampUs < this.prevTs) {
|
|
6121
|
+
this.faceResults = [];
|
|
6122
|
+
this.maskResults = [];
|
|
6123
|
+
this.faceAsked.clear();
|
|
6124
|
+
this.faceCrops.clear();
|
|
6125
|
+
this.followBox = null;
|
|
6126
|
+
this.followCrop = null;
|
|
6127
|
+
this.followTs = -Infinity;
|
|
6128
|
+
this.faceSeen = false;
|
|
6129
|
+
this.followFloats = 0;
|
|
6130
|
+
this.followAll = false;
|
|
6131
|
+
this.followTurn = 0;
|
|
6132
|
+
this.followMiss = 0;
|
|
6133
|
+
this.followGrow = 0;
|
|
6134
|
+
this.boxFloats = 0;
|
|
6135
|
+
this.scanFrom = 0;
|
|
6136
|
+
this.scanTurn = 0;
|
|
6137
|
+
this.probes.clear();
|
|
6138
|
+
this.caughtUpTs = -Infinity;
|
|
6139
|
+
this.thumbs.clear();
|
|
6140
|
+
}
|
|
5753
6141
|
this.prevTs = timestampUs;
|
|
5754
6142
|
if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
|
|
5755
6143
|
this.releaseHeld();
|
|
5756
6144
|
this.renderedTs = timestampUs;
|
|
5757
6145
|
return this.draw(source, timestampUs, false);
|
|
5758
6146
|
}
|
|
6147
|
+
this.ensureFaceExtra();
|
|
5759
6148
|
this.requestRemote("face", source, timestampUs);
|
|
5760
6149
|
const remoteMask = this.remoteMaskKind();
|
|
5761
|
-
if (remoteMask
|
|
6150
|
+
if (remoteMask === "merged") {
|
|
6151
|
+
this.requestRemote("person", source, timestampUs);
|
|
6152
|
+
this.requestRemote("mask", source, timestampUs);
|
|
6153
|
+
} else if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
|
|
5762
6154
|
this.queue.push({ c: this.hold(source), ts: timestampUs });
|
|
5763
|
-
this.
|
|
5764
|
-
if (this.queue.length <= this.
|
|
6155
|
+
while (this.queue.length > MAX_HOLD + 1) this.faceAsked.delete(this.spareFront());
|
|
6156
|
+
if (this.queue.length <= MAX_HOLD && !this.faceReady(this.queue[0].ts)) {
|
|
5765
6157
|
this.swallowed = true;
|
|
5766
6158
|
return false;
|
|
5767
6159
|
}
|
|
5768
|
-
|
|
6160
|
+
if (this.queue.length > 2 && this.faceReady(this.queue[1].ts) && timestampUs - this.caughtUpTs >= CATCH_UP_EVERY_US) {
|
|
6161
|
+
this.caughtUpTs = timestampUs;
|
|
6162
|
+
this.faceAsked.delete(this.spareFront());
|
|
6163
|
+
}
|
|
5769
6164
|
const front = this.queue.shift();
|
|
6165
|
+
this.faceAsked.delete(front.ts);
|
|
6166
|
+
this.faceFor(front.ts);
|
|
6167
|
+
this.masksFor(front.ts);
|
|
5770
6168
|
this.lastHeld = front.c;
|
|
5771
6169
|
this.renderedTs = front.ts;
|
|
5772
6170
|
return this.draw(front.c, front.ts, true);
|
|
5773
6171
|
}
|
|
5774
6172
|
remoteMaskKind() {
|
|
5775
|
-
|
|
5776
|
-
if (
|
|
5777
|
-
return
|
|
5778
|
-
|
|
5779
|
-
|
|
5780
|
-
|
|
5781
|
-
const
|
|
5782
|
-
|
|
5783
|
-
|
|
5784
|
-
|
|
6173
|
+
const person = this.personRemote && this.personRemoteWanted(this.filter, this.effects);
|
|
6174
|
+
if (!needsSkinClasses(this.filter, this.effects)) return person ? "person" : null;
|
|
6175
|
+
if (this.classRemote) return person ? "merged" : "mask";
|
|
6176
|
+
return person && !this.inThread.has("mask") ? "person" : null;
|
|
6177
|
+
}
|
|
6178
|
+
spareFront() {
|
|
6179
|
+
const h2 = this.queue.shift();
|
|
6180
|
+
this.spare.push(h2.c);
|
|
6181
|
+
return h2.ts;
|
|
6182
|
+
}
|
|
6183
|
+
applyMask(r2) {
|
|
6184
|
+
if (r2.person) {
|
|
6185
|
+
this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, r2.width, r2.height, r2.data);
|
|
6186
|
+
this.personMaskFresh = true;
|
|
6187
|
+
this.personMaskTs = r2.ts;
|
|
6188
|
+
this.personWorkerMs = r2.ms;
|
|
6189
|
+
this.personBytes = { width: r2.width, height: r2.height, data: r2.data };
|
|
6190
|
+
this.merge(r2.ts);
|
|
5785
6191
|
return;
|
|
5786
6192
|
}
|
|
5787
|
-
|
|
5788
|
-
|
|
5789
|
-
|
|
6193
|
+
this.uploadClassMask(r2.width, r2.height, r2.data);
|
|
6194
|
+
this.classMaskTs = r2.ts;
|
|
6195
|
+
this.maskWorkerMs = r2.ms;
|
|
6196
|
+
this.classBytes = { width: r2.width, height: r2.height, data: r2.data };
|
|
6197
|
+
this.merge(this.personMaskTs);
|
|
6198
|
+
}
|
|
6199
|
+
masksFor(tsUs) {
|
|
6200
|
+
for (const person of [false, true]) {
|
|
6201
|
+
let pick = null;
|
|
6202
|
+
for (const r2 of this.maskResults) if (r2.person === person && (!pick || Math.abs(r2.ts - tsUs) < Math.abs(pick.ts - tsUs))) pick = r2;
|
|
6203
|
+
if (!pick) continue;
|
|
6204
|
+
const chosen = pick;
|
|
6205
|
+
this.maskResults = this.maskResults.filter((r2) => r2.person !== person || r2.ts >= chosen.ts);
|
|
6206
|
+
if (chosen.ts !== (person ? this.personMaskTs : this.classMaskTs)) this.applyMask(chosen);
|
|
5790
6207
|
}
|
|
5791
6208
|
}
|
|
6209
|
+
faceReady(tsUs) {
|
|
6210
|
+
const asked = this.faceAsked.has(tsUs);
|
|
6211
|
+
return this.faceResults.some((r2) => asked ? r2.ts === tsUs : r2.ts > tsUs);
|
|
6212
|
+
}
|
|
5792
6213
|
dutyAllows(kind) {
|
|
5793
6214
|
const took = kind === "face" ? this.faceLatencyMs : this.maskLatencyMs.get(kind) ?? 0;
|
|
5794
6215
|
if (took * 1e3 < this.gapUs) return true;
|
|
@@ -5801,6 +6222,89 @@
|
|
|
5801
6222
|
this.sent.delete(kind);
|
|
5802
6223
|
return performance.now() - s2.at;
|
|
5803
6224
|
}
|
|
6225
|
+
applyFace(tsUs, n2, lm, cover, rects) {
|
|
6226
|
+
this.faceMisses = n2 === 0 ? this.faceMisses + 1 : 0;
|
|
6227
|
+
if (n2 > 0) this.showCover(cover, rects);
|
|
6228
|
+
else if (this.faceMisses >= COVER_MISSES) this.hideCover();
|
|
6229
|
+
this.lmBuf.set(lm);
|
|
6230
|
+
const [lo2, hi2] = splitMicros(tsUs);
|
|
6231
|
+
this.core.setFace(lo2, hi2, n2);
|
|
6232
|
+
this.faceTs = tsUs;
|
|
6233
|
+
}
|
|
6234
|
+
faceFor(tsUs) {
|
|
6235
|
+
this.faceResults.sort((a3, b2) => a3.ts - b2.ts);
|
|
6236
|
+
let pick = -1;
|
|
6237
|
+
for (let i2 = 0; i2 < this.faceResults.length; i2++) if (this.faceResults[i2].ts <= tsUs) pick = i2;
|
|
6238
|
+
if (pick < 0) return;
|
|
6239
|
+
const r2 = this.faceResults[pick];
|
|
6240
|
+
const next = this.faceResults[pick + 1];
|
|
6241
|
+
this.faceResults.splice(0, pick);
|
|
6242
|
+
if (r2.ts === tsUs || !next || !this.sameFaces(r2, next)) {
|
|
6243
|
+
if (r2.ts !== this.faceTs) this.applyFace(r2.ts, r2.n, r2.lm, r2.cover, r2.rects);
|
|
6244
|
+
return;
|
|
6245
|
+
}
|
|
6246
|
+
const a2 = (tsUs - r2.ts) / (next.ts - r2.ts);
|
|
6247
|
+
if (!this.lerpBuf || this.lerpBuf.length !== r2.lm.length) this.lerpBuf = new Float32Array(r2.lm.length);
|
|
6248
|
+
for (let i2 = 0; i2 < r2.n; i2++) this.lerpBuf[i2] = r2.lm[i2] + (next.lm[i2] - r2.lm[i2]) * a2;
|
|
6249
|
+
const near = a2 < 0.5 ? r2 : next;
|
|
6250
|
+
this.applyFace(tsUs, r2.n, this.lerpBuf, near.cover, near.rects);
|
|
6251
|
+
}
|
|
6252
|
+
hideCover() {
|
|
6253
|
+
if (!this.coverOn || !this.coverCanvas || this.coverHidden) return;
|
|
6254
|
+
const ctx = this.coverCanvas.getContext("2d");
|
|
6255
|
+
ctx.fillStyle = "#000";
|
|
6256
|
+
ctx.fillRect(0, 0, this.coverCanvas.width, this.coverCanvas.height);
|
|
6257
|
+
this.core.setFaceCover(this.coverCanvas);
|
|
6258
|
+
this.coverShown = null;
|
|
6259
|
+
this.coverHidden = true;
|
|
6260
|
+
}
|
|
6261
|
+
showCover(cover, rects) {
|
|
6262
|
+
this.coverHidden = false;
|
|
6263
|
+
if (!cover || !rects || !(this.effects.occluders ?? false)) {
|
|
6264
|
+
if (this.coverOn) this.core.setFaceCover(null);
|
|
6265
|
+
this.coverOn = false;
|
|
6266
|
+
this.coverShown = null;
|
|
6267
|
+
return;
|
|
6268
|
+
}
|
|
6269
|
+
if (cover === this.coverShown && this.coverOn) return;
|
|
6270
|
+
const w2 = COVER_FRAME_W;
|
|
6271
|
+
const h2 = Math.max(1, Math.round(w2 * (this.srcH || this.canvas.height) / (this.srcW || this.canvas.width)));
|
|
6272
|
+
if (!this.coverCanvas || this.coverCanvas.height !== h2) this.coverCanvas = new OffscreenCanvas(w2, h2);
|
|
6273
|
+
this.coverCell ??= new OffscreenCanvas(COVER_SIZE, COVER_SIZE);
|
|
6274
|
+
const ctx = this.coverCanvas.getContext("2d");
|
|
6275
|
+
const cell = this.coverCell.getContext("2d");
|
|
6276
|
+
ctx.fillStyle = "#fff";
|
|
6277
|
+
ctx.fillRect(0, 0, w2, h2);
|
|
6278
|
+
this.coverImg ??= cell.createImageData(COVER_SIZE, COVER_SIZE);
|
|
6279
|
+
const img = this.coverImg;
|
|
6280
|
+
const cells = COVER_SIZE * COVER_SIZE;
|
|
6281
|
+
const held = this.coverLast && this.coverLast !== cover && this.coverLast.length === cover.length ? this.coverLast : null;
|
|
6282
|
+
for (let f2 = 0; f2 * 3 < rects.length; f2++) {
|
|
6283
|
+
for (let i2 = 0; i2 < cells; i2++) {
|
|
6284
|
+
const now = cover[f2 * cells + i2];
|
|
6285
|
+
const v2 = held ? Math.min(now, now * (1 - COVER_HOLD) + held[f2 * cells + i2] * COVER_HOLD) : now;
|
|
6286
|
+
img.data[i2 * 4] = v2;
|
|
6287
|
+
img.data[i2 * 4 + 1] = v2;
|
|
6288
|
+
img.data[i2 * 4 + 2] = v2;
|
|
6289
|
+
img.data[i2 * 4 + 3] = 255;
|
|
6290
|
+
}
|
|
6291
|
+
cell.putImageData(img, 0, 0);
|
|
6292
|
+
const side = rects[f2 * 3 + 2] * w2;
|
|
6293
|
+
ctx.drawImage(this.coverCell, rects[f2 * 3] * w2 - side / 2, rects[f2 * 3 + 1] * h2 - side / 2, side, side);
|
|
6294
|
+
}
|
|
6295
|
+
this.core.setFaceCover(this.coverCanvas);
|
|
6296
|
+
this.coverOn = true;
|
|
6297
|
+
this.coverLast = cover;
|
|
6298
|
+
this.coverShown = cover;
|
|
6299
|
+
}
|
|
6300
|
+
sameFaces(a2, b2) {
|
|
6301
|
+
if (a2.n === 0 || a2.n !== b2.n) return false;
|
|
6302
|
+
for (let k2 = 0; k2 < a2.n; k2 += FACE_FLOATS) {
|
|
6303
|
+
const nose = k2 + 3;
|
|
6304
|
+
if (Math.abs(a2.lm[nose] - b2.lm[nose]) > FACE_JUMP || Math.abs(a2.lm[nose + 1] - b2.lm[nose + 1]) > FACE_JUMP) return false;
|
|
6305
|
+
}
|
|
6306
|
+
return true;
|
|
6307
|
+
}
|
|
5804
6308
|
releaseHeld() {
|
|
5805
6309
|
for (const h2 of this.queue) this.spare.push(h2.c);
|
|
5806
6310
|
this.queue = [];
|
|
@@ -5834,7 +6338,24 @@
|
|
|
5834
6338
|
this.used = null;
|
|
5835
6339
|
let served = false;
|
|
5836
6340
|
const remoteMask = this.remoteMaskKind();
|
|
5837
|
-
if (remoteMask
|
|
6341
|
+
if (remoteMask === "merged") {
|
|
6342
|
+
if (!faceDone) {
|
|
6343
|
+
this.requestRemote("person", source, timestampUs);
|
|
6344
|
+
this.requestRemote("mask", source, timestampUs);
|
|
6345
|
+
}
|
|
6346
|
+
const tex = this.mergedTex ?? this.personMaskTex;
|
|
6347
|
+
if (tex) {
|
|
6348
|
+
served = true;
|
|
6349
|
+
if (this.mergedTex ? this.mergedFresh : this.personMaskFresh) {
|
|
6350
|
+
this.mergedFresh = false;
|
|
6351
|
+
this.personMaskFresh = false;
|
|
6352
|
+
mask = tex;
|
|
6353
|
+
maskKind = this.mergedTex ? 1 : 0;
|
|
6354
|
+
maskTs = this.mergedTex ? this.mergedTs : this.personMaskTs;
|
|
6355
|
+
segMs = this.personWorkerMs;
|
|
6356
|
+
}
|
|
6357
|
+
}
|
|
6358
|
+
} else if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
|
|
5838
6359
|
if (remoteMask === "person") {
|
|
5839
6360
|
if (this.personMaskTex) {
|
|
5840
6361
|
served = true;
|
|
@@ -5922,7 +6443,7 @@
|
|
|
5922
6443
|
needsFace: s2 ? s2[10] > 0.5 : false,
|
|
5923
6444
|
autoExposure: s2 ? s2[11] : 0,
|
|
5924
6445
|
faces: s2 ? s2[12] : 0,
|
|
5925
|
-
heldFrames: faceDone ? this.
|
|
6446
|
+
heldFrames: faceDone ? this.queue.length : 0,
|
|
5926
6447
|
faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
|
|
5927
6448
|
maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0,
|
|
5928
6449
|
effectError: this.programErr || this.loadErr
|
|
@@ -5957,6 +6478,11 @@
|
|
|
5957
6478
|
if (this.classMaskTex) this.gl.deleteTexture(this.classMaskTex);
|
|
5958
6479
|
if (this.personMaskTex) this.gl.deleteTexture(this.personMaskTex);
|
|
5959
6480
|
this.personMaskTex = null;
|
|
6481
|
+
if (this.mergedTex) this.gl.deleteTexture(this.mergedTex);
|
|
6482
|
+
this.mergedTex = null;
|
|
6483
|
+
this.personBytes = null;
|
|
6484
|
+
this.classBytes = null;
|
|
6485
|
+
this.mergedBytes = null;
|
|
5960
6486
|
this.personRemote = false;
|
|
5961
6487
|
this.classMaskTex = null;
|
|
5962
6488
|
this.lmBuf = null;
|