@tradalab/mediakit 2.2.0 → 2.4.1

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.
Binary file
package/dist/recipe.d.ts CHANGED
@@ -33,6 +33,10 @@ type BgFilter = {
33
33
  /** A colour as red, green, blue, each 0..1. */
34
34
  type Rgb = [number, number, number];
35
35
  /** Where smoothing (`beautify`) may land. */
36
+ /** An inner layer drawn instead of the picture, for tuning: skin weights, the mask, the tracked faces. */
37
+ type DebugView = "off" | "skin" | "mask" | "face";
38
+ declare const DEBUG_VIEWS: readonly DebugView[];
39
+ declare function debugCode(view: DebugView | undefined): number;
36
40
  type SkinScope = "person" | "face";
37
41
  /** Where the colour LUT lands. */
38
42
  type GradeScope = "frame" | "person" | "skin";
@@ -326,4 +330,4 @@ interface CoreRecipe {
326
330
  }
327
331
  declare function coreRecipe(f: BgFilter, e: Effects): CoreRecipe;
328
332
 
329
- export { type BgFilter, type CoreRecipe, EFFECTS_SCHEMA, type Effects, type FieldSpec, GRADE_SCOPE, type GradeScope, MAKEUP_BLEND, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, SKIN_SCOPE, type SkinScope, TOUCH_UP, backgroundMode, coreRecipe, needsFace, needsMask, needsSegmentation, needsSkinClasses, normalizeEffects, touchUp, touchUpAmount };
333
+ export { type BgFilter, type CoreRecipe, DEBUG_VIEWS, type DebugView, EFFECTS_SCHEMA, type Effects, type FieldSpec, GRADE_SCOPE, type GradeScope, MAKEUP_BLEND, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, SKIN_SCOPE, type SkinScope, TOUCH_UP, backgroundMode, coreRecipe, debugCode, needsFace, needsMask, needsSegmentation, needsSkinClasses, normalizeEffects, touchUp, touchUpAmount };
package/dist/recipe.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {
2
+ DEBUG_VIEWS,
2
3
  EFFECTS_SCHEMA,
3
4
  GRADE_SCOPE,
4
5
  MAKEUP_BLEND,
@@ -11,6 +12,7 @@ import {
11
12
  TOUCH_UP,
12
13
  backgroundMode,
13
14
  coreRecipe,
15
+ debugCode,
14
16
  needsFace,
15
17
  needsMask,
16
18
  needsSegmentation,
@@ -18,8 +20,9 @@ import {
18
20
  normalizeEffects,
19
21
  touchUp,
20
22
  touchUpAmount
21
- } from "./chunk-2AD2GLNR.js";
23
+ } from "./chunk-YW75KJJO.js";
22
24
  export {
25
+ DEBUG_VIEWS,
23
26
  EFFECTS_SCHEMA,
24
27
  GRADE_SCOPE,
25
28
  MAKEUP_BLEND,
@@ -32,6 +35,7 @@ export {
32
35
  TOUCH_UP,
33
36
  backgroundMode,
34
37
  coreRecipe,
38
+ debugCode,
35
39
  needsFace,
36
40
  needsMask,
37
41
  needsSegmentation,
package/dist/worker.js CHANGED
@@ -4935,6 +4935,10 @@
4935
4935
  }
4936
4936
 
4937
4937
  // src/recipe.ts
4938
+ var DEBUG_VIEWS = ["off", "skin", "mask", "face"];
4939
+ function debugCode(view) {
4940
+ return Math.max(0, DEBUG_VIEWS.indexOf(view ?? "off"));
4941
+ }
4938
4942
  var NO_EFFECTS = {
4939
4943
  beautify: 0,
4940
4944
  exposure: 0,
@@ -5021,7 +5025,7 @@
5021
5025
  }
5022
5026
  function needsFace(e2) {
5023
5027
  return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.smileLines > 0 || e2.lips > 0 || e2.blush > 0 || e2.iris > 0 || (e2.eyeShadow ?? 0) > 0 || (e2.lipGloss ?? 0) > 0 || (e2.rosy ?? 0) > 0 || e2.skinScope === "face" && (e2.beautify > 0 || hasSkin(e2)) || // Whitening and skin light measure skin on the face; clarity is held to it.
5024
- hasSkin(e2) || e2.grade > 0 && e2.gradeScope === "skin" || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0 || (e2.jaw ?? 0) > 0 || (e2.cheekbone ?? 0) > 0 || (e2.forehead ?? 0) !== 0 || (e2.brow ?? 0) > 0 || (e2.contour ?? 0) > 0 || (e2.highlight ?? 0) > 0 || (e2.makeup ?? 0) > 0 && !!e2.makeupTexture;
5028
+ hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame" || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0 || (e2.jaw ?? 0) > 0 || (e2.cheekbone ?? 0) > 0 || (e2.forehead ?? 0) !== 0 || (e2.brow ?? 0) > 0 || (e2.contour ?? 0) > 0 || (e2.highlight ?? 0) > 0 || (e2.makeup ?? 0) > 0 && !!e2.makeupTexture;
5025
5029
  }
5026
5030
  function coreRecipe(f2, e2) {
5027
5031
  return {
@@ -5096,8 +5100,14 @@
5096
5100
  faceSize: 0,
5097
5101
  needsFace: false,
5098
5102
  autoExposure: 0,
5099
- faces: 0
5100
- };
5103
+ faces: 0,
5104
+ heldFrames: 0,
5105
+ faceAgeMs: 0,
5106
+ maskAgeMs: 0
5107
+ };
5108
+ var MAX_HOLD = 2;
5109
+ var DEPTH_VOTES = 15;
5110
+ var DETECT_DUTY = 0.6;
5101
5111
  function sourceSize(source) {
5102
5112
  if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
5103
5113
  if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
@@ -5151,9 +5161,18 @@
5151
5161
  classMaskTs = 0;
5152
5162
  syncFaces = false;
5153
5163
  renderedTs = 0;
5154
- held = [null, null];
5155
- heldTs = -1;
5156
- heldSlot = 0;
5164
+ queue = [];
5165
+ spare = [];
5166
+ holdDepth = 1;
5167
+ depthVotes = 0;
5168
+ gapUs = 33333;
5169
+ prevTs = -1;
5170
+ sent = /* @__PURE__ */ new Map();
5171
+ lastSentAt = /* @__PURE__ */ new Map();
5172
+ faceLatencyMs = 0;
5173
+ maskLatencyMs = /* @__PURE__ */ new Map();
5174
+ faceTs = -1;
5175
+ maskUsedTs = -1;
5157
5176
  lastHeld = null;
5158
5177
  swallowed = false;
5159
5178
  maskWorkerMs = 0;
@@ -5235,6 +5254,11 @@
5235
5254
  get outputCanvas() {
5236
5255
  return this.canvas;
5237
5256
  }
5257
+ /** Draws an inner layer instead of the picture until set back to "off". */
5258
+ setDebug(view) {
5259
+ if (this.closed) return;
5260
+ this.core.setDebug(debugCode(view));
5261
+ }
5238
5262
  /**
5239
5263
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
5240
5264
  *
@@ -5383,9 +5407,17 @@
5383
5407
  const [lo2, hi2] = splitMicros(tsUs);
5384
5408
  this.core.setFace(lo2, hi2, n2);
5385
5409
  this.faceWorkerMs = ms2;
5410
+ this.faceTs = tsUs;
5411
+ const took = this.took("face", tsUs);
5412
+ if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
5386
5413
  },
5387
5414
  mask: (tsUs, width, height, data, ms2) => {
5388
5415
  if (this.closed) return;
5416
+ const took = this.took(key, tsUs);
5417
+ if (took !== null) {
5418
+ const was = this.maskLatencyMs.get(key);
5419
+ this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
5420
+ }
5389
5421
  if (key === "person") {
5390
5422
  this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, width, height, data);
5391
5423
  this.personMaskFresh = true;
@@ -5441,6 +5473,7 @@
5441
5473
  requestRemote(kind, source, timestampUs) {
5442
5474
  const detect = this.detects.get(kind);
5443
5475
  if (!detect?.accepts(kind === "face" ? "face" : "mask")) return;
5476
+ if (!this.dutyAllows(kind)) return;
5444
5477
  let frame = null;
5445
5478
  try {
5446
5479
  const copyable = source instanceof VideoFrame || source instanceof OffscreenCanvas;
@@ -5449,7 +5482,11 @@
5449
5482
  });
5450
5483
  } catch {
5451
5484
  }
5452
- if (frame) detect.request(kind === "face" ? "face" : "mask", frame, timestampUs);
5485
+ if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
5486
+ const now = performance.now();
5487
+ this.sent.set(kind, { ts: timestampUs, at: now });
5488
+ this.lastSentAt.set(kind, now);
5489
+ }
5453
5490
  }
5454
5491
  /** Face and mask asked on the same frame share one draw. */
5455
5492
  detectCopy(source, timestampUs) {
@@ -5518,26 +5555,66 @@
5518
5555
  */
5519
5556
  render(source, timestampUs) {
5520
5557
  if (this.closed) return false;
5558
+ if (this.lastHeld) this.spare.push(this.lastHeld);
5521
5559
  this.lastHeld = null;
5522
5560
  this.swallowed = false;
5561
+ if (this.prevTs >= 0 && timestampUs > this.prevTs) {
5562
+ this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
5563
+ }
5564
+ this.prevTs = timestampUs;
5523
5565
  if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
5524
- this.heldTs = -1;
5566
+ this.releaseHeld();
5525
5567
  this.renderedTs = timestampUs;
5526
5568
  return this.draw(source, timestampUs, false);
5527
5569
  }
5528
5570
  this.requestRemote("face", source, timestampUs);
5529
- const prev = this.heldTs >= 0 ? this.held[this.heldSlot] : null;
5530
- const prevTs = this.heldTs;
5531
- this.heldSlot ^= 1;
5532
- this.hold(source, this.heldSlot);
5533
- this.heldTs = timestampUs;
5534
- if (!prev) {
5571
+ const remoteMask = this.remoteMaskKind();
5572
+ if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
5573
+ this.queue.push({ c: this.hold(source), ts: timestampUs });
5574
+ this.retuneDepth();
5575
+ if (this.queue.length <= this.holdDepth) {
5535
5576
  this.swallowed = true;
5536
5577
  return false;
5537
5578
  }
5538
- this.lastHeld = prev;
5539
- this.renderedTs = prevTs;
5540
- return this.draw(prev, prevTs, true);
5579
+ while (this.queue.length > this.holdDepth + 1) this.spare.push(this.queue.shift().c);
5580
+ const front = this.queue.shift();
5581
+ this.lastHeld = front.c;
5582
+ this.renderedTs = front.ts;
5583
+ return this.draw(front.c, front.ts, true);
5584
+ }
5585
+ remoteMaskKind() {
5586
+ if (this.personRemote && this.personRemoteWanted(this.filter, this.effects)) return "person";
5587
+ if (this.classRemote && needsSkinClasses(this.filter, this.effects)) return "mask";
5588
+ return null;
5589
+ }
5590
+ retuneDepth() {
5591
+ const kind = this.remoteMaskKind();
5592
+ const latency = Math.max(this.faceLatencyMs, kind ? this.maskLatencyMs.get(kind) ?? 0 : 0);
5593
+ const want = latency > 0 ? Math.min(MAX_HOLD, Math.max(1, Math.ceil(latency * 1e3 / this.gapUs))) : 1;
5594
+ if (want === this.holdDepth) {
5595
+ this.depthVotes = 0;
5596
+ return;
5597
+ }
5598
+ if (++this.depthVotes >= DEPTH_VOTES) {
5599
+ this.holdDepth = want;
5600
+ this.depthVotes = 0;
5601
+ }
5602
+ }
5603
+ dutyAllows(kind) {
5604
+ const took = kind === "face" ? this.faceLatencyMs : this.maskLatencyMs.get(kind) ?? 0;
5605
+ if (took * 1e3 < this.gapUs) return true;
5606
+ const last = this.lastSentAt.get(kind);
5607
+ return last === void 0 || performance.now() - last >= took / DETECT_DUTY;
5608
+ }
5609
+ took(kind, tsUs) {
5610
+ const s2 = this.sent.get(kind);
5611
+ if (s2?.ts !== tsUs) return null;
5612
+ this.sent.delete(kind);
5613
+ return performance.now() - s2.at;
5614
+ }
5615
+ releaseHeld() {
5616
+ for (const h2 of this.queue) this.spare.push(h2.c);
5617
+ this.queue = [];
5541
5618
  }
5542
5619
  dropped() {
5543
5620
  return this.swallowed;
@@ -5548,14 +5625,13 @@
5548
5625
  syncing() {
5549
5626
  return this.syncFaces && this.faceRemote && needsFace(this.effects);
5550
5627
  }
5551
- hold(source, slot) {
5628
+ hold(source) {
5552
5629
  const [w2, h2] = sourceSize(source);
5553
- let c2 = this.held[slot];
5554
- if (!c2 || c2.width !== w2 || c2.height !== h2) {
5555
- c2 = new OffscreenCanvas(w2, h2);
5556
- this.held[slot] = c2;
5557
- }
5630
+ let c2 = this.spare.pop();
5631
+ while (c2 && (c2.width !== w2 || c2.height !== h2)) c2 = this.spare.pop();
5632
+ c2 ??= new OffscreenCanvas(w2, h2);
5558
5633
  c2.getContext("2d").drawImage(source, 0, 0, w2, h2);
5634
+ return c2;
5559
5635
  }
5560
5636
  draw(source, timestampUs, faceDone) {
5561
5637
  const t0 = performance.now();
@@ -5568,8 +5644,9 @@
5568
5644
  let faceMs = 0;
5569
5645
  this.used = null;
5570
5646
  let served = false;
5571
- if (needSeg && this.personRemote && this.personRemoteWanted(this.filter, this.effects)) {
5572
- this.requestRemote("person", source, timestampUs);
5647
+ const remoteMask = this.remoteMaskKind();
5648
+ if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
5649
+ if (remoteMask === "person") {
5573
5650
  if (this.personMaskTex) {
5574
5651
  served = true;
5575
5652
  if (this.personMaskFresh) {
@@ -5580,8 +5657,7 @@
5580
5657
  }
5581
5658
  }
5582
5659
  }
5583
- if (needSeg && !served && this.classRemote && needsSkinClasses(this.filter, this.effects)) {
5584
- this.requestRemote("mask", source, timestampUs);
5660
+ if (remoteMask === "mask") {
5585
5661
  if (this.classMaskTex) {
5586
5662
  served = true;
5587
5663
  if (this.classMaskFresh) {
@@ -5608,9 +5684,11 @@
5608
5684
  }
5609
5685
  if (faceDone) {
5610
5686
  faceMs = this.faceWorkerMs;
5687
+ } else if (this.faceRemote && needsFace(this.effects)) {
5688
+ this.requestRemote("face", source, timestampUs);
5689
+ faceMs = this.faceWorkerMs;
5611
5690
  } else if (this.core.decideFace() === 1) {
5612
5691
  if (this.faceRemote) {
5613
- this.requestRemote("face", source, timestampUs);
5614
5692
  faceMs = this.faceWorkerMs;
5615
5693
  } else if (!this.face) {
5616
5694
  void this.ensureFace();
@@ -5619,8 +5697,10 @@
5619
5697
  const n2 = this.face.detect(source, timestampUs / 1e3, this.lmBuf);
5620
5698
  faceMs = performance.now() - f0;
5621
5699
  this.core.setFace(lo2, hi2, n2);
5700
+ this.faceTs = timestampUs;
5622
5701
  }
5623
5702
  }
5703
+ if (mask) this.maskUsedTs = maskTs;
5624
5704
  const [maskLo, maskHi] = splitMicros(maskTs);
5625
5705
  const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
5626
5706
  if (mask) this.used?.release();
@@ -5644,7 +5724,10 @@
5644
5724
  faceSize: s2 ? s2[9] : 0,
5645
5725
  needsFace: s2 ? s2[10] > 0.5 : false,
5646
5726
  autoExposure: s2 ? s2[11] : 0,
5647
- faces: s2 ? s2[12] : 0
5727
+ faces: s2 ? s2[12] : 0,
5728
+ heldFrames: faceDone ? this.holdDepth : 0,
5729
+ faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
5730
+ maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0
5648
5731
  };
5649
5732
  return glCalls > 0;
5650
5733
  }
@@ -5679,7 +5762,8 @@
5679
5762
  this.personRemote = false;
5680
5763
  this.classMaskTex = null;
5681
5764
  this.lmBuf = null;
5682
- this.held = [null, null];
5765
+ this.queue = [];
5766
+ this.spare = [];
5683
5767
  this.lastHeld = null;
5684
5768
  this.core.close();
5685
5769
  this.background?.close();
@@ -5762,6 +5846,7 @@
5762
5846
  const canvas = new OffscreenCanvas(m2.width, m2.height);
5763
5847
  engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false, m2.maxFaces);
5764
5848
  engine.syncFaces = m2.syncFaces ?? false;
5849
+ engine.setDebug(m2.debug ?? "off");
5765
5850
  abort = new AbortController();
5766
5851
  let n2 = 0;
5767
5852
  let lastPost = performance.now();
@@ -5839,6 +5924,8 @@
5839
5924
  });
5840
5925
  } else if (m2.t === "frames") {
5841
5926
  framesOn = m2.on;
5927
+ } else if (m2.t === "debug") {
5928
+ engine?.setDebug(m2.view);
5842
5929
  } else if (m2.t === "recipe") {
5843
5930
  engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).then(() => {
5844
5931
  if (engine?.loading) post({ t: "stats", stats: engine.last, frames: -1, loading: true });