@tradalab/mediakit 1.2.0 → 1.3.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/recipe.d.ts CHANGED
@@ -32,6 +32,10 @@ type BgFilter = {
32
32
  };
33
33
  /** A colour as red, green, blue, each 0..1. */
34
34
  type Rgb = [number, number, number];
35
+ /** Where smoothing (`beautify`) may land. */
36
+ type SkinScope = "person" | "face";
37
+ /** Where the colour LUT lands. */
38
+ type GradeScope = "frame" | "person" | "skin";
35
39
  interface Effects {
36
40
  /** Skin smoothing, 0..1. Needs the person mask, so it pulls in the segmentation cost. */
37
41
  beautify: number;
@@ -64,12 +68,37 @@ interface Effects {
64
68
  /** Iris colour: the ring only, clipped by the eyelids, the pupil left black. */
65
69
  iris: number;
66
70
  irisColor: Rgb;
71
+ /** Eye shadow, 0..1, multiplied into the lower part of the lid-to-brow band. */
72
+ eyeShadow: number;
73
+ eyeShadowColor: Rgb;
74
+ /** A white highlight across the middle of the lower lip, 0..1. Matte is 0. */
75
+ lipGloss: number;
76
+ /** A faint flush over the whole face's skin, 0..1 - the face, where `blush` is the cheeks. */
77
+ rosy: number;
78
+ rosyColor: Rgb;
79
+ /** Lift and cool the skin, 0..1: brighter, less yellow. */
80
+ whiten: number;
81
+ /**
82
+ * How `whiten` whitens. `natural` relights the skin and cools its yellow, keeping its colour.
83
+ * `pink` is the "fair and rosy" look East Asian beauty apps ship - GPUPixel's tuned skin
84
+ * tables (Apache-2.0, see NOTICE), served as `/mediakit/whiten_pink.png`; it fades out on
85
+ * darker skin, which those tables were not tuned for.
86
+ */
87
+ whitenStyle: "natural" | "pink";
88
+ /** Exposure on the skin only. */
89
+ skinLight: number;
90
+ /** Local contrast on the face. */
91
+ clarity: number;
92
+ /** Where smoothing may land. See {@link SkinScope}. */
93
+ skinScope: SkinScope;
67
94
  /**
68
95
  * A colour LUT: `lut` is a URL the host serves (a 512x512 "lookup" PNG, 64 levels, 8x8 tiles,
69
96
  * row 0 at the top), `grade` how strongly it applies. "" means none.
70
97
  */
71
98
  lut: string;
72
99
  grade: number;
100
+ /** Where the LUT lands. See {@link GradeScope}. */
101
+ gradeScope: GradeScope;
73
102
  /**
74
103
  * Take the person mask from a coloured screen instead of segmentation. Cheaper - MediaPipe
75
104
  * is never loaded - and the edge keeps hair the model loses. `keyColor` is the screen's
@@ -104,6 +133,22 @@ interface Effects {
104
133
  noseSlim: number;
105
134
  /** Chin length, -1 (shorter) .. 1 (longer). Withheld from meet like slimFace. */
106
135
  chin: number;
136
+ /** Reshaping along the face's own outline. */
137
+ jaw: number;
138
+ cheekbone: number;
139
+ forehead: number;
140
+ /** Brow fill, 0..1, multiplied in so the hairs show; lighter at the inner end. */
141
+ brow: number;
142
+ browColor: Rgb;
143
+ /** Shading under the cheekbones, along the jaw and beside the nose, 0..1. */
144
+ contour: number;
145
+ /** Light on the nose bridge, cheekbone tops, mid forehead and chin, 0..1. */
146
+ highlight: number;
147
+ /** A look painted as an image on the canonical face. */
148
+ makeupTexture: string;
149
+ makeup: number;
150
+ /** How the makeup texture blends: soft light (default), multiply, overlay, hard light, normal. */
151
+ makeupBlend: "softLight" | "multiply" | "overlay" | "hardLight" | "normal";
107
152
  }
108
153
  declare const NO_EFFECTS: Effects;
109
154
  /**
@@ -136,6 +181,23 @@ declare const MODE_NONE = 0;
136
181
  declare const MODE_BLUR = 1;
137
182
  declare const MODE_IMAGE = 2;
138
183
  declare const MODE_CUTOUT = 3;
184
+ /** Go-side `SkinScope` and `Scope`. Must match `core/recipe.go`. */
185
+ declare const SKIN_SCOPE: {
186
+ readonly person: 0;
187
+ readonly face: 1;
188
+ };
189
+ declare const MAKEUP_BLEND: {
190
+ readonly softLight: 0;
191
+ readonly multiply: 1;
192
+ readonly overlay: 2;
193
+ readonly hardLight: 3;
194
+ readonly normal: 4;
195
+ };
196
+ declare const GRADE_SCOPE: {
197
+ readonly frame: 0;
198
+ readonly person: 1;
199
+ readonly skin: 2;
200
+ };
139
201
  declare function backgroundMode(f: BgFilter): number;
140
202
  /**
141
203
  * Whether this recipe needs the person mask.
@@ -154,6 +216,8 @@ declare function needsMask(f: BgFilter, e: Effects): boolean;
154
216
  * whether MediaPipe is loaded - with `chromaKey` on, its 9 MB are never fetched.
155
217
  */
156
218
  declare function needsSegmentation(f: BgFilter, e: Effects): boolean;
219
+ /** Whether this recipe is better served by the multiclass segmenter. */
220
+ declare function needsSkinClasses(_f: BgFilter, e: Effects): boolean;
157
221
  /**
158
222
  * Whether this recipe needs face landmarks.
159
223
  *
@@ -183,7 +247,17 @@ interface CoreRecipe {
183
247
  blushColor: Rgb;
184
248
  iris: number;
185
249
  irisColor: Rgb;
250
+ eyeShadow: number;
251
+ eyeShadowColor: Rgb;
252
+ lipGloss: number;
253
+ rosy: number;
254
+ rosyColor: Rgb;
255
+ whiten: number;
256
+ skinLight: number;
257
+ clarity: number;
258
+ skinScope: number;
186
259
  grade: number;
260
+ gradeScope: number;
187
261
  chromaKey: boolean;
188
262
  keyColor: Rgb;
189
263
  keyTolerance: number;
@@ -193,7 +267,17 @@ interface CoreRecipe {
193
267
  enlargeEyes: number;
194
268
  noseSlim: number;
195
269
  chin: number;
270
+ jaw: number;
271
+ cheekbone: number;
272
+ forehead: number;
273
+ brow: number;
274
+ browColor: Rgb;
275
+ contour: number;
276
+ highlight: number;
277
+ makeup: number;
278
+ makeupBlend: number;
279
+ whitenStyle: number;
196
280
  }
197
281
  declare function coreRecipe(f: BgFilter, e: Effects): CoreRecipe;
198
282
 
199
- export { type BgFilter, type CoreRecipe, type Effects, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, TOUCH_UP, backgroundMode, coreRecipe, needsFace, needsMask, needsSegmentation, touchUp, touchUpAmount };
283
+ export { type BgFilter, type CoreRecipe, type Effects, 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, touchUp, touchUpAmount };
package/dist/recipe.js CHANGED
@@ -1,30 +1,38 @@
1
1
  import {
2
+ GRADE_SCOPE,
3
+ MAKEUP_BLEND,
2
4
  MODE_BLUR,
3
5
  MODE_CUTOUT,
4
6
  MODE_IMAGE,
5
7
  MODE_NONE,
6
8
  NO_EFFECTS,
9
+ SKIN_SCOPE,
7
10
  TOUCH_UP,
8
11
  backgroundMode,
9
12
  coreRecipe,
10
13
  needsFace,
11
14
  needsMask,
12
15
  needsSegmentation,
16
+ needsSkinClasses,
13
17
  touchUp,
14
18
  touchUpAmount
15
- } from "./chunk-V47SDU4L.js";
19
+ } from "./chunk-WRKQ2NFI.js";
16
20
  export {
21
+ GRADE_SCOPE,
22
+ MAKEUP_BLEND,
17
23
  MODE_BLUR,
18
24
  MODE_CUTOUT,
19
25
  MODE_IMAGE,
20
26
  MODE_NONE,
21
27
  NO_EFFECTS,
28
+ SKIN_SCOPE,
22
29
  TOUCH_UP,
23
30
  backgroundMode,
24
31
  coreRecipe,
25
32
  needsFace,
26
33
  needsMask,
27
34
  needsSegmentation,
35
+ needsSkinClasses,
28
36
  touchUp,
29
37
  touchUpAmount
30
38
  };
Binary file
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.
@@ -4165,8 +4168,18 @@
4165
4168
  blushColor: [0.9, 0.5, 0.42],
4166
4169
  iris: 0,
4167
4170
  irisColor: [0.3, 0.5, 0.75],
4171
+ eyeShadow: 0,
4172
+ eyeShadowColor: [0.55, 0.42, 0.37],
4173
+ lipGloss: 0,
4174
+ rosy: 0,
4175
+ rosyColor: [0.9, 0.42, 0.49],
4176
+ whiten: 0,
4177
+ skinLight: 0,
4178
+ clarity: 0,
4179
+ skinScope: "person",
4168
4180
  lut: "",
4169
4181
  grade: 0,
4182
+ gradeScope: "frame",
4170
4183
  chromaKey: false,
4171
4184
  keyColor: [0, 0, 0],
4172
4185
  keyTolerance: 0,
@@ -4175,12 +4188,26 @@
4175
4188
  slimFace: 0,
4176
4189
  enlargeEyes: 0,
4177
4190
  noseSlim: 0,
4178
- chin: 0
4191
+ chin: 0,
4192
+ jaw: 0,
4193
+ cheekbone: 0,
4194
+ forehead: 0,
4195
+ brow: 0,
4196
+ browColor: [0.36, 0.27, 0.22],
4197
+ contour: 0,
4198
+ highlight: 0,
4199
+ makeupTexture: "",
4200
+ makeup: 0,
4201
+ makeupBlend: "softLight",
4202
+ whitenStyle: "natural"
4179
4203
  };
4180
4204
  var MODE_NONE = 0;
4181
4205
  var MODE_BLUR = 1;
4182
4206
  var MODE_IMAGE = 2;
4183
4207
  var MODE_CUTOUT = 3;
4208
+ var SKIN_SCOPE = { person: 0, face: 1 };
4209
+ var MAKEUP_BLEND = { softLight: 0, multiply: 1, overlay: 2, hardLight: 3, normal: 4 };
4210
+ var GRADE_SCOPE = { frame: 0, person: 1, skin: 2 };
4184
4211
  function backgroundMode(f2) {
4185
4212
  switch (f2.kind) {
4186
4213
  case "blur":
@@ -4194,13 +4221,20 @@
4194
4221
  }
4195
4222
  }
4196
4223
  function needsMask(f2, e2) {
4197
- return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe;
4224
+ return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe || hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
4225
+ }
4226
+ function hasSkin(e2) {
4227
+ return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || (e2.clarity ?? 0) > 0;
4198
4228
  }
4199
4229
  function needsSegmentation(f2, e2) {
4200
4230
  return needsMask(f2, e2) && !e2.chromaKey;
4201
4231
  }
4232
+ function needsSkinClasses(_f, e2) {
4233
+ return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
4234
+ }
4202
4235
  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;
4236
+ 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.
4237
+ 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
4238
  }
4205
4239
  function coreRecipe(f2, e2) {
4206
4240
  return {
@@ -4222,7 +4256,17 @@
4222
4256
  blushColor: e2.blushColor,
4223
4257
  iris: e2.iris,
4224
4258
  irisColor: e2.irisColor,
4259
+ eyeShadow: e2.eyeShadow ?? 0,
4260
+ eyeShadowColor: e2.eyeShadowColor ?? NO_EFFECTS.eyeShadowColor,
4261
+ lipGloss: e2.lipGloss ?? 0,
4262
+ rosy: e2.rosy ?? 0,
4263
+ rosyColor: e2.rosyColor ?? NO_EFFECTS.rosyColor,
4264
+ whiten: e2.whiten ?? 0,
4265
+ skinLight: e2.skinLight ?? 0,
4266
+ clarity: e2.clarity ?? 0,
4267
+ skinScope: SKIN_SCOPE[e2.skinScope] ?? 0,
4225
4268
  grade: e2.grade,
4269
+ gradeScope: GRADE_SCOPE[e2.gradeScope] ?? 0,
4226
4270
  chromaKey: e2.chromaKey,
4227
4271
  keyColor: e2.keyColor,
4228
4272
  keyTolerance: e2.keyTolerance,
@@ -4231,7 +4275,17 @@
4231
4275
  slimFace: e2.slimFace,
4232
4276
  enlargeEyes: e2.enlargeEyes,
4233
4277
  noseSlim: e2.noseSlim,
4234
- chin: e2.chin
4278
+ chin: e2.chin,
4279
+ jaw: e2.jaw ?? 0,
4280
+ cheekbone: e2.cheekbone ?? 0,
4281
+ forehead: e2.forehead ?? 0,
4282
+ brow: e2.brow ?? 0,
4283
+ browColor: e2.browColor ?? NO_EFFECTS.browColor,
4284
+ contour: e2.contour ?? 0,
4285
+ highlight: e2.highlight ?? 0,
4286
+ makeup: e2.makeupTexture ? e2.makeup ?? 0 : 0,
4287
+ makeupBlend: MAKEUP_BLEND[e2.makeupBlend] ?? 0,
4288
+ whitenStyle: e2.whitenStyle === "pink" ? 1 : 0
4235
4289
  };
4236
4290
  }
4237
4291
 
@@ -4265,6 +4319,10 @@
4265
4319
  backgroundPath = null;
4266
4320
  /** The LUT the core holds, by path, so a slider drag does not decode it again. */
4267
4321
  lutPath = "";
4322
+ /** The makeup texture the core holds, by path - see setRecipe. */
4323
+ makeupPath = "";
4324
+ /** Whether the core holds the pink whitening table. */
4325
+ skinLutState = "none";
4268
4326
  closed = false;
4269
4327
  /**
4270
4328
  * The segmenter loads LAZILY - only once a recipe actually needs a person mask.
@@ -4274,6 +4332,11 @@
4274
4332
  */
4275
4333
  segmenter = null;
4276
4334
  segmenterLoading = null;
4335
+ /** The multiclass segmenter, for recipes that tell skin from clothes (`needsSkinClasses`). */
4336
+ classSegmenter = null;
4337
+ classLoading = null;
4338
+ /** The segmenter whose result this frame borrowed, to give it back after process(). */
4339
+ used = null;
4277
4340
  /**
4278
4341
  * The landmark detector, also lazy and independent of the segmenter.
4279
4342
  *
@@ -4296,11 +4359,11 @@
4296
4359
  this.core = core;
4297
4360
  }
4298
4361
  /** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
4299
- static async create(width, height, filter, effects = NO_EFFECTS) {
4362
+ static async create(width, height, filter, effects = NO_EFFECTS, alpha = false) {
4300
4363
  const canvas = document.createElement("canvas");
4301
4364
  canvas.width = width;
4302
4365
  canvas.height = height;
4303
- return _Engine.on(canvas, filter, effects);
4366
+ return _Engine.on(canvas, filter, effects, void 0, alpha);
4304
4367
  }
4305
4368
  /**
4306
4369
  * `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
@@ -4339,6 +4402,9 @@
4339
4402
  throw err;
4340
4403
  }
4341
4404
  }
4405
+ get loading() {
4406
+ return this.segmenterLoading !== null || this.classLoading !== null || this.faceLoading !== null;
4407
+ }
4342
4408
  get outputCanvas() {
4343
4409
  return this.canvas;
4344
4410
  }
@@ -4384,8 +4450,44 @@
4384
4450
  }
4385
4451
  this.lutPath = effects.lut;
4386
4452
  }
4453
+ if (effects.whitenStyle === "pink" && this.skinLutState === "none") {
4454
+ this.skinLutState = "loading";
4455
+ decodeImage("/mediakit/whiten_pink.png", { colorSpaceConversion: "none", premultiplyAlpha: "none" }).then((bmp) => {
4456
+ if (this.closed) {
4457
+ bmp.close();
4458
+ return;
4459
+ }
4460
+ this.core.setSkinLut(bmp);
4461
+ bmp.close();
4462
+ this.skinLutState = "ready";
4463
+ }).catch((err) => {
4464
+ console.error("mediakit: pink whitening table failed to load", err);
4465
+ this.skinLutState = "failed";
4466
+ });
4467
+ }
4468
+ const makeupTexture = effects.makeupTexture ?? "";
4469
+ if (makeupTexture !== this.makeupPath) {
4470
+ if (makeupTexture) {
4471
+ let bmp = null;
4472
+ try {
4473
+ bmp = await decodeImage(makeupTexture, { premultiplyAlpha: "none" });
4474
+ } catch (e2) {
4475
+ console.error("mediakit: makeup texture failed to load", e2);
4476
+ }
4477
+ if (this.closed || this.filter !== filter || this.effects !== effects) {
4478
+ bmp?.close();
4479
+ return;
4480
+ }
4481
+ this.core.setMakeup(bmp);
4482
+ bmp?.close();
4483
+ } else {
4484
+ this.core.setMakeup(null);
4485
+ }
4486
+ this.makeupPath = makeupTexture;
4487
+ }
4387
4488
  this.core.setRecipe(coreRecipe(filter, effects));
4388
4489
  if (needsSegmentation(filter, effects)) void this.ensureSegmenter();
4490
+ if (needsSkinClasses(filter, effects)) void this.ensureClassSegmenter();
4389
4491
  if (needsFace(effects)) void this.ensureFace();
4390
4492
  }
4391
4493
  ensureFace() {
@@ -4407,6 +4509,22 @@
4407
4509
  });
4408
4510
  return this.faceLoading;
4409
4511
  }
4512
+ ensureClassSegmenter() {
4513
+ if (this.classSegmenter || this.closed) return Promise.resolve();
4514
+ if (this.classLoading) return this.classLoading;
4515
+ this.classLoading = createSegmenter(this.canvas, "classes").then((s2) => {
4516
+ if (this.closed) {
4517
+ s2.close();
4518
+ return;
4519
+ }
4520
+ this.classSegmenter = s2;
4521
+ }).catch((e2) => {
4522
+ console.error("mediakit: multiclass segmenter failed to load", e2);
4523
+ }).finally(() => {
4524
+ this.classLoading = null;
4525
+ });
4526
+ return this.classLoading;
4527
+ }
4410
4528
  ensureSegmenter() {
4411
4529
  if (this.segmenter || this.closed) return Promise.resolve();
4412
4530
  if (this.segmenterLoading) return this.segmenterLoading;
@@ -4442,14 +4560,20 @@
4442
4560
  const [lo2, hi2] = splitMicros(timestampUs);
4443
4561
  const needSeg = this.core.decide(lo2, hi2, this.prevTotalMs, this.prevSegMs) === 1;
4444
4562
  let mask = null;
4563
+ let maskKind = 0;
4445
4564
  let segMs = 0;
4565
+ this.used = null;
4446
4566
  if (needSeg) {
4447
- if (!this.segmenter) {
4567
+ const classes = this.classSegmenter !== null && needsSkinClasses(this.filter, this.effects);
4568
+ const seg = classes ? this.classSegmenter : this.segmenter;
4569
+ if (!seg) {
4448
4570
  void this.ensureSegmenter();
4449
4571
  } else {
4450
4572
  const s0 = performance.now();
4451
- mask = this.segmenter.segment(source, timestampUs / 1e3);
4573
+ mask = seg.segment(source, timestampUs / 1e3);
4452
4574
  segMs = performance.now() - s0;
4575
+ this.used = seg;
4576
+ maskKind = classes ? 1 : 0;
4453
4577
  }
4454
4578
  }
4455
4579
  if (this.core.decideFace() === 1) {
@@ -4460,8 +4584,9 @@
4460
4584
  this.core.setFace(lo2, hi2, n2);
4461
4585
  }
4462
4586
  }
4463
- const glCalls = this.core.process(source, mask, this.background);
4464
- if (mask) this.segmenter?.release();
4587
+ const glCalls = this.core.process(source, mask, this.background, maskKind);
4588
+ if (mask) this.used?.release();
4589
+ this.used = null;
4465
4590
  this.prevSegMs = segMs;
4466
4591
  this.prevTotalMs = performance.now() - t0;
4467
4592
  const s2 = this.core.stats();
@@ -4493,14 +4618,16 @@
4493
4618
  * for the governor and the graph to decide with. See the ToMicros comment in
4494
4619
  * `core/clock.go`.
4495
4620
  */
4496
- capture(timestampUs) {
4497
- return new VideoFrame(this.canvas, { timestamp: timestampUs, alpha: "discard" });
4621
+ capture(timestampUs, keepAlpha = false) {
4622
+ return new VideoFrame(this.canvas, { timestamp: timestampUs, alpha: keepAlpha ? "keep" : "discard" });
4498
4623
  }
4499
4624
  close() {
4500
4625
  if (this.closed) return;
4501
4626
  this.closed = true;
4502
4627
  this.segmenter?.close();
4503
4628
  this.segmenter = null;
4629
+ this.classSegmenter?.close();
4630
+ this.classSegmenter = null;
4504
4631
  this.face?.close();
4505
4632
  this.face = null;
4506
4633
  this.lmBuf = null;
@@ -4509,6 +4636,7 @@
4509
4636
  this.background = null;
4510
4637
  this.backgroundPath = null;
4511
4638
  this.lutPath = "";
4639
+ this.makeupPath = "";
4512
4640
  this.gl.getExtension("WEBGL_lose_context")?.loseContext();
4513
4641
  }
4514
4642
  };
@@ -4566,14 +4694,38 @@
4566
4694
  var post = (m2) => self.postMessage(m2);
4567
4695
  var engine = null;
4568
4696
  var abort = null;
4697
+ var framesOn = false;
4569
4698
  async function start(m2) {
4699
+ await run(m2, m2.readable, m2.writable);
4700
+ }
4701
+ async function startTrack(m2) {
4702
+ const g2 = globalThis;
4703
+ if (!g2.MediaStreamTrackProcessor || !g2.VideoTrackGenerator) {
4704
+ m2.track.stop();
4705
+ throw new Error("mediakit: this worker has no MediaStreamTrackProcessor / VideoTrackGenerator");
4706
+ }
4707
+ const processor = new g2.MediaStreamTrackProcessor({ track: m2.track });
4708
+ const generator = new g2.VideoTrackGenerator();
4709
+ await run(m2, processor.readable, generator.writable, generator.track);
4710
+ }
4711
+ async function run(m2, readable, writable, outTrack) {
4570
4712
  const canvas = new OffscreenCanvas(m2.width, m2.height);
4571
- engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background);
4713
+ engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false);
4572
4714
  abort = new AbortController();
