-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine-3d.js
More file actions
386 lines (354 loc) · 15.5 KB
/
Copy pathengine-3d.js
File metadata and controls
386 lines (354 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* @module engine-3d
* @description
* 3D biometric pipeline for Ghostati.
*
* Parallel to engine.js (face-api / 128-D / euclidean distance), this module
* implements the ImageEmbedder path:
* - feature embedding extracted from a canvas or video element
* - Cosine similarity as the distance metric (higher = more similar)
* - Separate localStorage DB (state.db3d) sharing IDs with the 2D DB
* - MATCH_THRESHOLD_3D: similarity >= threshold counts as a match
*
* IMPORTANT — what this is NOT:
* The embedder was trained for generic image similarity, not face recognition.
* It responds strongly to global visual context (background, lighting,
* colour palette) as much as to facial geometry. False positives on
* similar backgrounds are expected and are part of the didactic point:
* this is a "feature-level" engine, not a biometric one.
*
* This module does NOT dispatch `matchStateChanged`. All events are
* dispatched by the orchestrator in main.js / auto-find-loop.js after
* composing the unified payload from both engines.
*
*/
import { state } from './state.js';
import { els } from './dom.js';
import { setLog } from './utils.js';
import { persistDb3d } from './db.js';
import { MEDIAPIPE_IMAGE_EMBEDDER_URL, MEDIAPIPE_TASKS_VISION_URL, MEDIAPIPE_WASM_URL } from './config.js';
import { t } from './i18n.js';
// ─────────────────────────────────────────────
// Model lifecycle
// ─────────────────────────────────────────────
/**
* Load the 3D embedder and store it on state.
* Safe to call multiple times — returns immediately if already loaded.
*
* Historical note: the exported name stays `loadMobileNet()` to preserve
* the public API used by main.js, even though the implementation now loads
* MediaPipe ImageEmbedder with MobileNetV3 Small.
*
* @param {object|null} embedderInstance Optional injected embedder for tests.
* @returns {Promise<void>}
* @see main.js – called during init after face-api models are loaded.
*/
export async function loadMobileNet(embedderInstance = null) {
if (state.imageEmbedder) return;
if (embedderInstance) {
state.imageEmbedder = embedderInstance;
return;
}
const { FilesetResolver, ImageEmbedder } = await import(MEDIAPIPE_TASKS_VISION_URL);
const vision = await FilesetResolver.forVisionTasks(MEDIAPIPE_WASM_URL);
state.imageEmbedder = await ImageEmbedder.createFromOptions(vision, {
baseOptions: {
modelAssetPath: MEDIAPIPE_IMAGE_EMBEDDER_URL,
delegate: 'GPU'
},
runningMode: 'VIDEO'
});
setLog(t('image_embedder_ready_log'), 'engine-3d');
}
// ─────────────────────────────────────────────
// Core math
// ─────────────────────────────────────────────
/**
* Cosine similarity between two numeric vectors.
* Returns a value in [-1, 1]; 1 = identical direction.
* Higher values mean MORE similar (opposite sign convention from L2 distance).
*
* @param {number[]} vecA
* @param {number[]} vecB
* @returns {number}
*/
export function cosineSimilarity(vecA, vecB) {
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < vecA.length; i++) {
dot += vecA[i] * vecB[i];
normA += vecA[i] * vecA[i];
normB += vecB[i] * vecB[i];
}
const denom = Math.sqrt(normA) * Math.sqrt(normB);
return denom === 0 ? 0 : dot / denom;
}
// ─────────────────────────────────────────────
// Embedding extraction
// ─────────────────────────────────────────────
/**
* Extract an ImageEmbedder embedding from a canvas or video element.
*
* NOTE: ImageEmbedder responds to global image content (background, colours,
* texture) as much as to the face itself. This is a known characteristic
* and is the didactic subject of the workshop.
*
* @param {HTMLCanvasElement|HTMLVideoElement} source
* @returns {Promise<number[]>} Embedding vector serialisable to JSON.
*/
export async function getFaceEmbedding(source) {
if (!state.imageEmbedder) throw new Error(t('image_embedder_not_loaded_error'));
try {
const result = state.imageEmbedder.embedForVideo(source, performance.now());
const embedding = result?.embeddings?.[0]?.floatEmbedding;
if (!embedding) {
throw new Error(t('no_embedding_returned_error'));
}
return Array.from(embedding);
} catch (err) {
console.error(t('console_get_face_embedding_error'), err);
throw new Error(t('embedding_extraction_error', { message: err.message }));
}
}
// ─────────────────────────────────────────────
// DB operations
// ─────────────────────────────────────────────
/**
* Scan the 3D DB for the best cosine-similarity match against `embedding`.
*
* @param {number[]} embedding Query vector.
* @returns {{ liveMaxSim: number|null, liveMaxId: number|null }}
*/
export function seekFaceInDb3d(embedding) {
if (!state.db3d || state.db3d.faces.length === 0) {
return { liveMaxSim: null, liveMaxId: null };
}
let maxSim = -Infinity, maxId = null;
for (const rec of state.db3d.faces) {
const sim = cosineSimilarity(embedding, rec.descriptor3d);
if (sim > maxSim) { maxSim = sim; maxId = rec.id; }
}
return { liveMaxSim: maxSim, liveMaxId: maxId };
}
/**
* Save an ImageEmbedder embedding to state.db3d under the given `id`.
* The `id` is assigned by engine.js (saveFace) and passed here so both
* DBs stay in sync without a shared counter.
*
* @param {number} id ID already assigned by the 2D engine's saveFace.
* @returns {Promise<{ id: number, liveInfo3d: object }|null>}
* Returns null if model not ready or DB not initialised.
*/
export async function saveFace3d(id) {
if (!state.imageEmbedder) {
setLog(t('image_embedder_not_ready_save_log'), 'engine-3d');
return null;
}
if (!state.db3d) {
setLog(t('db_3d_not_initialized_log'), 'engine-3d');
return null;
}
let embedding;
try {
embedding = await getFaceEmbedding(els.video);
} catch (err) {
setLog(t('embedding_extraction_log', { message: err.message }), 'engine-3d');
return null;
}
state.db3d.faces.push({
id,
descriptor3d: embedding,
savedAt: new Date().toISOString(),
});
persistDb3d();
setLog(t('image_embedder_saved_log', { id }), 'engine-3d');
// After saving, seek to get liveInfo (best match is the record we just saved: sim ≈ 1)
const liveInfo3d = seekFaceInDb3d(embedding);
return { id, liveInfo3d };
}
// ─────────────────────────────────────────────
// Compositing (3D efficacy test)
// ─────────────────────────────────────────────
/**
* Helper: is any 2D or 3D plugin currently active?
* Mirrors hasActivePlugin() from engine.js without importing it.
* @returns {boolean}
*/
function hasActivePlugin3d() {
const G = window.gstmxx;
const a2d = typeof G?.getActiveEffect === 'function' && G.getActiveEffect();
const a3d = typeof G?.getActiveEffect3d === 'function' && G.getActiveEffect3d();
return !!(a2d || a3d);
}
/**
* Build a composited canvas and extract an ImageEmbedder embedding from it.
*
* The base of the composite is, in order of preference:
* 1. `baseCanvas` — the 2D-composited frame from engine.js's
* `compositeAndDetect` (video + active 2D Ghostyle already baked in). This
* is what makes a 2D overlay such as the Face Brush actually move the 3D
* similarity score: the embedder must see the *painted* pixels, not the
* bare video. Without it the ImageEmbedder embeds the un-obfuscated frame
* and reports a match no matter how much the user paints.
* 2. the raw video frame, when no 2D composite was supplied (e.g. only a
* 3D/UV plugin is active).
*
* On top of that base it dispatches `beforeEfficacyComposite3d` so 3D plugins
* can draw before the embedding is extracted. Plugins receive:
* `{ canvas, ctx, landmarks3d: state.lastLandmarks3d }`
*
* The base is copied into a fresh canvas (never mutated in place), so passing
* the 2D path's canvas here is safe even though 3D plugins may draw on top.
*
* @param {HTMLCanvasElement|null} [baseCanvas=null] Pre-composited 2D frame to embed.
* @returns {Promise<{ canvas: HTMLCanvasElement, embedding: number[] }|null>}
* Returns null if model not ready.
*/
export async function compositeAndDetect3d(baseCanvas = null) {
if (!state.imageEmbedder) return null;
const canvas = document.createElement('canvas');
canvas.width = els.overlay.width;
canvas.height = els.overlay.height;
const ctx = canvas.getContext('2d');
// Prefer the 2D-composited frame (video + 2D Ghostyle) so 2D makeup is
// reflected in the embedding; otherwise fall back to the raw video frame.
const source = baseCanvas || els.video;
ctx.drawImage(source, 0, 0, canvas.width, canvas.height);
// Allow 3D ghostyle plugins to draw onto this canvas before embedding.
state.gstmxxEvents.dispatchEvent(new CustomEvent('beforeEfficacyComposite3d', {
detail: { canvas, ctx, landmarks3d: state.lastLandmarks3d }
}));
const embedding = await getFaceEmbedding(canvas);
return { canvas, embedding };
}
// ─────────────────────────────────────────────
// Find pipeline
// ─────────────────────────────────────────────
/**
* Run the full 3D find pipeline:
* 1. Extract embedding from the current video frame.
* 2. Find best cosine-similarity match in state.db3d.
* 3. If a plugin is active, also extract composite embedding.
* 4. Return raw metrics for the orchestrator to compose into the
* unified `matchStateChanged` payload — does NOT dispatch any event.
*
* Returns null if:
* - ImageEmbedder not loaded yet
* - state.db3d is empty (3D DB has no entries)
*
* Returns `{ liveInfo3d, composite3d }` otherwise, where composite3d is
* null when no plugin is active.
*
* @param {HTMLCanvasElement|null} [baseCanvas=null] 2D-composited frame (video +
* active 2D Ghostyle) to embed for the composite comparison. Passed straight
* through to `compositeAndDetect3d`; when omitted the composite falls back to
* the raw video frame.
* @returns {Promise<{ liveInfo3d: object, composite3d: object|null }|null>}
*/
export async function findFace3d(baseCanvas = null) {
if (!state.imageEmbedder) {
setLog(t('image_embedder_not_ready_compare_log'), 'engine-3d');
return null;
}
if (!state.db3d || state.db3d.faces.length === 0) {
return { liveInfo3d: { liveMaxSim: null, liveMaxId: null }, composite3d: null };
}
let liveEmbedding;
try {
liveEmbedding = await getFaceEmbedding(els.video);
} catch (err) {
setLog(t('live_embedding_extraction_log', { message: err.message }), 'engine-3d');
return null;
}
const liveInfo3d = seekFaceInDb3d(liveEmbedding);
const composite3d = hasActivePlugin3d() ? await compositeAndDetect3d(baseCanvas) : null;
return { liveInfo3d, composite3d };
}
// ─────────────────────────────────────────────
// Match decision
// ─────────────────────────────────────────────
/**
* Decide match state for the 3D (cosine-similarity) engine.
*
* Cosine similarity convention: HIGHER = MORE SIMILAR.
* threshold: similarity >= MATCH_THRESHOLD_3D → matched.
*
* When a ghostyle is present, the obfuscated embedding is the primary signal
* (same rationale as engine.js's decideMatchState for the 2D case).
*
* @param {object} params
* @param {number|null} params.liveMaxSim Best live similarity (highest in DB).
* @param {number|null} params.liveMaxId ID of best live match.
* @param {number|null} params.obfMaxSim Best composite similarity (null if no plugin).
* @param {number|null} params.obfMaxId ID of best composite match.
* @returns {{ detectionState: string, headline: string }}
*/
export function decideMatchState3d({ liveMaxSim, liveMaxId, obfMaxSim, obfMaxId }) {
const thr = state.MATCH_THRESHOLD_3D;
if (obfMaxSim != null) {
// Ghostyle active: judge on composite embedding
if (typeof obfMaxId === 'number' && obfMaxSim >= thr) {
return {
detectionState: 'matched',
headline: t('match_3d_ghostyle_matched_headline', { id: obfMaxId, similarity: obfMaxSim.toFixed(3), threshold: thr.toFixed(2) }),
};
}
return {
detectionState: 'eluded',
headline: t('match_3d_ghostyle_eluded_headline', { similarity: (obfMaxSim ?? 0).toFixed(3), threshold: thr.toFixed(2) }),
};
}
// No ghostyle: judge on live embedding
if (liveMaxSim == null) {
return { detectionState: 'unknown', headline: t('match_3d_empty_db_headline') };
}
if (liveMaxSim >= thr) {
return {
detectionState: 'matched',
headline: t('match_3d_live_matched_headline', { id: liveMaxId, similarity: liveMaxSim.toFixed(3), threshold: thr.toFixed(2) }),
};
}
return {
detectionState: 'eluded',
headline: t('match_3d_live_eluded_headline', { similarity: liveMaxSim.toFixed(3), threshold: thr.toFixed(2) }),
};
}
/**
* Build the `mediapipe` section of the unified `matchStateChanged` payload.
*
* NOTE: the field is named `mediapipe` in the payload (matching the landmark
* source) because both tasks are powered by MediaPipe. The two models run in the
* same MediaPipe-initiated pipeline.
*
* @param {{ liveInfo3d: object, composite3d: object|null }} result3d
* As returned by findFace3d().
* @returns {{ detectionState, headline, liveMaxSim, liveMaxId, obfMaxSim, obfMaxId }|null}
* Returns null if liveInfo3d is fully empty (model not ready / no records).
*/
export function evaluateMatch3d(result3d) {
if (!result3d) return null;
const { liveInfo3d, composite3d } = result3d;
if (!liveInfo3d) return null;
const { liveMaxSim, liveMaxId } = liveInfo3d;
let obfMaxSim = null, obfMaxId = null;
if (composite3d?.embedding) {
const obfInfo = seekFaceInDb3d(composite3d.embedding);
obfMaxSim = obfInfo.liveMaxSim;
obfMaxId = obfInfo.liveMaxId;
}
const { detectionState, headline } = decideMatchState3d({ liveMaxSim, liveMaxId, obfMaxSim, obfMaxId });
// The similarity that drove the decision (composite if present, live otherwise)
const similarity = obfMaxSim ?? liveMaxSim;
const matchedId = obfMaxSim != null
? (obfMaxSim >= state.MATCH_THRESHOLD_3D ? obfMaxId : null)
: (liveMaxSim != null && liveMaxSim >= state.MATCH_THRESHOLD_3D ? liveMaxId : null);
return {
detectionState,
headline,
similarity,
matchedId,
liveMaxSim,
liveMaxId,
obfMaxSim,
obfMaxId,
};
}