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