FaceState turns one selfie into playful, personalized versions of the same face: after an all-nighter, at a younger or older age, or as a fantasy character.
Generic filters apply the same effect to everyone. FaceState first runs YouCam HD Skin Analysis and uses the resulting dark-circle, eye-bag, radiance, wrinkle, texture, firmness, and other scores as the baseline for each state. The output is playful, but the way an effect ramps onto the face is driven by that person's measured skin data.
Built for the YouCam API Skin AI & Apparel VTO Hackathon (Skin AI track).
| State | Implementation |
|---|---|
| All-Nighter | Uses the analysis masks to locate the under-eye and face regions, then synthesizes shadows and a dulling effect from the selfie's own pixels in Canvas 2D. Diagnostic mask colors are never copied into the result. |
| Time Jump | Starts one YouCam Aging task and downloads its 16-rung age ladder. The client inserts the original selfie at the measured skin age and crossfades between the surrounding rungs for a continuous −20 to +20 year control. |
| Character | Pads the selfie to satisfy the avatar face detector, uploads it for the avatar feature, and runs the selected YouCam template (Fantasy, Viking, Sci-Fi, or Royal). |
All-Nighter intensity is personalized with a severity-weighted blend curve calculated from its driver scores. Time Jump direction and level, and All-Nighter's three effect levels, are rendered locally after the full-strength result is prepared. Changing a level never starts another API task. Character outputs are shown at full strength.
flowchart LR
Browser["Browser<br/>capture, Zustand state, Canvas studio"]
Cache[("Memory + IndexedDB<br/>analysis and transform cache")]
Routes["Next.js API routes<br/>analyze, age, avatar, simulate"]
ClientRender["Canvas 2D<br/>composite + share card"]
Adapter["lib/youcam<br/>upload, validate, poll, download"]
YouCam["YouCam S2S V2 API"]
Mock["Fixtures + mock filters"]
Browser -->|"processed selfie / state request"| Routes
Browser <--> Cache
Routes --> Adapter
Adapter <-->|"signed upload + async tasks"| YouCam
Mock --> Routes
Routes -->|"scores, masks, image data"| Browser
Browser --> ClientRender
The browser owns capture, interaction, compositing, and sharing. Next.js route handlers are the
only public boundary for analysis and transformation requests, while lib/youcam/ is the only
module that communicates with YouCam. The API key is read only on the server.
-
Capture and prepare the selfie. The capture screen accepts a camera frame or local image, corrects orientation, detects the face region, crops to the presented frame, and re-encodes it as JPEG. The canvas re-encode removes EXIF metadata, the long side is capped at 2048 px, and small images are upscaled when necessary to meet HD Skin Analysis dimensions. A face-aware retry can tighten the crop once if YouCam reports that the face is too small.
-
Analyze the measured baseline.
POST /api/analyzekeeps the uploaded bytes in memory and callsanalyzeSkin(). In live mode, the server registers the file, uploads it through the returned signed URL, starts an HD analysis task for all eight concerns, and polls it to a terminal state. The returned ZIP is downloaded immediately and unpacked withfflate. -
Normalize the result. Zod schemas validate YouCam's upload and task envelopes. The server extracts
score_info.json, normalizes raw and UI scores, reads the measured skin age, and returns only the concern masks needed by the local compositor plus their pixel-aligned base image. Missing concern scores fall back to a neutral value so one absent field does not break the studio. -
Build the selected state. State definitions in
lib/states/recipes.tsdeclare their score drivers, controls, templates, and task payloads. All-Nighter builds a reusable pixel-delta layer locally from mask geometry. Time Jump calls/api/ageonce for the complete age ladder. Character calls/api/avatarwith a separately padded image because YouCam uploads are feature-scoped and tight portraits can fail avatar detection./api/simulateremains the fallback for analyses without the masks required by All-Nighter. -
Render and share locally. The studio calculates severity from raw analysis scores and maps the selected level through a personalized alpha curve. Canvas 2D blends the selfie with a transform, applies a precomputed mask delta, or crossfades aging rungs. Holding the preview reveals the original. Sharing renders a 1080×1350 before/after JPEG in the browser and opens the mobile share sheet or downloads the file on desktop.
-
Avoid duplicate API spend. Analyses are keyed by a fingerprint of the processed selfie; transforms are keyed by
(fileId, state, variant). Both are cached in memory and IndexedDB, and concurrent identical requests share one in-flight promise. Late responses are tagged with their request key, so switching states cannot let a stale result repaint the current preview.
- HD Skin Analysis: implemented for all eight concerns, including scores, skin age, and masks.
- AI Aging Generator: implemented as a single task returning the complete age ladder.
- AI Avatar Generator: implemented for four validated character templates.
- AI Skin Simulation: adapter and fallback route are scaffolded, but the feature is disabled for live calls until its payload/tier is verified. The shipped All-Nighter path uses the HD analysis masks and local compositor instead.
In mock mode, the analysis response and masks come from the committed fixtures/ capture, while
state transforms use deterministic CSS-filter recipes. This exercises the same normalization,
state, cache, and rendering paths without spending API units.
| Path | Responsibility |
|---|---|
app/capture/ |
Camera/file input, face-aware framing, preview, and analysis flow |
app/studio/ |
State controls, async result selection, canvas preview, and sharing |
app/api/ |
Server-only request validation and analysis/transform endpoints |
lib/youcam/ |
YouCam configuration, HTTP client, uploads, task polling, schemas, and mocks |
lib/states/ |
State recipes, score drivers, and transform plans |
lib/render/ |
Blend math, mask-derived effects, compositing, and share-card rendering |
lib/client/ |
Zustand session state, selfie preparation, caching, and in-flight deduplication |
fixtures/ |
Captured mock analysis payloads and pixel-aligned masks |
pnpm install
cp .env.example .env.local
pnpm devOpen http://localhost:3000. MOCK_YOUCAM=1 is the default, so the app
runs without a YouCam key or API usage. For live mode, set MOCK_YOUCAM=0 and provide a
server-side YOUCAM_API_KEY. Live unit usage is recorded in docs/unit-log.md.
Run the checks with:
pnpm typecheck
pnpm lint
pnpm testSelfies are never written to the FaceState server's filesystem. They pass through route handlers in memory and, in live mode, through YouCam's transient upload flow. Image orientation is applied and EXIF metadata, including GPS fields, is removed in the browser before upload. The browser's IndexedDB cache stores normalized analysis and generated results on the user's device to prevent duplicate API calls.
For fun and general skin information — not medical advice.
Contributions are welcome; see CONTRIBUTING.md for the mock-first development workflow and pull-request checks. Please report vulnerabilities privately as described in SECURITY.md, and never include credentials or personal images in a public issue.
Live mode requires separately issued YouCam credentials and is subject to Perfect Corp.'s terms. The MIT license does not grant rights to Perfect Corp.'s services or trademarks. Committed API fixtures contain sanitized, expired response shapes and an AI-generated sample face; never commit live signed URLs or a real user's selfie.
FaceState is available under the MIT License.