Generative vision
StyleAI Studio
Swaps garments onto a real photo with diffusion models and pixel-precise segmentation, entirely on a single consumer GPU.
Overview
StyleAI Studio is an AI outfit-transformation platform, built as an internal prototype. A user uploads a photo and builds a look layer by layer: shirt, jacket, trousers, accessories. The face, body and pose are preserved, and every generation runs on one on-premise GPU with no cloud AI calls and no data leaving the machine. It is positioned as a visual styling and outfit-simulation tool, not a size-accurate fitting engine.
Result
A real captured run detected a 478-point face mesh and body landmarks, then generated a new garment end to end in around 12 seconds per apply.
Infrastructure
100% on-premise inference on a single NVIDIA RTX 4060 Ti. GGUF Q8 quantization and dynamic VRAM management keep the full pipeline running on one consumer GPU.
Before, analysis, after



Architecture
A photo is checked and analysed on the CPU. A session and prompt engine decide what to ask for, ComfyUI runs SAM3 and FLUX.2 Klein on one consumer GPU, and the result streams back to the browser as it is made. Nothing is sent to a cloud model.
Photo analysis
OpenCV layer
Instant, pure Python: resolution, orientation, brightness, contrast, sharpness, exposure, colour temperature, skin tone, background complexity and dominant colours.
MediaPipe layer
100 to 300 ms with no GPU: face detection, face landmarks (a 478-point mesh), a 33-landmark body pose, body visibility, shoulder width, torso ratio and standing or sitting.
Photo-type detection
Pose landmarks classify the photo as full body, half body or closeup. The type decides the prompt, the latent size and whether the face is masked.
Warm while you look
FLUX.2 and SAM3 load in the background during analysis (30 to 60 seconds), so the first apply does not wait on model loading.
Used twice
The checks reject bad photos before a generation is wasted, and the same data feeds the on-screen analysis while the model warms up.
Pre-checks before any generation
| Check | Method | If it fails |
|---|---|---|
| Face present | MediaPipe face detection | Asks for a photo with the face visible |
| Single person | Pose and detection | Asks for one person only |
| Front-facing | Face landmark angle | Asks for a front-facing photo |
| Body visible | Shoulder and hip keypoints | Asks for a half or full body photo |
| Resolution | Image size, 512 × 512 minimum | Asks for a higher-quality image |
| Lighting | Histogram mean and spread | Asks for better light |
| Sharpness | Laplacian variance | Asks for a clearer photo |
The wardrobe engine
Outfits are built layer by layer. Each layer has its own prompt template, and the prompt, not extra masking, controls what changes.
The prompt is the controller
No composing images, no re-masking per step. The prompt says what to do, the person image says where to start, and a session chain records every result.
Rules in a JSON file
Garment types, layers and prompt templates live in prompt_rules.json, so behaviour changes without touching code.
Dynamic prompts
A session context fills placeholders from the real outfit: “keep the upper body exactly as the pink shirt and black blazer, replace only the lower body.”
Navigation is free
Back, forward and jump-to-step restore stored images instantly. Applying a new item from mid-chain drops the forward history.
The ComfyUI workflow
- 01Load models
FLUX.2 Klein 4B (GGUF Q8), a Qwen 3 4B text encoder, the FLUX.2 VAE and SAM3.
- 02Segment the garment
SAM3 grounding at 0.2 confidence finds the clothing, and a clothes-only mask with VITMatte edge detail isolates it on the CPU, keeping VRAM for FLUX.
- 03Scale
Person and garment are scaled to about one megapixel.
- 04Encode the instruction
The prompt is encoded. The negative path is a real negative prompt, not a zeroed one, which protects colours.
- 05Reference conditioning
The person latent anchors identity and pose, then the garment latent is chained on top as the thing to transfer.
- 06Sample
Euler, 4 steps (5 for jackets), CFG 1, a latent of 832 × 1216 (832 × 960 for closeups) and a fresh seed each run.
- 07Decode and clean
VAE decode to PNG, then VRAM and RAM cleanup nodes run, so the next request starts clean.
Fitting on one GPU
The whole pipeline runs on a single RTX 4060 Ti with 16 GB. Quantisation, pre-warming, loading only what a request needs, and cleanup between runs keep it inside that budget.
| Component | Size | Role |
|---|---|---|
| FLUX.2 Klein 4B, GGUF Q8 | 4.1 GB | The generator. Quantised to Q8 so it fits beside everything else. |
| Qwen 3 4B text encoder | 7.5 GB on disk | Reads the editing instruction. |
| SAM3 | 3.3 GB | Segments garments and, for closeups, the face and hair. |
| FLUX.2 VAE | 0.3 GB | Encodes the person and garment, and decodes the result. |
| Clothes-mask matting | CPU | Runs on the CPU on purpose, to avoid VRAM contention. |
| Unused nodes | never loaded | The API client strips disconnected nodes from the workflow before queuing, so models a request does not need are not loaded. |
| Pre-warming | during analysis | Models load while the user is still reading the photo analysis. |
| Everything else on the GPU | offloaded | SAM3 plus FLUX needs about 14 GB. A second resident model, such as a local LLM, has to be offloaded first. |
ComfyUI runs with smart memory disabled and one FastAPI worker, so a request is never competing with another for the GPU. Extra requests queue in ComfyUI.
Garments and identity
Model-worn photos
SAM3 isolates the garment cleanly, so they work best.
Product shots on white
SAM3 crops these badly. The segmentation nodes are bypassed, or the background is removed first.
Upper-body pre-crop
For shirts and jackets, the reference is cropped to its top 55% before upload, which stops trouser pixels bleeding into the result.
Two-pass base swap
When a new base layer goes under an existing jacket, the base is replaced first and the jacket is then layered back on.
Shoes
A feet-only mask lets the model regenerate just the shoe area, and the garment image is not cropped.
Short prompts
Long, per-product descriptions made the model lose the inner clothing, so templates stay short.
Identity is protected with a mask, not a second model. SAM3 finds the face and hair, the mask is inverted and applied to the person latent as a noise mask, so those pixels are kept while the clothing changes. It is skipped when the face itself must change (glasses and caps) and for full outfits.
What did not work
PuLID-Flux for identity
Incompatible with the Klein 4B model. Face preservation comes from reference conditioning and a latent mask instead.
Pasting the original face back
A post-process composite had size mismatches and visible seams.
Horizontal lower-body mask
Arms below the waist got masked and sleeve colour bled.
Pre-composing garments
Overlaying garment images before generation did not work.
Higher CFG and more steps
CFG 3.5 with 8 steps gave better jacket detail but background artefacts, so CFG stays at 1.
Text-only lower-body protection
Without the garment pre-crop, the visual reference overpowered the text instruction.
Serving
| Method | Path | What it does |
|---|---|---|
POST | /analyse/ | Photo analysis with the face mesh and pose landmarks |
POST | /session/create | Upload a photo and start a session |
POST | /session/{id}/apply/stream | Apply a catalogue item, streaming progress over SSE |
POST | /session/{id}/back · forward · goto | Move through stored steps with no GPU call |
GET | /catalogue/categories · /catalogue/ | Category tree and items by region |
POST | /swap/direct/stream | One-off swap from an uploaded person and garment |
FastAPI fronts ComfyUI. Requests are authenticated with an API key, and progress is relayed from ComfyUI’s WebSocket to the browser as server-sent events.
The interface
Analysis panel
Shows 25+ checks with a face-mesh and body-zone overlay drawn on the photo.
Catalogue
Man, Woman and Child personas, hierarchical categories, tag filters and colour swatches.
Try-on studio
Live progress over SSE, a before and after slider, an applied-items strip with a running price, undo and back through the outfit history, add to cart and PNG download of the final look.
Plain web stack
A vanilla JavaScript single-page app, no framework and no build step, with a neo-brutalist design.
Limits and next steps
Works best for
Front-facing photos, standard proportions and clear lighting. It is a visual styling tool, not a size-accurate fitting engine.
Next
An add-layer mode and a single-item mode, both needing region-specific masking and inpainting, so the rest of the outfit is left untouched.