@tradalab/mediakit 1.0.0 → 1.1.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/chunk-XLLQ74JA.js +112 -0
- package/dist/chunk-XLLQ74JA.js.map +1 -0
- package/dist/index.d.ts +25 -4
- package/dist/index.js +100 -35
- package/dist/index.js.map +1 -1
- package/dist/mediakit.wasm +0 -0
- package/dist/recipe.d.ts +67 -1
- package/dist/recipe.js +9 -3
- package/dist/worker.js +99 -32
- package/dist/worker.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-5KXLFDFV.js +0 -62
- package/dist/chunk-5KXLFDFV.js.map +0 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// src/recipe.ts
|
|
2
|
+
var NO_EFFECTS = {
|
|
3
|
+
beautify: 0,
|
|
4
|
+
exposure: 0,
|
|
5
|
+
contrast: 0,
|
|
6
|
+
saturation: 0,
|
|
7
|
+
warmth: 0,
|
|
8
|
+
autoframe: false,
|
|
9
|
+
teeth: 0,
|
|
10
|
+
eyeBright: 0,
|
|
11
|
+
underEye: 0,
|
|
12
|
+
smileLines: 0,
|
|
13
|
+
lips: 0,
|
|
14
|
+
lipColor: [0.55, 0.18, 0.24],
|
|
15
|
+
blush: 0,
|
|
16
|
+
blushColor: [0.9, 0.5, 0.42],
|
|
17
|
+
iris: 0,
|
|
18
|
+
irisColor: [0.3, 0.5, 0.75],
|
|
19
|
+
lut: "",
|
|
20
|
+
grade: 0,
|
|
21
|
+
autoLight: false,
|
|
22
|
+
slimFace: 0,
|
|
23
|
+
enlargeEyes: 0,
|
|
24
|
+
noseSlim: 0,
|
|
25
|
+
chin: 0
|
|
26
|
+
};
|
|
27
|
+
function touchUp(amount) {
|
|
28
|
+
const a = Math.min(1, Math.max(0, amount));
|
|
29
|
+
return {
|
|
30
|
+
beautify: a * TOUCH_UP.beautify,
|
|
31
|
+
eyeBright: a * TOUCH_UP.eyeBright,
|
|
32
|
+
underEye: a * TOUCH_UP.underEye,
|
|
33
|
+
smileLines: a * TOUCH_UP.smileLines,
|
|
34
|
+
teeth: a * TOUCH_UP.teeth
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
var TOUCH_UP = {
|
|
38
|
+
beautify: 0.6,
|
|
39
|
+
eyeBright: 0.5,
|
|
40
|
+
underEye: 0.45,
|
|
41
|
+
smileLines: 0.5,
|
|
42
|
+
teeth: 0.4
|
|
43
|
+
};
|
|
44
|
+
function touchUpAmount(e) {
|
|
45
|
+
let a = 0;
|
|
46
|
+
for (const k of Object.keys(TOUCH_UP)) {
|
|
47
|
+
a = Math.max(a, (e[k] ?? 0) / TOUCH_UP[k]);
|
|
48
|
+
}
|
|
49
|
+
return Math.min(1, a);
|
|
50
|
+
}
|
|
51
|
+
var MODE_NONE = 0;
|
|
52
|
+
var MODE_BLUR = 1;
|
|
53
|
+
var MODE_IMAGE = 2;
|
|
54
|
+
function backgroundMode(f) {
|
|
55
|
+
switch (f.kind) {
|
|
56
|
+
case "blur":
|
|
57
|
+
return MODE_BLUR;
|
|
58
|
+
case "image":
|
|
59
|
+
return MODE_IMAGE;
|
|
60
|
+
default:
|
|
61
|
+
return MODE_NONE;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function needsMask(f, e) {
|
|
65
|
+
return f.kind !== "none" || e.beautify > 0 || e.autoframe;
|
|
66
|
+
}
|
|
67
|
+
function needsFace(e) {
|
|
68
|
+
return e.teeth > 0 || e.eyeBright > 0 || e.underEye > 0 || e.smileLines > 0 || e.lips > 0 || e.blush > 0 || e.iris > 0 || e.slimFace > 0 || e.enlargeEyes > 0 || e.noseSlim > 0 || e.chin !== 0;
|
|
69
|
+
}
|
|
70
|
+
function coreRecipe(f, e) {
|
|
71
|
+
return {
|
|
72
|
+
background: backgroundMode(f),
|
|
73
|
+
blurRadius: f.kind === "blur" ? f.blurRadius : 0,
|
|
74
|
+
beautify: e.beautify,
|
|
75
|
+
exposure: e.exposure,
|
|
76
|
+
contrast: e.contrast,
|
|
77
|
+
saturation: e.saturation,
|
|
78
|
+
warmth: e.warmth,
|
|
79
|
+
autoframe: e.autoframe,
|
|
80
|
+
teeth: e.teeth,
|
|
81
|
+
eyeBright: e.eyeBright,
|
|
82
|
+
underEye: e.underEye,
|
|
83
|
+
smileLines: e.smileLines,
|
|
84
|
+
lips: e.lips,
|
|
85
|
+
lipColor: e.lipColor,
|
|
86
|
+
blush: e.blush,
|
|
87
|
+
blushColor: e.blushColor,
|
|
88
|
+
iris: e.iris,
|
|
89
|
+
irisColor: e.irisColor,
|
|
90
|
+
grade: e.grade,
|
|
91
|
+
autoLight: e.autoLight,
|
|
92
|
+
slimFace: e.slimFace,
|
|
93
|
+
enlargeEyes: e.enlargeEyes,
|
|
94
|
+
noseSlim: e.noseSlim,
|
|
95
|
+
chin: e.chin
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export {
|
|
100
|
+
NO_EFFECTS,
|
|
101
|
+
touchUp,
|
|
102
|
+
TOUCH_UP,
|
|
103
|
+
touchUpAmount,
|
|
104
|
+
MODE_NONE,
|
|
105
|
+
MODE_BLUR,
|
|
106
|
+
MODE_IMAGE,
|
|
107
|
+
backgroundMode,
|
|
108
|
+
needsMask,
|
|
109
|
+
needsFace,
|
|
110
|
+
coreRecipe
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=chunk-XLLQ74JA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/recipe.ts"],"sourcesContent":["/**\n * The bridge between what the user picked and the recipe the Go core takes.\n *\n * Both halves live here because both map onto `core.Recipe` field for field: BgFilter carries\n * the background mode and its one parameter, Effects carries everything else. They are two\n * types rather than one because the UI edits them from two different places - a preset picker\n * and a slider panel - not because they belong to different layers.\n */\n\n/**\n * What replaces the background.\n *\n * `imagePath` is a URL the consuming app serves: a path under its own public root, or a data\n * URL for something the user uploaded. The Engine fetches it and cover-crops it to the frame\n * aspect, so any shape is safe to pass.\n */\nexport type BgFilter =\n | { kind: \"none\" }\n | { kind: \"blur\"; blurRadius: number }\n | { kind: \"image\"; imagePath: string }\n\n/** A colour as red, green, blue, each 0..1. */\nexport type Rgb = [number, number, number]\n\nexport interface Effects {\n /** Skin smoothing, 0..1. Needs the person mask, so it pulls in the segmentation cost. */\n beautify: number\n /** Exposure, -1..1. Positive lifts the shadows more than the highlights. */\n exposure: number\n /** Contrast, -1..1. */\n contrast: number\n /** Saturation, -1..1. */\n saturation: number\n /** Warm (positive) or cool (negative), -1..1. */\n warmth: number\n /** Keep the person framed. Needs the person mask. */\n autoframe: boolean\n\n // ── Need FACE landmarks ──────────────────────────────────────────────────────────\n /** Teeth whitening, 0..1. */\n teeth: number\n /** Brighten the whites of the eyes, 0..1. */\n eyeBright: number\n /** Reduce under-eye shadow, 0..1. */\n underEye: number\n /** Soften the nasolabial folds, 0..1. */\n smileLines: number\n\n /**\n * Makeup, 0..1 each, in its colour. Drawn against the full landmark mesh, so the lip tint\n * follows the lip line rather than an ellipse. Not offered in meet, like reshaping.\n */\n lips: number\n lipColor: Rgb\n blush: number\n blushColor: Rgb\n /** Iris colour: the ring only, clipped by the eyelids, the pupil left black. */\n iris: number\n irisColor: Rgb\n\n /**\n * A colour LUT: `lut` is a URL the host serves (a 512x512 \"lookup\" PNG, 64 levels, 8x8 tiles,\n * row 0 at the top), `grade` how strongly it applies. \"\" means none.\n */\n lut: string\n grade: number\n\n /**\n * Lift exposure when the room is dark, on top of `exposure`. Only ever brightens. Needs no\n * detector: it reads the frame's own brightness.\n */\n autoLight: boolean\n\n /**\n * Jawline slimming, 0..1.\n *\n * Deliberately NOT offered in meet: Zoom, Google Meet and Teams all ship without face\n * reshaping, and in a work setting it reads as unserious. The field is here because the\n * second consumer of mediakit is slate, where reshaping is ordinary.\n */\n slimFace: number\n /** Eye enlargement, 0..1. Withheld from meet for the same reason as slimFace. */\n enlargeEyes: number\n /** Nose narrowing, 0..1. Withheld from meet like slimFace. */\n noseSlim: number\n /** Chin length, -1 (shorter) .. 1 (longer). Withheld from meet like slimFace. */\n chin: number\n}\n\nexport const NO_EFFECTS: Effects = {\n beautify: 0,\n exposure: 0,\n contrast: 0,\n saturation: 0,\n warmth: 0,\n autoframe: false,\n teeth: 0,\n eyeBright: 0,\n underEye: 0,\n smileLines: 0,\n lips: 0,\n lipColor: [0.55, 0.18, 0.24],\n blush: 0,\n blushColor: [0.9, 0.5, 0.42],\n iris: 0,\n irisColor: [0.3, 0.5, 0.75],\n lut: \"\",\n grade: 0,\n autoLight: false,\n slimFace: 0,\n enlargeEyes: 0,\n noseSlim: 0,\n chin: 0,\n}\n\n/**\n * One knob for \"look a little fresher\": skin, eyes, under-eyes, smile lines and teeth, moved\n * together in proportions tuned to read as rested rather than filtered at 1.\n *\n * A host that shows this instead of five sliders gets the shape Zoom and TikTok ship - people\n * reach for one control, not five, and five half-understood knobs mostly get set too high. The\n * ratios live here, next to the fields they drive, so every host means the same thing by it.\n */\nexport function touchUp(amount: number): Pick<Effects, \"beautify\" | \"eyeBright\" | \"underEye\" | \"smileLines\" | \"teeth\"> {\n const a = Math.min(1, Math.max(0, amount))\n return {\n beautify: a * TOUCH_UP.beautify,\n eyeBright: a * TOUCH_UP.eyeBright,\n underEye: a * TOUCH_UP.underEye,\n smileLines: a * TOUCH_UP.smileLines,\n teeth: a * TOUCH_UP.teeth,\n }\n}\n\n/** What `touchUp(1)` sets. Exported so a host can read a stored recipe back into one knob. */\nexport const TOUCH_UP = {\n beautify: 0.6,\n eyeBright: 0.5,\n underEye: 0.45,\n smileLines: 0.5,\n teeth: 0.4,\n} as const\n\n/**\n * The touch-up position a recipe corresponds to: the LARGEST of its five fields relative to\n * `touchUp(1)`. A recipe whose fields were set one by one has no single answer, and reading one\n * field only would show 0 for a recipe still whitening teeth - a knob at 0 that cannot turn off\n * what is running. Taking the largest, any effect still on shows, and moving the knob rewrites\n * all five consistently.\n */\nexport function touchUpAmount(e: Effects): number {\n let a = 0\n for (const k of Object.keys(TOUCH_UP) as (keyof typeof TOUCH_UP)[]) {\n a = Math.max(a, (e[k] ?? 0) / TOUCH_UP[k])\n }\n return Math.min(1, a)\n}\n\n/** Go-side `BackgroundMode`. Must match `core/recipe.go`. */\nexport const MODE_NONE = 0\nexport const MODE_BLUR = 1\nexport const MODE_IMAGE = 2\n\nexport function backgroundMode(f: BgFilter): number {\n switch (f.kind) {\n case \"blur\":\n return MODE_BLUR\n case \"image\":\n return MODE_IMAGE\n default:\n return MODE_NONE\n }\n}\n\n/**\n * Whether this recipe needs the person mask.\n *\n * Must match `Recipe.NeedsMask` on the Go side. This is the single biggest saving in the\n * module: a colour-only recipe skips MediaPipe entirely - the most expensive part of a frame,\n * and 9 MB of wasm the user then never downloads.\n *\n * The Go version is authoritative; this one exists so the JS side knows whether to load the\n * segmenter at all, BEFORE it can ask Go.\n */\nexport function needsMask(f: BgFilter, e: Effects): boolean {\n return f.kind !== \"none\" || e.beautify > 0 || e.autoframe\n}\n\n/**\n * Whether this recipe needs face landmarks.\n *\n * Must match `Recipe.NeedsFace` on the Go side. Split from {@link needsMask} because the two\n * detectors are independent costs, and the landmark model is 15 times the size of the\n * segmentation one - making someone who only wants a blurred background download it is waste\n * that shows up on the first load.\n */\nexport function needsFace(e: Effects): boolean {\n return (\n e.teeth > 0 ||\n e.eyeBright > 0 ||\n e.underEye > 0 ||\n e.smileLines > 0 ||\n e.lips > 0 ||\n e.blush > 0 ||\n e.iris > 0 ||\n e.slimFace > 0 ||\n e.enlargeEyes > 0 ||\n e.noseSlim > 0 ||\n e.chin !== 0\n )\n}\n\n/** The recipe in the shape the Go core takes. Field names must match `setRecipe` in wasmjs/main.go. */\nexport interface CoreRecipe {\n background: number\n blurRadius: number\n beautify: number\n exposure: number\n contrast: number\n saturation: number\n warmth: number\n autoframe: boolean\n teeth: number\n eyeBright: number\n underEye: number\n smileLines: number\n lips: number\n lipColor: Rgb\n blush: number\n blushColor: Rgb\n iris: number\n irisColor: Rgb\n grade: number\n autoLight: boolean\n slimFace: number\n enlargeEyes: number\n noseSlim: number\n chin: number\n}\n\nexport function coreRecipe(f: BgFilter, e: Effects): CoreRecipe {\n return {\n background: backgroundMode(f),\n blurRadius: f.kind === \"blur\" ? f.blurRadius : 0,\n beautify: e.beautify,\n exposure: e.exposure,\n contrast: e.contrast,\n saturation: e.saturation,\n warmth: e.warmth,\n autoframe: e.autoframe,\n teeth: e.teeth,\n eyeBright: e.eyeBright,\n underEye: e.underEye,\n smileLines: e.smileLines,\n lips: e.lips,\n lipColor: e.lipColor,\n blush: e.blush,\n blushColor: e.blushColor,\n iris: e.iris,\n irisColor: e.irisColor,\n grade: e.grade,\n autoLight: e.autoLight,\n slimFace: e.slimFace,\n enlargeEyes: e.enlargeEyes,\n noseSlim: e.noseSlim,\n chin: e.chin,\n }\n}\n"],"mappings":";AAyFO,IAAM,aAAsB;AAAA,EAC/B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,OAAO;AAAA,EACP,WAAW;AAAA,EACX,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,UAAU,CAAC,MAAM,MAAM,IAAI;AAAA,EAC3B,OAAO;AAAA,EACP,YAAY,CAAC,KAAK,KAAK,IAAI;AAAA,EAC3B,MAAM;AAAA,EACN,WAAW,CAAC,KAAK,KAAK,IAAI;AAAA,EAC1B,KAAK;AAAA,EACL,OAAO;AAAA,EACP,WAAW;AAAA,EACX,UAAU;AAAA,EACV,aAAa;AAAA,EACb,UAAU;AAAA,EACV,MAAM;AACV;AAUO,SAAS,QAAQ,QAA+F;AACnH,QAAM,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC;AACzC,SAAO;AAAA,IACH,UAAU,IAAI,SAAS;AAAA,IACvB,WAAW,IAAI,SAAS;AAAA,IACxB,UAAU,IAAI,SAAS;AAAA,IACvB,YAAY,IAAI,SAAS;AAAA,IACzB,OAAO,IAAI,SAAS;AAAA,EACxB;AACJ;AAGO,IAAM,WAAW;AAAA,EACpB,UAAU;AAAA,EACV,WAAW;AAAA,EACX,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,OAAO;AACX;AASO,SAAS,cAAc,GAAoB;AAC9C,MAAI,IAAI;AACR,aAAW,KAAK,OAAO,KAAK,QAAQ,GAAgC;AAChE,QAAI,KAAK,IAAI,IAAI,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;AAAA,EAC7C;AACA,SAAO,KAAK,IAAI,GAAG,CAAC;AACxB;AAGO,IAAM,YAAY;AAClB,IAAM,YAAY;AAClB,IAAM,aAAa;AAEnB,SAAS,eAAe,GAAqB;AAChD,UAAQ,EAAE,MAAM;AAAA,IACZ,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,IACX;AACI,aAAO;AAAA,EACf;AACJ;AAYO,SAAS,UAAU,GAAa,GAAqB;AACxD,SAAO,EAAE,SAAS,UAAU,EAAE,WAAW,KAAK,EAAE;AACpD;AAUO,SAAS,UAAU,GAAqB;AAC3C,SACI,EAAE,QAAQ,KACV,EAAE,YAAY,KACd,EAAE,WAAW,KACb,EAAE,aAAa,KACf,EAAE,OAAO,KACT,EAAE,QAAQ,KACV,EAAE,OAAO,KACT,EAAE,WAAW,KACb,EAAE,cAAc,KAChB,EAAE,WAAW,KACb,EAAE,SAAS;AAEnB;AA8BO,SAAS,WAAW,GAAa,GAAwB;AAC5D,SAAO;AAAA,IACH,YAAY,eAAe,CAAC;AAAA,IAC5B,YAAY,EAAE,SAAS,SAAS,EAAE,aAAa;AAAA,IAC/C,UAAU,EAAE;AAAA,IACZ,UAAU,EAAE;AAAA,IACZ,UAAU,EAAE;AAAA,IACZ,YAAY,EAAE;AAAA,IACd,QAAQ,EAAE;AAAA,IACV,WAAW,EAAE;AAAA,IACb,OAAO,EAAE;AAAA,IACT,WAAW,EAAE;AAAA,IACb,UAAU,EAAE;AAAA,IACZ,YAAY,EAAE;AAAA,IACd,MAAM,EAAE;AAAA,IACR,UAAU,EAAE;AAAA,IACZ,OAAO,EAAE;AAAA,IACT,YAAY,EAAE;AAAA,IACd,MAAM,EAAE;AAAA,IACR,WAAW,EAAE;AAAA,IACb,OAAO,EAAE;AAAA,IACT,WAAW,EAAE;AAAA,IACb,UAAU,EAAE;AAAA,IACZ,aAAa,EAAE;AAAA,IACf,UAAU,EAAE;AAAA,IACZ,MAAM,EAAE;AAAA,EACZ;AACJ;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BgFilter, Effects } from './recipe.js';
|
|
2
|
-
export { NO_EFFECTS, needsFace, needsMask } from './recipe.js';
|
|
2
|
+
export { NO_EFFECTS, Rgb, TOUCH_UP, needsFace, needsMask, touchUp, touchUpAmount } from './recipe.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* One mediakit pipeline: canvas plus WebGL2 plus the Go core plus the detectors, which is
|
|
@@ -33,6 +33,10 @@ interface FrameStats {
|
|
|
33
33
|
faceSize: number;
|
|
34
34
|
/** Whether the current recipe needs face landmarks. */
|
|
35
35
|
needsFace: boolean;
|
|
36
|
+
/** The exposure lift auto light is applying, 0 when it is off or the room is bright. */
|
|
37
|
+
autoExposure: number;
|
|
38
|
+
/** How many faces are being tracked. */
|
|
39
|
+
faces: number;
|
|
36
40
|
}
|
|
37
41
|
type EngineCanvas = HTMLCanvasElement | OffscreenCanvas;
|
|
38
42
|
declare class Engine {
|
|
@@ -41,6 +45,8 @@ declare class Engine {
|
|
|
41
45
|
private core;
|
|
42
46
|
private background;
|
|
43
47
|
private backgroundPath;
|
|
48
|
+
/** The LUT the core holds, by path, so a slider drag does not decode it again. */
|
|
49
|
+
private lutPath;
|
|
44
50
|
private closed;
|
|
45
51
|
/**
|
|
46
52
|
* The segmenter loads LAZILY - only once a recipe actually needs a person mask.
|
|
@@ -67,10 +73,17 @@ declare class Engine {
|
|
|
67
73
|
private constructor();
|
|
68
74
|
/** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
|
|
69
75
|
static create(width: number, height: number, filter: BgFilter, effects?: Effects): Promise<Engine>;
|
|
70
|
-
|
|
76
|
+
/** `background`: see {@link Engine.setRecipe}. */
|
|
77
|
+
static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap): Promise<Engine>;
|
|
71
78
|
get outputCanvas(): EngineCanvas;
|
|
72
|
-
/**
|
|
73
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
|
|
81
|
+
*
|
|
82
|
+
* `background` is `filter.imagePath` already decoded, and the Engine takes ownership of it.
|
|
83
|
+
* A worker needs it: there is no `Image` there, so an SVG - every background meet bundles -
|
|
84
|
+
* cannot be decoded at all, and the main thread has to do it and transfer the bitmap.
|
|
85
|
+
*/
|
|
86
|
+
setRecipe(filter: BgFilter, effects?: Effects, background?: ImageBitmap): Promise<void>;
|
|
74
87
|
private ensureFace;
|
|
75
88
|
private ensureSegmenter;
|
|
76
89
|
/**
|
|
@@ -138,6 +151,8 @@ declare class VideoPipeline {
|
|
|
138
151
|
private abort;
|
|
139
152
|
private worker;
|
|
140
153
|
private workerStats;
|
|
154
|
+
/** The background path the worker already holds; see backgroundForWorker. */
|
|
155
|
+
private workerBackgroundPath;
|
|
141
156
|
private video;
|
|
142
157
|
private rvfcHandle;
|
|
143
158
|
private stopped;
|
|
@@ -152,6 +167,12 @@ declare class VideoPipeline {
|
|
|
152
167
|
setEffects(effects: Effects): Promise<void>;
|
|
153
168
|
private pushRecipe;
|
|
154
169
|
stop(): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* The image background decoded HERE, for the worker: it has no `Image`, so it cannot decode
|
|
172
|
+
* an SVG, which is every background meet bundles. Only when the path changed - the worker
|
|
173
|
+
* Engine keeps the last one by path, and a slider drag must not decode it again.
|
|
174
|
+
*/
|
|
175
|
+
private backgroundForWorker;
|
|
155
176
|
private send;
|
|
156
177
|
private teardownWorker;
|
|
157
178
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
NO_EFFECTS,
|
|
3
|
+
TOUCH_UP,
|
|
3
4
|
coreRecipe,
|
|
4
5
|
needsFace,
|
|
5
|
-
needsMask
|
|
6
|
-
|
|
6
|
+
needsMask,
|
|
7
|
+
touchUp,
|
|
8
|
+
touchUpAmount
|
|
9
|
+
} from "./chunk-XLLQ74JA.js";
|
|
7
10
|
|
|
8
11
|
// src/segmenter.ts
|
|
9
12
|
import { FilesetResolver, ImageSegmenter } from "@mediapipe/tasks-vision";
|
|
@@ -55,15 +58,16 @@ async function createSegmenter(canvas) {
|
|
|
55
58
|
import { FaceLandmarker, FilesetResolver as FilesetResolver2 } from "@mediapipe/tasks-vision";
|
|
56
59
|
var WASM_DIR2 = "/mediakit/mp";
|
|
57
60
|
var MODEL_URL2 = "/mediakit/face_landmarker.task";
|
|
58
|
-
|
|
61
|
+
var FACE_FLOATS = 478 * 3;
|
|
62
|
+
async function createFaceDetector(canvas, maxFaces) {
|
|
59
63
|
const fileset = await FilesetResolver2.forVisionTasks(WASM_DIR2);
|
|
60
64
|
const landmarker = await FaceLandmarker.createFromOptions(fileset, {
|
|
61
65
|
baseOptions: { modelAssetPath: MODEL_URL2, delegate: "GPU" },
|
|
62
66
|
canvas,
|
|
63
67
|
runningMode: "VIDEO",
|
|
64
|
-
//
|
|
65
|
-
// the
|
|
66
|
-
numFaces:
|
|
68
|
+
// As many as the core tracks: one landmark pass returns them all, so the cost of a second
|
|
69
|
+
// face is the copy below and its own shader passes, not a second inference.
|
|
70
|
+
numFaces: maxFaces,
|
|
67
71
|
// No blendshapes and no transformation matrix - extra work for data nobody reads.
|
|
68
72
|
outputFaceBlendshapes: false,
|
|
69
73
|
outputFacialTransformationMatrixes: false
|
|
@@ -71,14 +75,16 @@ async function createFaceDetector(canvas) {
|
|
|
71
75
|
return {
|
|
72
76
|
detect(frame, timestampMs, out) {
|
|
73
77
|
const res = landmarker.detectForVideo(frame, timestampMs);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
let n = 0;
|
|
79
|
+
for (const lm of res.faceLandmarks ?? []) {
|
|
80
|
+
if (lm.length * 3 !== FACE_FLOATS || n + FACE_FLOATS > out.length) break;
|
|
81
|
+
for (let i = 0; i < lm.length; i++) {
|
|
82
|
+
const p = lm[i];
|
|
83
|
+
out[n + i * 3] = p.x;
|
|
84
|
+
out[n + i * 3 + 1] = p.y;
|
|
85
|
+
out[n + i * 3 + 2] = p.z;
|
|
86
|
+
}
|
|
87
|
+
n += FACE_FLOATS;
|
|
82
88
|
}
|
|
83
89
|
return n;
|
|
84
90
|
},
|
|
@@ -174,7 +180,9 @@ var ZERO_STATS = {
|
|
|
174
180
|
needsMask: false,
|
|
175
181
|
faceFound: false,
|
|
176
182
|
faceSize: 0,
|
|
177
|
-
needsFace: false
|
|
183
|
+
needsFace: false,
|
|
184
|
+
autoExposure: 0,
|
|
185
|
+
faces: 0
|
|
178
186
|
};
|
|
179
187
|
var Engine = class _Engine {
|
|
180
188
|
canvas;
|
|
@@ -186,6 +194,8 @@ var Engine = class _Engine {
|
|
|
186
194
|
// image background is on costs a fetch plus two createImageBitmap calls, which is a
|
|
187
195
|
// visible stutter under a dragged finger and the exact case setRecipe exists to make cheap.
|
|
188
196
|
backgroundPath = null;
|
|
197
|
+
/** The LUT the core holds, by path, so a slider drag does not decode it again. */
|
|
198
|
+
lutPath = "";
|
|
189
199
|
closed = false;
|
|
190
200
|
/**
|
|
191
201
|
* The segmenter loads LAZILY - only once a recipe actually needs a person mask.
|
|
@@ -223,7 +233,8 @@ var Engine = class _Engine {
|
|
|
223
233
|
canvas.height = height;
|
|
224
234
|
return _Engine.on(canvas, filter, effects);
|
|
225
235
|
}
|
|
226
|
-
|
|
236
|
+
/** `background`: see {@link Engine.setRecipe}. */
|
|
237
|
+
static async on(canvas, filter, effects = NO_EFFECTS, background) {
|
|
227
238
|
const width = canvas.width;
|
|
228
239
|
const height = canvas.height;
|
|
229
240
|
const gl = canvas.getContext("webgl2", {
|
|
@@ -241,7 +252,7 @@ var Engine = class _Engine {
|
|
|
241
252
|
const err = core.init(gl, width, height);
|
|
242
253
|
if (err) throw new Error(`mediakit: ${err}`);
|
|
243
254
|
engine = new _Engine(canvas, gl, core);
|
|
244
|
-
await engine.setRecipe(filter, effects);
|
|
255
|
+
await engine.setRecipe(filter, effects, background);
|
|
245
256
|
return engine;
|
|
246
257
|
} catch (err) {
|
|
247
258
|
try {
|
|
@@ -258,13 +269,21 @@ var Engine = class _Engine {
|
|
|
258
269
|
get outputCanvas() {
|
|
259
270
|
return this.canvas;
|
|
260
271
|
}
|
|
261
|
-
/**
|
|
262
|
-
|
|
272
|
+
/**
|
|
273
|
+
* Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
|
|
274
|
+
*
|
|
275
|
+
* `background` is `filter.imagePath` already decoded, and the Engine takes ownership of it.
|
|
276
|
+
* A worker needs it: there is no `Image` there, so an SVG - every background meet bundles -
|
|
277
|
+
* cannot be decoded at all, and the main thread has to do it and transfer the bitmap.
|
|
278
|
+
*/
|
|
279
|
+
async setRecipe(filter, effects = this.effects, background) {
|
|
263
280
|
if (this.closed) return;
|
|
264
281
|
this.filter = filter;
|
|
265
282
|
this.effects = effects;
|
|
266
|
-
if (filter.kind === "image" && filter.imagePath !== this.backgroundPath) {
|
|
267
|
-
const
|
|
283
|
+
if (filter.kind === "image" && (background || filter.imagePath !== this.backgroundPath)) {
|
|
284
|
+
const w = this.canvas.width;
|
|
285
|
+
const h = this.canvas.height;
|
|
286
|
+
const bmp = background ? await coverCrop(background, w, h) : await loadBitmap(filter.imagePath, w, h);
|
|
268
287
|
if (this.closed) {
|
|
269
288
|
bmp.close();
|
|
270
289
|
return;
|
|
@@ -273,6 +292,25 @@ var Engine = class _Engine {
|
|
|
273
292
|
this.background = bmp;
|
|
274
293
|
this.backgroundPath = filter.imagePath;
|
|
275
294
|
}
|
|
295
|
+
if (effects.lut !== this.lutPath) {
|
|
296
|
+
if (effects.lut) {
|
|
297
|
+
let bmp = null;
|
|
298
|
+
try {
|
|
299
|
+
bmp = await decodeImage(effects.lut, { colorSpaceConversion: "none", premultiplyAlpha: "none" });
|
|
300
|
+
} catch (e) {
|
|
301
|
+
console.error("mediakit: LUT failed to load", e);
|
|
302
|
+
}
|
|
303
|
+
if (this.closed || this.filter !== filter || this.effects !== effects) {
|
|
304
|
+
bmp?.close();
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
this.core.setLut(bmp);
|
|
308
|
+
bmp?.close();
|
|
309
|
+
} else {
|
|
310
|
+
this.core.setLut(null);
|
|
311
|
+
}
|
|
312
|
+
this.lutPath = effects.lut;
|
|
313
|
+
}
|
|
276
314
|
this.core.setRecipe(coreRecipe(filter, effects));
|
|
277
315
|
if (needsMask(filter, effects)) void this.ensureSegmenter();
|
|
278
316
|
if (needsFace(effects)) void this.ensureFace();
|
|
@@ -280,13 +318,15 @@ var Engine = class _Engine {
|
|
|
280
318
|
ensureFace() {
|
|
281
319
|
if (this.face || this.closed) return Promise.resolve();
|
|
282
320
|
if (this.faceLoading) return this.faceLoading;
|
|
283
|
-
|
|
321
|
+
const buf = this.core.faceBuffer();
|
|
322
|
+
if (!buf) return Promise.resolve();
|
|
323
|
+
this.faceLoading = createFaceDetector(this.canvas, Math.max(1, Math.floor(buf.length / FACE_FLOATS))).then((f) => {
|
|
284
324
|
if (this.closed) {
|
|
285
325
|
f.close();
|
|
286
326
|
return;
|
|
287
327
|
}
|
|
288
328
|
this.face = f;
|
|
289
|
-
this.lmBuf =
|
|
329
|
+
this.lmBuf = buf;
|
|
290
330
|
}).catch((e) => {
|
|
291
331
|
console.error("mediakit: face detector failed to load", e);
|
|
292
332
|
}).finally(() => {
|
|
@@ -365,7 +405,9 @@ var Engine = class _Engine {
|
|
|
365
405
|
needsMask: s ? s[7] > 0.5 : false,
|
|
366
406
|
faceFound: s ? s[8] > 0.5 : false,
|
|
367
407
|
faceSize: s ? s[9] : 0,
|
|
368
|
-
needsFace: s ? s[10] > 0.5 : false
|
|
408
|
+
needsFace: s ? s[10] > 0.5 : false,
|
|
409
|
+
autoExposure: s ? s[11] : 0,
|
|
410
|
+
faces: s ? s[12] : 0
|
|
369
411
|
};
|
|
370
412
|
return glCalls > 0;
|
|
371
413
|
}
|
|
@@ -393,16 +435,17 @@ var Engine = class _Engine {
|
|
|
393
435
|
this.background?.close();
|
|
394
436
|
this.background = null;
|
|
395
437
|
this.backgroundPath = null;
|
|
438
|
+
this.lutPath = "";
|
|
396
439
|
this.gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
397
440
|
}
|
|
398
441
|
};
|
|
399
|
-
async function decodeImage(path) {
|
|
442
|
+
async function decodeImage(path, opts) {
|
|
400
443
|
if (typeof Image === "undefined") {
|
|
401
444
|
const res = await fetch(path);
|
|
402
|
-
if (!res.ok) throw new Error(`mediakit: cannot load
|
|
445
|
+
if (!res.ok) throw new Error(`mediakit: cannot load image ${path} (HTTP ${res.status})`);
|
|
403
446
|
const blob = await res.blob();
|
|
404
447
|
try {
|
|
405
|
-
return await createImageBitmap(blob);
|
|
448
|
+
return await createImageBitmap(blob, opts);
|
|
406
449
|
} catch (err) {
|
|
407
450
|
if (blob.type.includes("svg")) {
|
|
408
451
|
throw new Error(
|
|
@@ -424,10 +467,12 @@ async function decodeImage(path) {
|
|
|
424
467
|
`mediakit: background image ${path} has no intrinsic size - an SVG needs width and height, a viewBox alone is not enough`
|
|
425
468
|
);
|
|
426
469
|
}
|
|
427
|
-
return createImageBitmap(img);
|
|
470
|
+
return createImageBitmap(img, opts);
|
|
428
471
|
}
|
|
429
472
|
async function loadBitmap(path, w, h) {
|
|
430
|
-
|
|
473
|
+
return coverCrop(await decodeImage(path), w, h);
|
|
474
|
+
}
|
|
475
|
+
async function coverCrop(src, w, h) {
|
|
431
476
|
if (src.width * h === src.height * w) return src;
|
|
432
477
|
const scale = Math.max(w / src.width, h / src.height);
|
|
433
478
|
const cw = Math.min(src.width, Math.round(w / scale));
|
|
@@ -467,6 +512,8 @@ var VideoPipeline = class {
|
|
|
467
512
|
// worker path
|
|
468
513
|
worker = null;
|
|
469
514
|
workerStats = null;
|
|
515
|
+
/** The background path the worker already holds; see backgroundForWorker. */
|
|
516
|
+
workerBackgroundPath = null;
|
|
470
517
|
// canvas path
|
|
471
518
|
video = null;
|
|
472
519
|
rvfcHandle = 0;
|
|
@@ -489,7 +536,7 @@ var VideoPipeline = class {
|
|
|
489
536
|
this.stopped = false;
|
|
490
537
|
if (hasInsertableStreams() && typeof Worker !== "undefined" && typeof OffscreenCanvas !== "undefined") {
|
|
491
538
|
try {
|
|
492
|
-
await this.startWorker(source, width, height);
|
|
539
|
+
await this.startWorker(source, width, height, await this.backgroundForWorker(this.filter));
|
|
493
540
|
this.transport = "worker";
|
|
494
541
|
return;
|
|
495
542
|
} catch (e) {
|
|
@@ -517,7 +564,9 @@ var VideoPipeline = class {
|
|
|
517
564
|
}
|
|
518
565
|
async pushRecipe() {
|
|
519
566
|
if (this.worker) {
|
|
520
|
-
|
|
567
|
+
const filter = this.filter;
|
|
568
|
+
const background = await this.backgroundForWorker(filter);
|
|
569
|
+
this.send({ t: "recipe", filter, effects: this.effects, background }, background ? [background] : []);
|
|
521
570
|
return;
|
|
522
571
|
}
|
|
523
572
|
await this.engine?.setRecipe(this.filter, this.effects);
|
|
@@ -543,6 +592,17 @@ var VideoPipeline = class {
|
|
|
543
592
|
this.engine = null;
|
|
544
593
|
}
|
|
545
594
|
// ── worker ───────────────────────────────────────────────────────────────────────
|
|
595
|
+
/**
|
|
596
|
+
* The image background decoded HERE, for the worker: it has no `Image`, so it cannot decode
|
|
597
|
+
* an SVG, which is every background meet bundles. Only when the path changed - the worker
|
|
598
|
+
* Engine keeps the last one by path, and a slider drag must not decode it again.
|
|
599
|
+
*/
|
|
600
|
+
async backgroundForWorker(filter) {
|
|
601
|
+
if (filter.kind !== "image" || filter.imagePath === this.workerBackgroundPath) return void 0;
|
|
602
|
+
const bmp = await decodeImage(filter.imagePath);
|
|
603
|
+
this.workerBackgroundPath = filter.imagePath;
|
|
604
|
+
return bmp;
|
|
605
|
+
}
|
|
546
606
|
send(m, transfer = []) {
|
|
547
607
|
this.worker?.postMessage(m, transfer);
|
|
548
608
|
}
|
|
@@ -552,6 +612,7 @@ var VideoPipeline = class {
|
|
|
552
612
|
this.worker.terminate();
|
|
553
613
|
this.worker = null;
|
|
554
614
|
this.workerStats = null;
|
|
615
|
+
this.workerBackgroundPath = null;
|
|
555
616
|
}
|
|
556
617
|
/**
|
|
557
618
|
* Give the worker the frame STREAMS, and keep the track on this thread.
|
|
@@ -580,7 +641,7 @@ var VideoPipeline = class {
|
|
|
580
641
|
* so it is the Chrome dialect. A browser that only has them inside a worker (Safari) does
|
|
581
642
|
* not get this path and stays on the canvas transport until someone can test that.
|
|
582
643
|
*/
|
|
583
|
-
startWorker(source, width, height) {
|
|
644
|
+
startWorker(source, width, height, background) {
|
|
584
645
|
const processor = new MediaStreamTrackProcessor({ track: source });
|
|
585
646
|
const generator = new MediaStreamTrackGenerator({
|
|
586
647
|
kind: "video",
|
|
@@ -630,9 +691,10 @@ var VideoPipeline = class {
|
|
|
630
691
|
width,
|
|
631
692
|
height,
|
|
632
693
|
filter: this.filter,
|
|
633
|
-
effects: this.effects
|
|
694
|
+
effects: this.effects,
|
|
695
|
+
background
|
|
634
696
|
},
|
|
635
|
-
[processor.readable, generator.writable]
|
|
697
|
+
background ? [processor.readable, generator.writable, background] : [processor.readable, generator.writable]
|
|
636
698
|
);
|
|
637
699
|
} catch (err) {
|
|
638
700
|
fail(err instanceof Error ? err : new Error(String(err)));
|
|
@@ -714,9 +776,12 @@ var VideoPipeline = class {
|
|
|
714
776
|
export {
|
|
715
777
|
Engine,
|
|
716
778
|
NO_EFFECTS,
|
|
779
|
+
TOUCH_UP,
|
|
717
780
|
VideoPipeline,
|
|
718
781
|
mediakitSupported,
|
|
719
782
|
needsFace,
|
|
720
|
-
needsMask
|
|
783
|
+
needsMask,
|
|
784
|
+
touchUp,
|
|
785
|
+
touchUpAmount
|
|
721
786
|
};
|
|
722
787
|
//# sourceMappingURL=index.js.map
|