@tradalab/mediakit 2.2.0 → 2.4.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.
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,13 @@
5096
5100
  faceSize: 0,
5097
5101
  needsFace: false,
5098
5102
  autoExposure: 0,
5099
- faces: 0
5103
+ faces: 0,
5104
+ heldFrames: 0,
5105
+ faceAgeMs: 0,
5106
+ maskAgeMs: 0
5100
5107
  };
5108
+ var MAX_HOLD = 2;
5109
+ var DEPTH_VOTES = 15;
5101
5110
  function sourceSize(source) {
5102
5111
  if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
5103
5112
  if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
@@ -5151,9 +5160,17 @@
5151
5160
  classMaskTs = 0;
5152
5161
  syncFaces = false;
5153
5162
  renderedTs = 0;
5154
- held = [null, null];
5155
- heldTs = -1;
5156
- heldSlot = 0;
5163
+ queue = [];
5164
+ spare = [];
5165
+ holdDepth = 1;
5166
+ depthVotes = 0;
5167
+ gapUs = 33333;
5168
+ prevTs = -1;
5169
+ sent = /* @__PURE__ */ new Map();
5170
+ faceLatencyMs = 0;
5171
+ maskLatencyMs = /* @__PURE__ */ new Map();
5172
+ faceTs = -1;
5173
+ maskUsedTs = -1;
5157
5174
  lastHeld = null;
5158
5175
  swallowed = false;
5159
5176
  maskWorkerMs = 0;
@@ -5235,6 +5252,11 @@
5235
5252
  get outputCanvas() {
5236
5253
  return this.canvas;
5237
5254
  }
5255
+ /** Draws an inner layer instead of the picture until set back to "off". */
5256
+ setDebug(view) {
5257
+ if (this.closed) return;
5258
+ this.core.setDebug(debugCode(view));
5259
+ }
5238
5260
  /**
5239
5261
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
5240
5262
  *
@@ -5383,9 +5405,17 @@
5383
5405
  const [lo2, hi2] = splitMicros(tsUs);
5384
5406
  this.core.setFace(lo2, hi2, n2);
5385
5407
  this.faceWorkerMs = ms2;
5408
+ this.faceTs = tsUs;
5409
+ const took = this.took("face", tsUs);
5410
+ if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
5386
5411
  },
5387
5412
  mask: (tsUs, width, height, data, ms2) => {
5388
5413
  if (this.closed) return;
5414
+ const took = this.took(key, tsUs);
5415
+ if (took !== null) {
5416
+ const was = this.maskLatencyMs.get(key);
5417
+ this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
5418
+ }
5389
5419
  if (key === "person") {
5390
5420
  this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, width, height, data);
5391
5421
  this.personMaskFresh = true;
@@ -5449,7 +5479,9 @@
5449
5479
  });
5450
5480
  } catch {
5451
5481
  }
5452
- if (frame) detect.request(kind === "face" ? "face" : "mask", frame, timestampUs);
5482
+ if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
5483
+ this.sent.set(kind, { ts: timestampUs, at: performance.now() });
5484
+ }
5453
5485
  }
5454
5486
  /** Face and mask asked on the same frame share one draw. */
5455
5487
  detectCopy(source, timestampUs) {
@@ -5518,26 +5550,60 @@
5518
5550
  */
5519
5551
  render(source, timestampUs) {
5520
5552
  if (this.closed) return false;
5553
+ if (this.lastHeld) this.spare.push(this.lastHeld);
5521
5554
  this.lastHeld = null;
5522
5555
  this.swallowed = false;
5556
+ if (this.prevTs >= 0 && timestampUs > this.prevTs) {
5557
+ this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
5558
+ }
5559
+ this.prevTs = timestampUs;
5523
5560
  if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
5524
- this.heldTs = -1;
5561
+ this.releaseHeld();
5525
5562
  this.renderedTs = timestampUs;
5526
5563
  return this.draw(source, timestampUs, false);
5527
5564
  }
5528
5565
  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) {
5566
+ const remoteMask = this.remoteMaskKind();
5567
+ if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
5568
+ this.queue.push({ c: this.hold(source), ts: timestampUs });
5569
+ this.retuneDepth();
5570
+ if (this.queue.length <= this.holdDepth) {
5535
5571
  this.swallowed = true;
5536
5572
  return false;
5537
5573
  }
5538
- this.lastHeld = prev;
5539
- this.renderedTs = prevTs;
5540
- return this.draw(prev, prevTs, true);
5574
+ while (this.queue.length > this.holdDepth + 1) this.spare.push(this.queue.shift().c);
5575
+ const front = this.queue.shift();
5576
+ this.lastHeld = front.c;
5577
+ this.renderedTs = front.ts;
5578
+ return this.draw(front.c, front.ts, true);
5579
+ }
5580
+ remoteMaskKind() {
5581
+ if (this.personRemote && this.personRemoteWanted(this.filter, this.effects)) return "person";
5582
+ if (this.classRemote && needsSkinClasses(this.filter, this.effects)) return "mask";
5583
+ return null;
5584
+ }
5585
+ retuneDepth() {
5586
+ const kind = this.remoteMaskKind();
5587
+ const latency = Math.max(this.faceLatencyMs, kind ? this.maskLatencyMs.get(kind) ?? 0 : 0);
5588
+ const want = latency > 0 ? Math.min(MAX_HOLD, Math.max(1, Math.ceil(latency * 1e3 / this.gapUs))) : 1;
5589
+ if (want === this.holdDepth) {
5590
+ this.depthVotes = 0;
5591
+ return;
5592
+ }
5593
+ if (++this.depthVotes >= DEPTH_VOTES) {
5594
+ this.holdDepth = want;
5595
+ this.depthVotes = 0;
5596
+ }
5597
+ }
5598
+ took(kind, tsUs) {
5599
+ const s2 = this.sent.get(kind);
5600
+ if (s2?.ts !== tsUs) return null;
5601
+ this.sent.delete(kind);
5602
+ return performance.now() - s2.at;
5603
+ }
5604
+ releaseHeld() {
5605
+ for (const h2 of this.queue) this.spare.push(h2.c);
5606
+ this.queue = [];
5541
5607
  }
5542
5608
  dropped() {
5543
5609
  return this.swallowed;
@@ -5548,14 +5614,13 @@
5548
5614
  syncing() {
5549
5615
  return this.syncFaces && this.faceRemote && needsFace(this.effects);
5550
5616
  }
5551
- hold(source, slot) {
5617
+ hold(source) {
5552
5618
  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
- }
5619
+ let c2 = this.spare.pop();
5620
+ while (c2 && (c2.width !== w2 || c2.height !== h2)) c2 = this.spare.pop();
5621
+ c2 ??= new OffscreenCanvas(w2, h2);
5558
5622
  c2.getContext("2d").drawImage(source, 0, 0, w2, h2);
5623
+ return c2;
5559
5624
  }
5560
5625
  draw(source, timestampUs, faceDone) {
5561
5626
  const t0 = performance.now();
@@ -5568,8 +5633,9 @@
5568
5633
  let faceMs = 0;
5569
5634
  this.used = null;
5570
5635
  let served = false;
5571
- if (needSeg && this.personRemote && this.personRemoteWanted(this.filter, this.effects)) {
5572
- this.requestRemote("person", source, timestampUs);
5636
+ const remoteMask = this.remoteMaskKind();
5637
+ if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
5638
+ if (remoteMask === "person") {
5573
5639
  if (this.personMaskTex) {
5574
5640
  served = true;
5575
5641
  if (this.personMaskFresh) {
@@ -5580,8 +5646,7 @@
5580
5646
  }
5581
5647
  }
5582
5648
  }
5583
- if (needSeg && !served && this.classRemote && needsSkinClasses(this.filter, this.effects)) {
5584
- this.requestRemote("mask", source, timestampUs);
5649
+ if (remoteMask === "mask") {
5585
5650
  if (this.classMaskTex) {
5586
5651
  served = true;
5587
5652
  if (this.classMaskFresh) {
@@ -5608,9 +5673,11 @@
5608
5673
  }
5609
5674
  if (faceDone) {
5610
5675
  faceMs = this.faceWorkerMs;
5676
+ } else if (this.faceRemote && needsFace(this.effects)) {
5677
+ this.requestRemote("face", source, timestampUs);
5678
+ faceMs = this.faceWorkerMs;
5611
5679
  } else if (this.core.decideFace() === 1) {
5612
5680
  if (this.faceRemote) {
5613
- this.requestRemote("face", source, timestampUs);
5614
5681
  faceMs = this.faceWorkerMs;
5615
5682
  } else if (!this.face) {
5616
5683
  void this.ensureFace();
@@ -5619,8 +5686,10 @@
5619
5686
  const n2 = this.face.detect(source, timestampUs / 1e3, this.lmBuf);
5620
5687
  faceMs = performance.now() - f0;
5621
5688
  this.core.setFace(lo2, hi2, n2);
5689
+ this.faceTs = timestampUs;
5622
5690
  }
5623
5691
  }
5692
+ if (mask) this.maskUsedTs = maskTs;
5624
5693
  const [maskLo, maskHi] = splitMicros(maskTs);
5625
5694
  const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
5626
5695
  if (mask) this.used?.release();
@@ -5644,7 +5713,10 @@
5644
5713
  faceSize: s2 ? s2[9] : 0,
5645
5714
  needsFace: s2 ? s2[10] > 0.5 : false,
5646
5715
  autoExposure: s2 ? s2[11] : 0,
5647
- faces: s2 ? s2[12] : 0
5716
+ faces: s2 ? s2[12] : 0,
5717
+ heldFrames: faceDone ? this.holdDepth : 0,
5718
+ faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
5719
+ maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0
5648
5720
  };
5649
5721
  return glCalls > 0;
5650
5722
  }
@@ -5679,7 +5751,8 @@
5679
5751
  this.personRemote = false;
5680
5752
  this.classMaskTex = null;
5681
5753
  this.lmBuf = null;
5682
- this.held = [null, null];
5754
+ this.queue = [];
5755
+ this.spare = [];
5683
5756
  this.lastHeld = null;
5684
5757
  this.core.close();
5685
5758
  this.background?.close();
@@ -5762,6 +5835,7 @@
5762
5835
  const canvas = new OffscreenCanvas(m2.width, m2.height);
5763
5836
  engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false, m2.maxFaces);
5764
5837
  engine.syncFaces = m2.syncFaces ?? false;
5838
+ engine.setDebug(m2.debug ?? "off");
5765
5839
  abort = new AbortController();
5766
5840
  let n2 = 0;
5767
5841
  let lastPost = performance.now();
@@ -5839,6 +5913,8 @@
5839
5913
  });
5840
5914
  } else if (m2.t === "frames") {
5841
5915
  framesOn = m2.on;
5916
+ } else if (m2.t === "debug") {
5917
+ engine?.setDebug(m2.view);
5842
5918
  } else if (m2.t === "recipe") {
5843
5919
  engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).then(() => {
5844
5920
  if (engine?.loading) post({ t: "stats", stats: engine.last, frames: -1, loading: true });