Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(selfie): reject a blown-out face crop instead of passing it as well lit

nirholas1d182dc9bfd6
diff --git a/src/face-quality.js b/src/face-quality.js
index 3b09048bd..0e980199f 100644
--- a/src/face-quality.js
+++ b/src/face-quality.js
@@ -83,6 +83,7 @@ export { SLOT_PRESETS };
* @property {number|null} roll
* @property {number} blur
* @property {number} luma
+ * @property {number} clippedFrac Share of the face crop blown to near-white.
* @property {string|null} reason User-facing retake prompt; null when allPass.
* @property {Array|null} landmarks
*/
@@ -166,7 +167,7 @@ export async function createQualitySession(videoEl, canvasEl, opts = {}) {
return {
...gradeFrame({ faceFound: false }),
yaw: null, pitch: null, roll: null,
- blur: 0, luma: 0, landmarks: null,
+ blur: 0, luma: 0, clippedFrac: 0, landmarks: null,
};
}
@@ -184,6 +185,7 @@ export async function createQualitySession(videoEl, canvasEl, opts = {}) {
noseY: nose ? nose.y : null,
blur: q.blur,
luma: q.luma,
+ clippedFrac: q.clippedFrac,
});
return {
@@ -193,6 +195,7 @@ export async function createQualitySession(videoEl, canvasEl, opts = {}) {
roll: pose.roll,
blur: q.blur,
luma: q.luma,
+ clippedFrac: q.clippedFrac,
landmarks: lms,
};
}
@@ -242,7 +245,7 @@ export async function createQualitySession(videoEl, canvasEl, opts = {}) {
const fy = Math.max(0, (minY - pad)) * vh;
const fw = Math.min(vw - fx, (maxX - minX + 2 * pad) * vw);
const fh = Math.min(vh - fy, (maxY - minY + 2 * pad) * vh);
- if (fw < 4 || fh < 4) return { luma: 0, blur: 0 };
+ if (fw < 4 || fh < 4) return { luma: 0, blur: 0, clippedFrac: 0 };
_qCtx.drawImage(video, fx, fy, fw, fh, 0, 0, SIZE, SIZE);
const { data: d } = _qCtx.getImageData(0, 0, SIZE, SIZE);
@@ -250,8 +253,8 @@ export async function createQualitySession(videoEl, canvasEl, opts = {}) {
for (let i = 0, j = 0; i < d.length; i += 4, j++) {
grays[j] = 0.299 * d[i] + 0.587 * d[i + 1] + 0.114 * d[i + 2];
}
- const { luma, blurStddev } = grayFaceStats(grays, SIZE, SIZE);
- return { luma, blur: blurStddev };
+ const { luma, blurStddev, clippedFrac } = grayFaceStats(grays, SIZE, SIZE);
+ return { luma, blur: blurStddev, clippedFrac };
}
return session;
@@ -308,7 +311,7 @@ export async function checkImageQuality(source, slot = 'frontal') {
return {
...gradeFrame({ faceFound: false }),
yaw: null, pitch: null, roll: null,
- blur: 0, luma: 0, landmarks: null,
+ blur: 0, luma: 0, clippedFrac: 0, landmarks: null,
};
}
@@ -324,6 +327,7 @@ export async function checkImageQuality(source, slot = 'frontal') {
noseY: nose ? nose.y : null,
blur: q.blur,
luma: q.luma,
+ clippedFrac: q.clippedFrac,
});
return {
@@ -333,6 +337,7 @@ export async function checkImageQuality(source, slot = 'frontal') {
roll: pose.roll,
blur: q.blur,
luma: q.luma,
+ clippedFrac: q.clippedFrac,
landmarks: lms,
};
}
@@ -350,7 +355,7 @@ function measureStillFace(source, lms) {
const SIZE = 64;
const sw = source.videoWidth || source.naturalWidth || source.width;
const sh = source.videoHeight || source.naturalHeight || source.height;
- if (!sw || !sh) return { luma: 0, blur: 0 };
+ if (!sw || !sh) return { luma: 0, blur: 0, clippedFrac: 0 };
let minX = 1, minY = 1, maxX = 0, maxY = 0;
for (const lm of lms) {
if (lm.x < minX) minX = lm.x;
@@ -363,18 +368,18 @@ function measureStillFace(source, lms) {
const fy = Math.max(0, minY - pad) * sh;
const fw = Math.min(sw - fx, (maxX - minX + 2 * pad) * sw);
const fh = Math.min(sh - fy, (maxY - minY + 2 * pad) * sh);
- if (fw < 4 || fh < 4) return { luma: 0, blur: 0 };
+ if (fw < 4 || fh < 4) return { luma: 0, blur: 0, clippedFrac: 0 };
const canvas = document.createElement('canvas');
canvas.width = canvas.height = SIZE;
const ctx = canvas.getContext('2d', { willReadFrequently: true });
- if (!ctx) return { luma: 0, blur: 0 };
+ if (!ctx) return { luma: 0, blur: 0, clippedFrac: 0 };
ctx.drawImage(source, fx, fy, fw, fh, 0, 0, SIZE, SIZE);
const { data: d } = ctx.getImageData(0, 0, SIZE, SIZE);
const grays = new Float32Array(SIZE * SIZE);
for (let i = 0, j = 0; i < d.length; i += 4, j++) {
grays[j] = 0.299 * d[i] + 0.587 * d[i + 1] + 0.114 * d[i + 2];
}
- const { luma, blurStddev } = grayFaceStats(grays, SIZE, SIZE);
- return { luma, blur: blurStddev };
+ const { luma, blurStddev, clippedFrac } = grayFaceStats(grays, SIZE, SIZE);
+ return { luma, blur: blurStddev, clippedFrac };
}
diff --git a/src/selfie-gates.js b/src/selfie-gates.js
index d6300b0d2..c83103d2d 100644
--- a/src/selfie-gates.js
+++ b/src/selfie-gates.js
@@ -52,12 +52,16 @@ export const SLOT_PRESETS = Object.freeze({
export const GATES = Object.freeze({
/** Laplacian-response stddev floor on the 64px live face crop. */
- BLUR_STDDEV_MIN: 3.5,
+ BLUR_STDDEV_MIN: 12,
/** Variance-of-Laplacian floor for still photos at up to 1024px. */
SHARPNESS_VAR_MIN: 90,
/** Mean face luma bounds (Rec. 601, 0..255). */
LUMA_MIN: 40,
LUMA_MAX: 218,
+ /** Max share of the face crop allowed to be blown to near-white. */
+ CLIPPED_FRAC_MAX: 0.18,
+ /** A face pixel at or above this luma carries no recoverable texture. */
+ CLIP_LEVEL: 250,
/** Max nose offset from frame centre, as a fraction of frame size. */
CENTER_TOL_X: 0.22,
CENTER_TOL_Y: 0.28,
@@ -66,24 +70,34 @@ export const GATES = Object.freeze({
});
/**
- * Mean luma and Laplacian-response stddev of an 8-bit grey buffer.
- * This is the live viewfinder's blur/lighting measurement, extracted pure so
- * it can be pinned by unit tests. `blurStddev` is the standard deviation of a
- * 4-neighbour Laplacian over the interior pixels: sharp facial detail yields
- * strong high-frequency response, defocus and motion blur collapse it.
+ * Mean luma, blown-highlight share, and Laplacian-response stddev of an 8-bit
+ * grey buffer. This is the live viewfinder's blur/lighting measurement,
+ * extracted pure so it can be pinned by unit tests. `blurStddev` is the
+ * standard deviation of a 4-neighbour Laplacian over the interior pixels:
+ * sharp facial detail yields strong high-frequency response, defocus and
+ * motion blur collapse it. `clippedFrac` is the share of pixels at or above
+ * CLIP_LEVEL, which mean luma cannot see: a face half-blown by window glare
+ * still averages into the accepted band while carrying no texture where it
+ * counts, and clipping raises the Laplacian response rather than lowering it,
+ * so the blur gate cannot catch it either.
*
* @param {Uint8Array|Uint8ClampedArray|Float32Array|number[]} grey row-major, length w*h
* @param {number} w
* @param {number} h
- * @returns {{ luma: number, blurStddev: number }}
+ * @returns {{ luma: number, blurStddev: number, clippedFrac: number }}
*/
export function grayFaceStats(grey, w, h) {
const n = w * h;
- if (n === 0) return { luma: 0, blurStddev: 0 };
+ if (n === 0) return { luma: 0, blurStddev: 0, clippedFrac: 0 };
let lumaSum = 0;
- for (let i = 0; i < n; i++) lumaSum += grey[i];
+ let clipped = 0;
+ for (let i = 0; i < n; i++) {
+ lumaSum += grey[i];
+ if (grey[i] >= GATES.CLIP_LEVEL) clipped++;
+ }
const luma = lumaSum / n;
- if (w < 3 || h < 3) return { luma, blurStddev: 0 };
+ const clippedFrac = clipped / n;
+ if (w < 3 || h < 3) return { luma, blurStddev: 0, clippedFrac };
let ls = 0;
let ls2 = 0;
let ln = 0;
@@ -103,7 +117,7 @@ export function grayFaceStats(grey, w, h) {
}
const mean = ls / ln;
const blurStddev = Math.sqrt(Math.max(0, ls2 / ln - mean * mean));
- return { luma, blurStddev };
+ return { luma, blurStddev, clippedFrac };
}
/**
@@ -117,6 +131,7 @@ export function grayFaceStats(grey, w, h) {
* noseX?: number|null, noseY?: number|null,
* blur?: number,
* luma?: number,
+ * clippedFrac?: number,
* }} m `blur` is the grayFaceStats blurStddev; noseX/noseY are normalised 0..1.
* @returns {{
* faceFound: boolean, yawOk: boolean, centered: boolean,
@@ -146,7 +161,10 @@ export function gradeFrame(m) {
Math.abs(m.noseX - 0.5) < GATES.CENTER_TOL_X &&
Math.abs(m.noseY - 0.5) < GATES.CENTER_TOL_Y;
const blurOk = (m.blur ?? 0) >= GATES.BLUR_STDDEV_MIN;
- const lumaOk = (m.luma ?? 0) >= GATES.LUMA_MIN && (m.luma ?? 0) <= GATES.LUMA_MAX;
+ const tooDark = (m.luma ?? 0) < GATES.LUMA_MIN;
+ const blownOut =
+ (m.luma ?? 0) > GATES.LUMA_MAX || (m.clippedFrac ?? 0) > GATES.CLIPPED_FRAC_MAX;
+ const lumaOk = !tooDark && !blownOut;
let reason = null;
if (!yawOk) {
@@ -158,11 +176,10 @@ export function gradeFrame(m) {
reason = 'Center your face in the oval.';
} else if (!blurOk) {
reason = 'Hold steady. The image is blurry.';
- } else if (!lumaOk) {
- reason =
- (m.luma ?? 0) < GATES.LUMA_MIN
- ? 'Too dark. Find better light.'
- : 'Too bright. Reduce glare.';
+ } else if (tooDark) {
+ reason = 'Too dark. Find better light.';
+ } else if (blownOut) {
+ reason = 'Too bright. Move out of direct light or turn away from the window.';
}
return {