Compositing a face swap back into a photo: the five steps that decide quality
A face swap model produces a face. Getting that face back into the photo without a visible seam is a separate job, done by a compositing pipeline, and it decides more of the final quality than the model does. Here are t
A face swap model produces a face. Getting that face back into the photo without a visible seam is a separate job, done by a compositing pipeline, and it decides more of the final quality than the model does.
Here are the five steps that matter, in the order they run, and what each one can ruin.
1. Mask, then feather
The swap comes back as an image plus a mask. If the mask is a hard ellipse around the face, the seam is a hard ellipse around the face. The fix is a feather of a few pixels, but the width has to scale with the head: 2 px on a 200 px face and 2 px on a 1000 px face are not the same edge.
A useful rule: feather width in pixels should be about 1 percent of the face's inter-ocular distance. Below that the edge shows as a line; above it the face starts to look airbrushed into the background.
2. Colour transfer, matched on the neck
Most pipelines match colour in a low-resolution space and then paste at full resolution, which averages the lit and shadowed sides of the face together. The result is a face that is slightly too flat and slightly too bright in the shadows.
Match against the neck instead of the cheeks. The neck is lit by the same source, it is not part of the swap, and it does not move between the source and the target. If the face and the neck agree, the face sits in the room even when the cheek highlight is a little off.
3. Grain, not blur
The fastest way to hide a seam is to blur it, and that is also the fastest way to make a face look plastic. High-frequency noise is what carries skin texture, and it has to be continuous across the seam.
If the source and the target were shot at different ISO, the noise amplitude differs and the seam appears as a change in grain rather than a change in colour. Measure the standard deviation in a flat patch of cheek on both sides, then add or subtract noise on the swapped region to match. Two or three percent is usually enough.
4. One downscale, at the end
Every resize is a resample, and every resample softens. A pipeline that swaps at 512, pastes at 1024, then exports at 2048 has softened the face twice and sharpened nothing.
Do the swap at the highest resolution you can afford, paste once, and downscale once. If you must work small, keep the target size in mind: a 512 px swap exported at 2048 is a 512 px face with bigger pixels, not more detail.
5. Check at 100 percent, not in the feed
The last step is the one people skip. A swap that survives a 400 px thumbnail can still fall apart at full size, because downscaling averages away exactly the high-frequency evidence a seam leaves behind.
Open the output at 100 percent and look at three zones: the jaw and neck, the eye sockets, and the hairline. Those are where the mask edge, the micro-contrast loss and the texture discontinuity show up first.
The takeaway
If you are building a swap feature, the model is the part you can least control and the compositing is the part you can. Feather width, colour reference, grain matching, and a single export resize are four parameters that move the perceived quality more than a model upgrade, and they are all cheap to test.
I work on faceswapai.com, which does photo, video and GIF swaps with up to ten faces in a group photo and ten minutes of video, HD and no watermark. The pipeline above is roughly what it runs.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.