Faded school portraits, blurry wedding snaps and low-resolution scans of family albums are exactly what AI face restoration was built for. CodeFormer, from Nanyang Technological University's S-Lab, rebuilds damaged or blurry faces, and it can tidy up the rest of the photo too. It's free to download and runs on your own graphics card, so your family photos never leave your PC. This guide follows the official CodeFormer GitHub README.
CodeFormer was presented at NeurIPS 2022 as a "blind face restoration" model. In plain English, it doesn't need to know how a photo was damaged. It recognises faces and predicts a cleaner, sharper version of each one. The README covers four jobs:
Whole-image enhancement – finds every face in a photo, restores it and pastes it back
Face restoration on cropped, aligned faces – for 512x512 face crops
Face colourisation – adds colour to black-and-white or faded face crops
Face inpainting – fills in parts of a face you've painted over in white
Be realistic about what that means. CodeFormer is a face model. On its own it doesn't remove scratches, creases or stains from the rest of the picture, although it can hand the background to Real-ESRGAN for upscaling. For badly scratched prints, Microsoft's Bringing Old Photos Back to Life project is designed to detect and repair scratches. And because the model predicts detail it can't actually see, a restored face is a best guess, not a recovered original. Keep your original scan.
Check the licence first
CodeFormer uses the NTU S-Lab License 1.0, which only allows redistribution and use for non-commercial purposes. Restoring your own family photos is fine. If you want to sell a restoration service or use it in paid client work, you'll need permission from the authors, or a different model. GFPGAN, for example, uses the Apache 2.0 licence.
The README also warns that only the demos on Hugging Face, Replicate and OpenXLab are maintained by the authors. It names CodeFormer.net, CodeFormer.cn and CodeFormerAI.com as non-official websites that use the models without the authors' permission. Running it locally avoids that problem completely.
What you need
The README lists PyTorch 1.7.1 or newer, CUDA 10.1 or newer and the packages in its requirements file. CUDA means an Nvidia graphics card. The developers don't publish a minimum VRAM figure. Face restoration models are much smaller than the image generators and large language models we usually cover, so as our suggestion rather than an official requirement, an 8GB card such as the ASUS RTX 5060 8GB Dual OC is a sensible starting point. If you also want to run image generators, a 12GB card like the ASUS RTX 3060 12GB Dual Gaming OC V2 gives you more room. Our guide to how much VRAM you need explains the trade-offs.
The README's commands use Anaconda (conda) and git. They work in a Linux terminal, and the README's video section mentions Windows and Mac users, but if you run into build problems on Windows, WSL2 with Ubuntu is the easiest fallback. Our guide to setting up WSL2 for GPU-accelerated local AI covers that.
Step 1: Scan your photos properly
The better the input, the less the AI has to guess. Scan prints on a flatbed scanner rather than photographing them with a phone, save them as PNG or high-quality JPEG files, and keep the untouched scans in a separate folder. Crop out the album page or scanner lid, but don't sharpen or denoise the scans first. Let the model work from the original detail.
Open a terminal and run the README's commands:
git clone https://github.com/sczhou/CodeFormer
cd CodeFormer
conda create -n codeformer python=3.8 -y
conda activate codeformer
pip3 install -r requirements.txt
python basicsr/setup.py develop
The README also lists an optional conda install -c conda-forge dlib step. You only need it for the dlib face detector or for cropping faces with dlib. If your PyTorch install doesn't detect your graphics card, install a CUDA build of PyTorch for your driver, then try again.
Step 3: Download the models
CodeFormer needs its face detection models and its own restoration weights. The README provides a script for both:
python scripts/download_pretrained_models.py facelib
python scripts/download_pretrained_models.py CodeFormer
Add python scripts/download_pretrained_models.py dlib if you want the dlib detector. If the script fails, the README links to the same files on the project's GitHub Releases page. They go in the weights/facelib and weights/CodeFormer folders.
Step 4: Restore a whole photo
For a normal scanned photo with one or more faces in it, use the whole-image command and point it at a file or folder:
python inference_codeformer.py -w 0.7 --input_path inputs/my_scans
Results are saved in the results folder. Two optional flags are worth trying:
Step 5: Choose the right fidelity weight
The -w setting is CodeFormer's most important control. It runs from 0 to 1. The README says lower values tend to give higher-quality results, while higher values give results that are more faithful to the input. In practice that's a trade-off between a cleaner face and one that still looks like the person in the photo.
We'd start at the README's 0.7 for family photos, where likeness matters most. Then run the same photo at a lower value such as 0.5 and compare the two. If a relative's face starts to look like a stranger's, move the value back up towards 1.
Optional: cropped faces, colour and inpainting
For close-up portraits, you can work on cropped, aligned faces instead. The README's scripts/crop_align_face.py script creates 512x512 crops (it uses dlib). Then run:
python inference_codeformer.py -w 0.5 --has_aligned --input_path inputs/cropped_faces
The same crops work with inference_colorization.py, which colourises black-and-white or faded faces, and inference_inpainting.py, which fills in areas you've masked with a white brush in an image editor. Both work on face crops only, not whole photos.
Restoring old video
CodeFormer also accepts .mp4, .mov and .avi clips. Install ffmpeg (conda install -c conda-forge ffmpeg), then use the README's video command:
python inference_codeformer.py --bg_upsampler realesrgan --face_upsample -w 1.0 --input_path old_clip.mp4
For general video upscaling rather than faces, see our guide to upscaling videos locally with Video2X.
Troubleshooting
Hair looks odd around the face: the README notes that whole-image mode blends the face back into the background, which can affect hair texture at the edges. Try the cropped-face workflow for portraits.
The face doesn't look like the person: raise -w towards 1.
Out of memory errors: process one image at a time, or see our guide to fixing CUDA out of memory errors.
You'd rather not use a terminal: CodeFormer's README lists third-party integrations, including the node-based chaiNNer app and AUTOMATIC1111's Stable Diffusion WebUI. These aren't maintained by the CodeFormer authors, so check each project's own instructions.