4573
4715
  let n2 = 0;
4716
+ let lastPost = performance.now();
4717
+ let lastLoading = engine.loading;
4574
4718
  const transform = new TransformStream({
4575
4719
  transform: (frame, controller) => {
4576
4720
  const ts2 = frame.timestamp ?? 0;
4721
+ n2++;
4722
+ const now = performance.now();
4723
+ const loading = engine?.loading ?? false;
4724
+ if (engine && (now - lastPost >= 500 || loading !== lastLoading)) {
4725
+ lastPost = now;
4726
+ lastLoading = loading;
4727
+ post({ t: "stats", stats: engine.last, frames: n2, loading });
4728
+ }
4577
4729
  let drew = false;
4578
4730
  try {
4579
4731
  drew = engine?.render(frame, ts2) ?? false;
@@ -4581,16 +4733,28 @@
4581
4733
  post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
4582
4734
  }
4583
4735
  if (!drew || !engine) {
4736
+ if (framesOn) {
4737
+ const copy = frame.clone();
4738
+ self.postMessage({ t: "frame", frame: copy }, [copy]);
4739
+ }
4584
4740
  controller.enqueue(frame);
4585
4741
  return;
4586
4742
  }
4587
4743
  controller.enqueue(engine.capture(ts2));
4744
+ if (framesOn) {
4745
+ const copy = engine.capture(ts2, true);
4746
+ self.postMessage({ t: "frame", frame: copy }, [copy]);
4747
+ }
4588
4748
  frame.close();
4589
- if (++n2 % 15 === 0) post({ t: "stats", stats: engine.last });
4590
4749
  }
4591
4750
  });
