Person matte for video
A neural matte on its own boils, loses limbs in shadow and eats thin parts. This skill wraps it in a pipeline that fixes those failures one by one, plus a review loop with the person who owns the footage.
Run every script as python <skill-dir>/scripts/NAME.py ... (<skill-dir> is this skill's folder; in Claude Code
${CLAUDE_SKILL_DIR}) from the folder that holds project.json (paths inside the config are relative to it; paths
on the command line are relative to the current folder). Scripts print their usage when run without arguments
(-h for the full help). One JSON project file drives everything: read config first.
Before you start
- Consent and privacy. The footage shows a real person. Everything here runs locally. Send their frames to an external model or service (an image generator, a cloud matting API) only after the owner names the service and agrees. Never upload, publish or share their footage or stills yourself. Never alter or "improve" their face or body shape; colour grading the whole person is fine when asked.
- Install:
pip install -r <skill-dir>/requirements.txt(Python 3.12+, ideally in a virtual environment). - Self-check:
python <skill-dir>/tests/test_pipeline.py(synthetic clip, no real footage;--modelalso runs BiRefNet). - Clean plate (background without the person, same framing) - needed for the shadow veto, hole fill and the cast shadow. Options: a frame before the person enters or after they leave; a per-pixel temporal median over frames where the person moved away; a separate empty shot. Filling the person out with a generative model counts as sending their frames out: only with the owner's consent.
- Work on copies. Keep every rendered version in its own folder (
work/out_v1,work/out_v2, ...) and never delete earlier versions: the comparison tools need them, and an earlier version may turn out to be the preferred one. - Ask which output files the owner needs and produce only those.
Pipeline
- Frames.
extract_frames.py video.mp4 frames/- lossless PNGs; it prints the source fps for the config. If the shot must be levelled, cropped or reframed, do it once at this point, so camera frames, mattes and output share one geometry. - Scenes. Split the timeline into scenes in the config (
[start, end), one background each,"keep"for the untouched camera). Mattes are never interpolated or averaged across a scene boundary. - Keys.
matte_keys.py project.json- BiRefNet everykey_strideframes (3 is a good start), every frame insidematte.per_frame, and zoomed re-runs of small parts inmatte.zoom_boxes. - In-betweens.
matte_interp.py project.json- optical-flow warp of the neighbouring keys. Frames where the two keys disagree (fast or thin motion -> doubled, ghosted limbs) go towork/interp_suggest.json: add those ranges tomatte.per_frameand runmatte_keys.py project.json a bandmatte_interp.py project.json a bagain for them. If most of a scene is flagged, lowerkey_strideinstead. After changing anymattesetting, move the old files of that range out ofwork/keysfirst: matte_interp uses every key file it finds. - Refine (optional).
matte_refine.py project.json- gate to an existing approved cut-out (refine.reference), remove the semi-transparent cast shadow against the clean plate (refine.shadow_veto; a shadow matted solidly needs a rotosubshape), fill small holes. Skin in shade is darker than the plate too: see troubleshooting before enabling the shadow veto. - Stabilise.
matte_stabilize.py project.json- long temporal average where nothing moves, none where it does. - Roto (only where needed). Shapes in
roto.jsonfor parts no matte gets right (schema inroto.pyand config); read vertices offsheets.py grid, preview withroto.py project.json FRAME out.png. - Composite.
composite.py project.json- edge decontamination, optional camera detail over a graded version (composite.detail_from), cast shadow, shade regions, fringe regions. Roto and fringe regions act here, sowork/matteshows the alpha before them; preview roto withroto.py. - Encode.
encode.py "work/out/%05d.png" NAME --project project.json --profiles social->NAME_social.mp4(addmasteronly if wanted;--audio original.mp4puts the original sound on the social file, padded or cut to the video length).
After changing any pass, re-run every later pass for the affected frames (keys -> interp -> refine -> stabilise ->
composite). Render into work/out, then copy the frames to work/out_vN before the next change.
Review loop with the owner
Owners often review on a phone and send screenshots of what looks wrong. Work like this:
- Locate, don't guess.
locate_screenshot.py shot.png "work/out_vN/%05d.png" FIRST LASTgives frame and box (score > 0.9); refine with--step 1around the hit. Then compare camera | previous | new crops withsheets.py compare. - Diagnose before fixing. Look at the matte value at the bad pixels (
work/matte), the camera pixels and the background layer separately: is the wrong colour coming from the person layer (matte or a contaminated edge) or from the background? Many "the background is wrong" reports are a contaminated edge of the person layer. troubleshooting maps symptoms to causes. - Measure. Put a box per flagged place into
regions.jsonand runjitter.pyon the old and new version; report the numbers per place. A real movement keeps a high count; a fixed boil drops toward 0. - Sharpness. Whenever the person's pixels come from another version, compare a head crop with the camera
(
sheets.py sharp); a resampled or compressed grade can lose a noticeable share of the detail. - Independent review before sending. Have 2-5 reviewers (subagents or a workflow) look at the QA sheets with different lenses and try to refute the fix; verify every non-minor finding before acting. See review.
- After two misses on the same spot, stop guessing. Render 2-3 options on one frame next to the original
(
sheets.py compare) and let the owner choose. The original footage is the target for shape and light; do not "improve" it (for example by relighting a part that is truly in shadow). - Report per item: what changed, the numbers, what is not verified. Send each video file separately.
Files
- config - every config key, defaults, roto schema; read before writing project.json
- troubleshooting - symptom -> cause -> fix; read when a review comes back
- review - QA sheets, review lenses, prompts for review agents; read before sending a version
scripts/- the pipeline;tests/- synthetic clip generator and end-to-end test