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(avatar): the photographed face has to end up facing the camera

nirholasf42b807232ca
diff --git a/workers/avatar-reconstruction/face_projection.py b/workers/avatar-reconstruction/face_projection.py
index a0d2d3256..5b903c078 100644
--- a/workers/avatar-reconstruction/face_projection.py
+++ b/workers/avatar-reconstruction/face_projection.py
@@ -182,8 +182,14 @@ class WeakPerspectiveCamera:
# the bottom of the photo, which reads as "nothing is visible" rather
# than as an error.
px = np.stack([mapped[:, 0], -mapped[:, 1]], axis=1)
- # MediaPipe's z is smaller nearer the camera, so it is already a depth.
- return px, mapped[:, 2]
+ # Depth sign is determined empirically, not from MediaPipe's documented z
+ # convention: Umeyama's reflection guard forces a proper rotation, which
+ # can absorb a sign flip in the correspondence and leave the fitted frame
+ # mirrored in z relative to the raw landmarks. The check that settles it
+ # is that the face must come out facing the camera; with the opposite
+ # sign the face oval measures -0.78 against the view axis, i.e. the
+ # pipeline believes the front of the head is pointing away.
+ return px, -mapped[:, 2]
def view_direction(self) -> np.ndarray:
"""
@@ -192,10 +198,13 @@ class WeakPerspectiveCamera:
Constant across the head under an orthographic model, which is the point:
there is no camera centre to be wrong about, only a viewing axis.
"""
- # Depth grows away from the camera, so the camera lies toward -z.
- axis = self.R.T @ np.array([0.0, 0.0, -1.0])
+ # Paired with the depth sign above: the camera lies toward +z in the
+ # fitted frame. Verified by the face oval measuring +0.78 against this
+ # axis on the reference set, which is what a face pointing at the lens
+ # looks like.
+ axis = self.R.T @ np.array([0.0, 0.0, 1.0])
n = np.linalg.norm(axis)
- return axis / n if n > 0 else np.array([0.0, 0.0, -1.0])
+ return axis / n if n > 0 else np.array([0.0, 0.0, 1.0])
def _dedupe_correspondences(
@@ -319,9 +328,16 @@ def _uv_barycentric_map(
tri_map = np.full((tex_h, tex_w), -1, dtype=np.int32)
bary_map = np.zeros((tex_h, tex_w, 3), dtype=np.float32)
- # UV origin is bottom-left in glTF and top-left in an image.
+ # NO v-flip. glTF's UV origin is nominally bottom-left, so flipping looks
+ # correct in isolation, but this template's atlas and the precomputed
+ # `face_uv_map.json` both address it top-left (py = v * height) and the
+ # landmark warp composites against that. Flipping here writes the projected
+ # texture in mirrored, and makes the face-oval protect mask shield a band
+ # that is not the face. The symptom is silent: the avatar still renders, and
+ # the only tell is that projected coverage and the oval stop overlapping,
+ # which is what the coverage harness measures.
px = uvs[:, 0] * (tex_w - 1)
- py = (1.0 - uvs[:, 1]) * (tex_h - 1)
+ py = uvs[:, 1] * (tex_h - 1)
for _t, tri in enumerate(faces):
i0, i1, i2 = tri
diff --git a/workers/avatar-reconstruction/test_face_projection.py b/workers/avatar-reconstruction/test_face_projection.py
index 3ba4780e1..b65cb6a7b 100644
--- a/workers/avatar-reconstruction/test_face_projection.py
+++ b/workers/avatar-reconstruction/test_face_projection.py
@@ -145,6 +145,45 @@ def test_reaches_surface_no_landmark_describes():
print(f"ok reaches surface no landmark describes ({painted:.0%} of it painted)")
+def test_the_surface_under_the_landmarks_faces_the_camera():
+ """
+ The invariant that pins the depth and view-direction signs.
+
+ Landmarks come from a face, and a photographed face points at the lens. So
+ whatever surface the landmarks sit on MUST come out facing the camera. Get
+ the sign wrong and the pipeline believes the front of the head points away:
+ every gate then rejects the face and paints the back instead, silently, with
+ a perfectly plausible-looking coverage number.
+
+ This is not hypothetical. The shipped signs were inverted and every other
+ test here still passed, because they select their test regions BY facing and
+ so adapt to whichever sign is in force. Only an absolute claim catches it.
+ """
+ pos, nrm, uv, faces = _sphere()
+ patch = np.where((pos[:, 2] > 0.8) & (np.abs(pos[:, 1]) < 0.3))[0]
+ pts, _, _ = _synthetic_landmarks(pos, patch)
+
+ cam = fp.solve_camera(pts, patch, pos, 800, 800)
+ assert cam is not None
+ facing = nrm[patch] @ cam.view_direction()
+ assert facing.mean() > 0.5, (
+ f"the surface the landmarks sit on faces AWAY from the camera "
+ f"(mean {facing.mean():+.2f}); depth or view-direction sign is inverted"
+ )
+
+ # And it must actually be painted, not merely oriented correctly.
+ photo = np.full((800, 800, 3), 128, dtype=np.uint8)
+ _, weight, covered = fp.project_photo_to_uv(
+ photo, None, pos, nrm, uv, faces, pts, patch, 256, 256,
+ )
+ pos_map, _, _ = fp._rasterize_uv(pos, uv, faces, nrm, 256, 256)
+ near_patch = covered & (pos_map[:, :, 2] > 0.8) & (np.abs(pos_map[:, :, 1]) < 0.3)
+ assert near_patch.any(), "no texels found on the landmark patch"
+ painted = (weight[near_patch] > 0.01).mean()
+ assert painted > 0.8, f"only {painted:.0%} of the landmark surface was painted"
+ print(f"ok the surface under the landmarks faces the camera and is painted ({painted:.0%})")
+
+
def test_background_pixels_are_refused():
"""A texel projecting just off the silhouette must not pick up the wall."""
pos, nrm, uv, faces = _sphere()