4592
- post({ t: "ready" });
4593
- void m2.readable.pipeThrough(transform, { signal: abort.signal }).pipeTo(m2.writable, { signal: abort.signal }).catch((e2) => {
4751
+ if (outTrack) {
4752
+ ;
4753
+ self.postMessage({ t: "ready", track: outTrack, loading: engine.loading }, [outTrack]);
4754
+ } else {
4755
+ post({ t: "ready", loading: engine.loading });
4756
+ }
4757
+ void readable.pipeThrough(transform, { signal: abort.signal }).pipeTo(writable, { signal: abort.signal }).catch((e2) => {
4594
4758
  if (abort?.signal.aborted) return;
4595
4759
  post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
4596
4760
  });
@@ -4604,12 +4768,16 @@
4604
4768
  self.onmessage = (e2) => {
4605
4769
  const m2 = e2.data;
4606
4770
  try {
4607
- if (m2.t === "start") {
4608
- void start(m2).catch((err) => {
4771
+ if (m2.t === "start" || m2.t === "startTrack") {
4772
+ void (m2.t === "start" ? start(m2) : startTrack(m2)).catch((err) => {
4609
4773
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4610
4774
  });
4775
+ } else if (m2.t === "frames") {
4776
+ framesOn = m2.on;
4611
4777
  } else if (m2.t === "recipe") {
4612
- engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).catch((err) => {
4778
+ engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).then(() => {
4779
+ if (engine?.loading) post({ t: "stats", stats: engine.last, frames: -1, loading: true });
4780
+ }).catch((err) => {
4613
4781
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4614
4782
  });
4615
4783
  } else {