@tradalab/mediakit 1.2.0 → 1.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.
package/dist/worker.js CHANGED
@@ -3993,12 +3993,15 @@
3993
3993
 
3994
3994
  // src/segmenter.ts
3995
3995
  var WASM_DIR = "/mediakit/mp";
3996
- var MODEL_URL = "/mediakit/selfie_segmenter.tflite";
3997
- async function createSegmenter(canvas) {
3996
+ var MODEL_URL = {
3997
+ person: "/mediakit/selfie_segmenter.tflite",
3998
+ classes: "/mediakit/selfie_multiclass_256x256.tflite"
3999
+ };
4000
+ async function createSegmenter(canvas, model = "person") {
3998
4001
  const fileset = await Xo.forVisionTasks(WASM_DIR);
3999
4002
  const segmenter = await Uh.createFromOptions(fileset, {
4000
4003
  baseOptions: {
4001
- modelAssetPath: MODEL_URL,
4004
+ modelAssetPath: MODEL_URL[model],
4002
4005
  // MediaPipe's GPU delegate runs on WebGL2, which Chrome, Safari and Firefox all
4003
4006
  // have. That is why mediakit picked MediaPipe over ONNX Runtime Web at P1: ORT's
4004
4007
  // fast path is WebGPU, which Firefox only has on Windows.
@@ -4154,6 +4157,9 @@
4154
4157
  contrast: 0,
4155
4158
  saturation: 0,
4156
4159
  warmth: 0,
4160
+ tint: 0,
4161
+ vibrance: 0,
4162
+ grain: 0,
4157
4163
  autoframe: false,
4158
4164
  teeth: 0,
4159
4165
  eyeBright: 0,
@@ -4165,8 +4171,18 @@
4165
4171
  blushColor: [0.9, 0.5, 0.42],
4166
4172
  iris: 0,
4167
4173
  irisColor: [0.3, 0.5, 0.75],
4174
+ eyeShadow: 0,
4175
+ eyeShadowColor: [0.55, 0.42, 0.37],
4176
+ lipGloss: 0,
4177
+ rosy: 0,
4178
+ rosyColor: [0.9, 0.42, 0.49],
4179
+ whiten: 0,
4180
+ skinLight: 0,
4181
+ clarity: 0,
4182
+ skinScope: "person",
4168
4183
  lut: "",
4169
4184
  grade: 0,
4185
+ gradeScope: "frame",
4170
4186
  chromaKey: false,
4171
4187
  keyColor: [0, 0, 0],
4172
4188
  keyTolerance: 0,
@@ -4175,12 +4191,26 @@
4175
4191
  slimFace: 0,
4176
4192
  enlargeEyes: 0,
4177
4193
  noseSlim: 0,
4178
- chin: 0
4194
+ chin: 0,
4195
+ jaw: 0,
4196
+ cheekbone: 0,
4197
+ forehead: 0,
4198
+ brow: 0,
4199
+ browColor: [0.36, 0.27, 0.22],
4200
+ contour: 0,
4201
+ highlight: 0,
4202
+ makeupTexture: "",
4203
+ makeup: 0,
4204
+ makeupBlend: "softLight",
4205
+ whitenStyle: "natural"
4179
4206
  };
4180
4207
  var MODE_NONE = 0;
4181
4208
  var MODE_BLUR = 1;
4182
4209
  var MODE_IMAGE = 2;
4183
4210
  var MODE_CUTOUT = 3;
4211
+ var SKIN_SCOPE = { person: 0, face: 1 };
4212
+ var MAKEUP_BLEND = { softLight: 0, multiply: 1, overlay: 2, hardLight: 3, normal: 4 };
4213
+ var GRADE_SCOPE = { frame: 0, person: 1, skin: 2 };
4184
4214
  function backgroundMode(f2) {
4185
4215
  switch (f2.kind) {
4186
4216
  case "blur":
@@ -4194,13 +4224,20 @@
4194
4224
  }
4195
4225
  }
4196
4226
  function needsMask(f2, e2) {
4197
- return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe;
4227
+ return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe || hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
4228
+ }
4229
+ function hasSkin(e2) {
4230
+ return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || (e2.clarity ?? 0) > 0;
4198
4231
  }
4199
4232
  function needsSegmentation(f2, e2) {
4200
4233
  return needsMask(f2, e2) && !e2.chromaKey;
4201
4234
  }
4235
+ function needsSkinClasses(_f, e2) {
4236
+ return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
4237
+ }
4202
4238
  function needsFace(e2) {
4203
- return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.smileLines > 0 || e2.lips > 0 || e2.blush > 0 || e2.iris > 0 || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0;
4239
+ 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.
4240
+ 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;
4204
4241
  }
4205
4242
  function coreRecipe(f2, e2) {
4206
4243
  return {
@@ -4211,6 +4248,9 @@
4211
4248
  contrast: e2.contrast,
4212
4249
  saturation: e2.saturation,
4213
4250
  warmth: e2.warmth,
4251
+ tint: e2.tint ?? 0,
4252
+ vibrance: e2.vibrance ?? 0,
4253
+ grain: e2.grain ?? 0,
4214
4254
  autoframe: e2.autoframe,
4215
4255
  teeth: e2.teeth,
4216
4256
  eyeBright: e2.eyeBright,
@@ -4222,7 +4262,17 @@
4222
4262
  blushColor: e2.blushColor,
4223
4263
  iris: e2.iris,
4224
4264
  irisColor: e2.irisColor,
4265
+ eyeShadow: e2.eyeShadow ?? 0,
4266
+ eyeShadowColor: e2.eyeShadowColor ?? NO_EFFECTS.eyeShadowColor,
4267
+ lipGloss: e2.lipGloss ?? 0,
4268
+ rosy: e2.rosy ?? 0,
4269
+ rosyColor: e2.rosyColor ?? NO_EFFECTS.rosyColor,
4270
+ whiten: e2.whiten ?? 0,
4271
+ skinLight: e2.skinLight ?? 0,
4272
+ clarity: e2.clarity ?? 0,
4273
+ skinScope: SKIN_SCOPE[e2.skinScope] ?? 0,
4225
4274
  grade: e2.grade,
4275
+ gradeScope: GRADE_SCOPE[e2.gradeScope] ?? 0,
4226
4276
  chromaKey: e2.chromaKey,
4227
4277
  keyColor: e2.keyColor,
4228
4278
  keyTolerance: e2.keyTolerance,
@@ -4231,7 +4281,17 @@
4231
4281
  slimFace: e2.slimFace,
4232
4282
  enlargeEyes: e2.enlargeEyes,
4233
4283
  noseSlim: e2.noseSlim,
4234
- chin: e2.chin
4284
+ chin: e2.chin,
4285
+ jaw: e2.jaw ?? 0,
4286
+ cheekbone: e2.cheekbone ?? 0,
4287
+ forehead: e2.forehead ?? 0,
4288
+ brow: e2.brow ?? 0,
4289
+ browColor: e2.browColor ?? NO_EFFECTS.browColor,
4290
+ contour: e2.contour ?? 0,
4291
+ highlight: e2.highlight ?? 0,
4292
+ makeup: e2.makeupTexture ? e2.makeup ?? 0 : 0,
4293
+ makeupBlend: MAKEUP_BLEND[e2.makeupBlend] ?? 0,
4294
+ whitenStyle: e2.whitenStyle === "pink" ? 1 : 0
4235
4295
  };
4236
4296
  }
4237
4297
 
@@ -4265,6 +4325,10 @@
4265
4325
  backgroundPath = null;
4266
4326
  /** The LUT the core holds, by path, so a slider drag does not decode it again. */
4267
4327
  lutPath = "";
4328
+ /** The makeup texture the core holds, by path - see setRecipe. */
4329
+ makeupPath = "";
4330
+ /** Whether the core holds the pink whitening table. */
4331
+ skinLutState = "none";
4268
4332
  closed = false;
4269
4333
  /**
4270
4334
  * The segmenter loads LAZILY - only once a recipe actually needs a person mask.
@@ -4274,6 +4338,11 @@
4274
4338
  */
4275
4339
  segmenter = null;
4276
4340
  segmenterLoading = null;
4341
+ /** The multiclass segmenter, for recipes that tell skin from clothes (`needsSkinClasses`). */
4342
+ classSegmenter = null;
4343
+ classLoading = null;
4344
+ /** The segmenter whose result this frame borrowed, to give it back after process(). */
4345
+ used = null;
4277
4346
  /**
4278
4347
  * The landmark detector, also lazy and independent of the segmenter.
4279
4348
  *
@@ -4296,11 +4365,11 @@
4296
4365
  this.core = core;
4297
4366
  }
4298
4367
  /** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
4299
- static async create(width, height, filter, effects = NO_EFFECTS) {
4368
+ static async create(width, height, filter, effects = NO_EFFECTS, alpha = false) {
4300
4369
  const canvas = document.createElement("canvas");
4301
4370
  canvas.width = width;
4302
4371
  canvas.height = height;
4303
- return _Engine.on(canvas, filter, effects);
4372
+ return _Engine.on(canvas, filter, effects, void 0, alpha);
4304
4373
  }
4305
4374
  /**
4306
4375
  * `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
@@ -4339,6 +4408,9 @@
4339
4408
  throw err;
4340
4409
  }
4341
4410
  }
4411
+ get loading() {
4412
+ return this.segmenterLoading !== null || this.classLoading !== null || this.faceLoading !== null;
4413
+ }
4342
4414
  get outputCanvas() {
4343
4415
  return this.canvas;
4344
4416
  }
@@ -4384,8 +4456,44 @@
4384
4456
  }
4385
4457
  this.lutPath = effects.lut;
4386
4458
  }
4459
+ if (effects.whitenStyle === "pink" && this.skinLutState === "none") {
4460
+ this.skinLutState = "loading";
4461
+ decodeImage("/mediakit/whiten_pink.png", { colorSpaceConversion: "none", premultiplyAlpha: "none" }).then((bmp) => {
4462
+ if (this.closed) {
4463
+ bmp.close();
4464
+ return;
4465
+ }
4466
+ this.core.setSkinLut(bmp);
4467
+ bmp.close();
4468
+ this.skinLutState = "ready";
4469
+ }).catch((err) => {
4470
+ console.error("mediakit: pink whitening table failed to load", err);
4471
+ this.skinLutState = "failed";
4472
+ });
4473
+ }
4474
+ const makeupTexture = effects.makeupTexture ?? "";
4475
+ if (makeupTexture !== this.makeupPath) {
4476
+ if (makeupTexture) {
4477
+ let bmp = null;
4478
+ try {
4479
+ bmp = await decodeImage(makeupTexture, { premultiplyAlpha: "none" });
4480
+ } catch (e2) {
4481
+ console.error("mediakit: makeup texture failed to load", e2);
4482
+ }
4483
+ if (this.closed || this.filter !== filter || this.effects !== effects) {
4484
+ bmp?.close();
4485
+ return;
4486
+ }
4487
+ this.core.setMakeup(bmp);
4488
+ bmp?.close();
4489
+ } else {
4490
+ this.core.setMakeup(null);
4491
+ }
4492
+ this.makeupPath = makeupTexture;
4493
+ }
4387
4494
  this.core.setRecipe(coreRecipe(filter, effects));
4388
4495
  if (needsSegmentation(filter, effects)) void this.ensureSegmenter();
4496
+ if (needsSkinClasses(filter, effects)) void this.ensureClassSegmenter();
4389
4497
  if (needsFace(effects)) void this.ensureFace();
4390
4498
  }
4391
4499
  ensureFace() {
@@ -4407,6 +4515,22 @@
4407
4515
  });
4408
4516
  return this.faceLoading;
4409
4517
  }
4518
+ ensureClassSegmenter() {
4519
+ if (this.classSegmenter || this.closed) return Promise.resolve();
4520
+ if (this.classLoading) return this.classLoading;
4521
+ this.classLoading = createSegmenter(this.canvas, "classes").then((s2) => {
4522
+ if (this.closed) {
4523
+ s2.close();
4524
+ return;
4525
+ }
4526
+ this.classSegmenter = s2;
4527
+ }).catch((e2) => {
4528
+ console.error("mediakit: multiclass segmenter failed to load", e2);
4529
+ }).finally(() => {
4530
+ this.classLoading = null;
4531
+ });
4532
+ return this.classLoading;
4533
+ }
4410
4534
  ensureSegmenter() {
4411
4535
  if (this.segmenter || this.closed) return Promise.resolve();
4412
4536
  if (this.segmenterLoading) return this.segmenterLoading;
@@ -4442,14 +4566,20 @@
4442
4566
  const [lo2, hi2] = splitMicros(timestampUs);
4443
4567
  const needSeg = this.core.decide(lo2, hi2, this.prevTotalMs, this.prevSegMs) === 1;
4444
4568
  let mask = null;
4569
+ let maskKind = 0;
4445
4570
  let segMs = 0;
4571
+ this.used = null;
4446
4572
  if (needSeg) {
4447
- if (!this.segmenter) {
4573
+ const classes = this.classSegmenter !== null && needsSkinClasses(this.filter, this.effects);
4574
+ const seg = classes ? this.classSegmenter : this.segmenter;
4575
+ if (!seg) {
4448
4576
  void this.ensureSegmenter();
4449
4577
  } else {
4450
4578
  const s0 = performance.now();
4451
- mask = this.segmenter.segment(source, timestampUs / 1e3);
4579
+ mask = seg.segment(source, timestampUs / 1e3);
4452
4580
  segMs = performance.now() - s0;
4581
+ this.used = seg;
4582
+ maskKind = classes ? 1 : 0;
4453
4583
  }
4454
4584
  }
4455
4585
  if (this.core.decideFace() === 1) {
@@ -4460,8 +4590,9 @@
4460
4590
  this.core.setFace(lo2, hi2, n2);
4461
4591
  }
4462
4592
  }
4463
- const glCalls = this.core.process(source, mask, this.background);
4464
- if (mask) this.segmenter?.release();
4593
+ const glCalls = this.core.process(source, mask, this.background, maskKind);
4594
+ if (mask) this.used?.release();
4595
+ this.used = null;
4465
4596
  this.prevSegMs = segMs;
4466
4597
  this.prevTotalMs = performance.now() - t0;
4467
4598
  const s2 = this.core.stats();
@@ -4493,14 +4624,16 @@
4493
4624
  * for the governor and the graph to decide with. See the ToMicros comment in
4494
4625
  * `core/clock.go`.
4495
4626
  */
4496
- capture(timestampUs) {
4497
- return new VideoFrame(this.canvas, { timestamp: timestampUs, alpha: "discard" });
4627
+ capture(timestampUs, keepAlpha = false) {
4628
+ return new VideoFrame(this.canvas, { timestamp: timestampUs, alpha: keepAlpha ? "keep" : "discard" });
4498
4629
  }
4499
4630
  close() {
4500
4631
  if (this.closed) return;
4501
4632
  this.closed = true;
4502
4633
  this.segmenter?.close();
4503
4634
  this.segmenter = null;
4635
+ this.classSegmenter?.close();
4636
+ this.classSegmenter = null;
4504
4637
  this.face?.close();
4505
4638
  this.face = null;
4506
4639
  this.lmBuf = null;
@@ -4509,6 +4642,7 @@
4509
4642
  this.background = null;
4510
4643
  this.backgroundPath = null;
4511
4644
  this.lutPath = "";
4645
+ this.makeupPath = "";
4512
4646
  this.gl.getExtension("WEBGL_lose_context")?.loseContext();
4513
4647
  }
4514
4648
  };
@@ -4566,14 +4700,38 @@
4566
4700
  var post = (m2) => self.postMessage(m2);
4567
4701
  var engine = null;
4568
4702
  var abort = null;
4703
+ var framesOn = false;
4569
4704
  async function start(m2) {
4705
+ await run(m2, m2.readable, m2.writable);
4706
+ }
4707
+ async function startTrack(m2) {
4708
+ const g2 = globalThis;
4709
+ if (!g2.MediaStreamTrackProcessor || !g2.VideoTrackGenerator) {
4710
+ m2.track.stop();
4711
+ throw new Error("mediakit: this worker has no MediaStreamTrackProcessor / VideoTrackGenerator");
4712
+ }
4713
+ const processor = new g2.MediaStreamTrackProcessor({ track: m2.track });
4714
+ const generator = new g2.VideoTrackGenerator();
4715
+ await run(m2, processor.readable, generator.writable, generator.track);
4716
+ }
4717
+ async function run(m2, readable, writable, outTrack) {
4570
4718
  const canvas = new OffscreenCanvas(m2.width, m2.height);
4571
- engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background);
4719
+ engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false);
4572
4720
  abort = new AbortController();
4573
4721
  let n2 = 0;
4722
+ let lastPost = performance.now();
4723
+ let lastLoading = engine.loading;
4574
4724
  const transform = new TransformStream({
4575
4725
  transform: (frame, controller) => {
4576
4726
  const ts2 = frame.timestamp ?? 0;
4727
+ n2++;
4728
+ const now = performance.now();
4729
+ const loading = engine?.loading ?? false;
4730
+ if (engine && (now - lastPost >= 500 || loading !== lastLoading)) {
4731
+ lastPost = now;
4732
+ lastLoading = loading;
4733
+ post({ t: "stats", stats: engine.last, frames: n2, loading });
4734
+ }
4577
4735
  let drew = false;
4578
4736
  try {
4579
4737
  drew = engine?.render(frame, ts2) ?? false;
@@ -4581,16 +4739,28 @@
4581
4739
  post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
4582
4740
  }
4583
4741
  if (!drew || !engine) {
4742
+ if (framesOn) {
4743
+ const copy = frame.clone();
4744
+ self.postMessage({ t: "frame", frame: copy }, [copy]);
4745
+ }
4584
4746
  controller.enqueue(frame);
4585
4747
  return;
4586
4748
  }
4587
4749
  controller.enqueue(engine.capture(ts2));
4750
+ if (framesOn) {
4751
+ const copy = engine.capture(ts2, true);
4752
+ self.postMessage({ t: "frame", frame: copy }, [copy]);
4753
+ }
4588
4754
  frame.close();
4589
- if (++n2 % 15 === 0) post({ t: "stats", stats: engine.last });
4590
4755
  }
4591
4756
  });
4592
- post({ t: "ready" });
4593
- void m2.readable.pipeThrough(transform, { signal: abort.signal }).pipeTo(m2.writable, { signal: abort.signal }).catch((e2) => {
4757
+ if (outTrack) {
4758
+ ;
4759
+ self.postMessage({ t: "ready", track: outTrack, loading: engine.loading }, [outTrack]);
4760
+ } else {
4761
+ post({ t: "ready", loading: engine.loading });
4762
+ }
4763
+ void readable.pipeThrough(transform, { signal: abort.signal }).pipeTo(writable, { signal: abort.signal }).catch((e2) => {
4594
4764
  if (abort?.signal.aborted) return;
4595
4765
  post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
4596
4766
  });
@@ -4604,12 +4774,16 @@
4604
4774
  self.onmessage = (e2) => {
4605
4775
  const m2 = e2.data;
4606
4776
  try {
4607
- if (m2.t === "start") {
4608
- void start(m2).catch((err) => {
4777
+ if (m2.t === "start" || m2.t === "startTrack") {
4778
+ void (m2.t === "start" ? start(m2) : startTrack(m2)).catch((err) => {
4609
4779
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4610
4780
  });
4781
+ } else if (m2.t === "frames") {
4782
+ framesOn = m2.on;
4611
4783
  } else if (m2.t === "recipe") {
4612
- engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).catch((err) => {
4784
+ engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).then(() => {
4785
+ if (engine?.loading) post({ t: "stats", stats: engine.last, frames: -1, loading: true });
4786
+ }).catch((err) => {
4613
4787
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4614
4788
  });
4615
4789
  } else {