How to check whether a face swap matches the grain, with 20 lines of numpy

2026-11-29来源 dev.toEnglish

A face swap can be geometrically perfect and still look pasted, because the new face does not share the noise of the frame around it. This post is a small, repeatable measurement for that: compare the local noise in a patch of swapped skin with the local noise in the background, and see how far apart they are.

Why grain matters more than sharpness

Every photo has noise. Film has grain from the emulsion and the scanner; a phone at ISO 3200 has luminance grain plus chroma speckle in the shadows. A generator upsamples and averages, so the face it returns is usually cleaner than its surroundings. The eye reads that as a sticker long before it can say why.

Sharpening does not fix it. Sharpening amplifies whatever noise is already there, so the background gets crunchier and the clean face stays clean, which widens the gap.

The measurement

Take a patch of skin (cheek or forehead, no eyes, no hair) and a patch of background at a similar brightness. Compute the standard deviation of the high-frequency component in each. If the face is more than about 40% smoother than the background, the seam will show at 100%.

import numpy as np from PIL import Image def high_freq_sigma(patch: np.ndarray) -> float: # patch: HxWx3 float array in 0..255 gray = patch.mean(axis=2) # 3x3 box blur, then subtract to keep only high frequencies k = np.ones((3, 3)) / 9.0 h, w = gray.shape padded = np.pad(gray, 1, mode='reflect') blurred = np.zeros_like(gray) for y in range(3): for x in range(3): blurred += k[y, x] * padded[y:y+h, x:x+w] return float((gray - blurred).std()) def compare(img_path: str, face_box, bg_box) -> dict: img = np.asarray(Image.open(img_path).convert('RGB'), dtype=np.float64) f = img[face_box[1]:face_box[3], face_box[0]:face_box[2]] b = img[bg_box[1]:bg_box[3], bg_box[0]:bg_box[2]] sf, sb = high_freq_sigma(f), high_freq_sigma(b) return {'face': round(sf, 2), 'bg': round(sb, 2), 'ratio': round(sf / sb, 2) if sb else None} print(compare('swap.png', (300, 180, 420, 300), (40, 40, 160, 160))) 

A ratio near 1.0 means the face belongs to the frame. A ratio below 0.6 means the face was rendered from a cleaner source, and you will see the boundary as a change in texture rather than a line.

What to do with the number

Two options, and they are not equivalent.

Match the noise. Add grain over the whole composite at the end, not only the face, with strength set by the darkest part of the image. Dark backgrounds need more; bright, evenly lit portraits need almost none. Do it after any colour work, or the grain gets smoothed away again.

Fix the source. If you control the source photo, shoot it at a similar ISO and with similar light. A clean source in a noisy frame is a permanent mismatch; a noisy source in a clean frame is much easier to hide.

The part that is easy to miss

Chroma noise separates from luminance noise. Many swaps keep luminance grain and have clean colour, so the face looks slightly desaturated and plastic next to a noisy neck. If you are checking by eye, zoom to 200% on the cheek and on the wall and compare the colour speckle, not the brightness.

For video the same test applies frame by frame: sample three frames and look for a face whose grain is frozen while the background grain moves. That is the most obvious version of the same mistake.

If you want a target to test against, I use faceswapai.com for photo, video and GIF swaps (up to ten faces in a group shot, ten minutes of video, HD and no watermark), mostly because the export keeps the original resolution, which is what makes a grain comparison meaningful at all.

原文:https://www.a8q9.cn/en/article/how-to-check-whether-a-face-swap-matches-the-grain-with-20-lines-of-numpy-3ccc2c.html

相关文章