Dev.to AI 🤖 Ai 👁 0 📖 3 min read

Checking a face swap with three image metrics instead of your eyes

Every face-swap tool has the same demo problem: the result looks fine until it doesn't, and the difference is hard to put into words. Eyes are a bad judge. A small set of image metrics catches the same failures more cons

Every face-swap tool has the same demo problem: the result looks fine until it doesn't, and the difference is hard to put into words. Eyes are a bad judge. A small set of image metrics catches the same failures more consistently, and none of them needs a model.

Below are three measurements you can run on the output of any swap, plus what each one actually detects.

1. Variance of Laplacian, per region

The variance of the Laplacian is the standard blur score: convolve the greyscale image with a Laplacian kernel and take the variance. High variance means lots of edge energy.

Run it on the whole face and you learn almost nothing, because a swap is sharp where the original was sharp. Run it on the face and on the neck separately, and the ratio is the signal. In a real photo the two are within about 20% of each other. A swap that has replaced the skin texture typically drops the face score by a third while the neck is untouched.

import cv2, numpy as np

def sharpness(img):
    return cv2.Laplacian(img, cv2.CV_64F).var()

face = cv2.imread('crop_face.png', 0)
neck = cv2.imread('crop_neck.png', 0)
print(sharpness(face) / sharpness(neck))

Anything below roughly 0.7 says the swapped region lost detail the rest of the frame still has.

2. SSIM between the swapped crop and the source crop

Structural similarity compares local luminance, contrast and structure. Between the output crop and the source face it tells you how much of the source actually survived the round trip through the model.

A 256-pixel pipeline resized up and back will sit around 0.85 on a good day. Below 0.75 usually means the swap is painting a generic face rather than carrying the identity. The number is not a quality grade; it is a way to tell whether two runs used the same settings.

3. Edge continuity across the mask boundary

This one you write yourself and it is the most useful of the three. Take the mask boundary, sample gradient magnitude on both sides at the same points, and compare.

If the inside of the boundary is much smoother than the outside, the seam is visible as a texture change even when the colour matches. That is the failure mode people describe as 'it looks pasted' without being able to point at a line.

inner = grad[mask_eroded]
outer = grad[mask_dilated & ~mask]
print(inner.mean(), outer.mean())

A ratio near 1 means the texture is continuous. A ratio of 0.5 or less means the swap stopped at the jaw.

What the numbers do not cover

None of these measures whether the face is the right person. Identity is a different problem, and the cheap proxy is a face-embedding distance between the source and the output crop, with the caveat that the model may legitimately change the geometry.

They also do not catch lighting. A perfectly sharp, structurally faithful swap under the wrong light still reads as fake. For that the check is manual and takes five seconds: compare the highlight on the nose with the highlight on the nearest real surface.

A practical order

  1. Run the sharpness ratio. It catches the most common failure first.
  2. Run SSIM against the source. It catches a swap that painted a stranger.
  3. Run the boundary ratio. It catches the seam.
  4. Only then look at the image, and look at it at 100%.

If you want a reference implementation to compare against, I have been testing with FaceSwapAI, which does photo, GIF and video swaps up to ten minutes with up to ten faces in a group shot, in HD without a watermark. The metrics above are tool-agnostic; that is the point of them.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.