Face Swap

Swap the face from one photo onto another, with dlib's 68-point facial landmarks and OpenCV. Built by Noorullah Zamindar β€” original Flask app at NoorullahZamindar-007/Face-Swaping-App.

This is a classical landmark-based pipeline, not a neural network trained by the author. The "model" is dlib's pre-trained 68-point shape predictor (shape_predictor_68_face_landmarks.dat, shipped in this repo, ~100 MB), plus the swap code in face_swap.py, which is the pipeline from the original face_swap_utils.py, kept unchanged in behaviour and packaged so it can run without the Flask web server.

How it works

  1. Detect faces in both photos with dlib's HOG-based face detector.
  2. Find the 68 facial landmarks of the largest face in each photo.
  3. Delaunay-triangulate the first face's landmarks.
  4. Affine-warp each triangle of the first face onto the matching triangle of the second face.
  5. Seamless-clone the warped face onto the second photo, so the edges match the second photo's lighting.

The first image's face ends up on the second image.

How to use

pip install -r requirements.txt
python face_swap.py samples/face_a_obama.jpg samples/face_b_bush.jpg -o swapped.jpg
from face_swap import face_swap

# Puts the face of image1 onto image2 and saves the result.
face_swap("face_a.jpg", "face_b.jpg", "swapped.jpg")

The landmark model file must sit next to face_swap.py (it ships in this repo) or in the working directory. dlib has no wheel for some platforms and may compile from source on install β€” allow several minutes for pip install dlib the first time.

Measured results

Measured 19 Nov 2026 with evaluate.py (included), OpenCV 5.1.0, dlib 20.0.1, on the two sample portraits in samples/ β€” the official White House portraits of Barack Obama and George W. Bush (public domain, via Wikimedia Commons).

Measure Value
Faces detected, Obama portrait 1
Faces detected, Bush portrait 1
Landmarks per face 68 / 68
Delaunay triangles (Obama face) 112
Delaunay triangles (Bush face) 113
Swap time (model already loaded) 2.51 s
Output size 3000 Γ— 2267 (the Bush photo's size)

Did the face actually move? β€” measured against the Bush photo

Measure Value
Mean absolute difference inside the face region 27.78 (per channel, 0–255)
Face-region pixels changed by more than 10 79.25%
Mean absolute difference outside the face region 0.46
Background pixels changed by more than 10 0.06%

The face region changed a lot and the background essentially did not β€” the small background difference is JPEG noise (the output is written as .jpg) plus the clone edge. See samples/comparison.jpg: Obama's face on Bush's photo, with Bush's hair, suit and background untouched.

Controls

Control Result
Blank image (solid gray, no face) as the first image ValueError: No face detected in the first image.
Blank image as the second image ValueError: No face detected in the second image.
Missing input file ValueError: Could not read image: ...

Repo test suite

The original project's own test suite (25 tests: triangulation, warping, blending, upload validation, downscaling, error cases) passes against this code: 25 passed.

Limitations β€” read before using

  • Only the largest face in each photo is swapped. Group photos swap the most prominent face, not every face.
  • Works best on frontal, well-lit faces. The dlib HOG detector can miss faces that are turned away, partly occluded, or very small, and the warp looks wrong when the two faces are at very different angles or expressions.
  • The result is a visible composite, not a photorealistic deepfake. Skin tone, lighting and pose differences show at the blend edge β€” this is a demo/teaching pipeline, not a production face-swap model.
  • Use it responsibly. Only swap faces you have the right to use β€” your own photos, or photos shared for this purpose. Do not create misleading images of real people. The samples here are public-domain official portraits of public figures, used only to demonstrate the pipeline.
  • Photos larger than 4000 px on the long edge are downscaled before swapping, so the result is at most 4000 px on a side.
  • No training data, no training metrics. Nothing here was trained by the author; the landmark model is dlib's, pre-trained on the iBUG 300-W dataset. There is no accuracy number to report for it beyond the detection and swap measurements above.

Files

  • face_swap.py β€” the swap pipeline (library + CLI), from the original face_swap_utils.py
  • shape_predictor_68_face_landmarks.dat β€” dlib's pre-trained 68-point shape predictor (the "model"; ~100 MB)
  • evaluate.py β€” reproduces every number above
  • metrics.json β€” the measured results
  • requirements.txt β€” opencv-python-headless, numpy, dlib
  • samples/face_a_obama.jpg β€” Obama official portrait (face source)
  • samples/face_b_bush.jpg β€” Bush official portrait (target photo)
  • samples/swapped.jpg β€” the measured swap output
  • samples/comparison.jpg β€” source, target and swapped side by side
  • samples/face_a_landmarks.jpg, samples/face_b_landmarks.jpg β€” the detected landmarks and face hulls drawn on each portrait

Source

Original project: NoorullahZamindar-007/Face-Swaping-App (Flask web app with upload form). The full Flask app lives there; this repo ships the swap logic as a reusable module so it can be run without the web server.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support