This repository provides a head-anonymization workflow that uses:
- head-segmentation for head masks
- SG161222/Realistic_Vision_V5.0_noVAE as the base model
- ControlNet for inpainting
- DeepFace for generating consistent demographic attributes (age, gender, race)
This pipeline aims to anonymize faces while preserving certain identity-related features (like approximate age, race, and gender). By analyzing faces with DeepFace, we build a Stable Diffusion prompt that matches the subject’s demographic attributes, ensuring a realistic yet anonymized result by staying above 0.6 cosine distance from the original image.
Prompt:
A photorealistic portrait of a young adult White man, with a neutral expression, looking directly at the camera. Ultra-detailed, 8k resolution, professional photography.
Sample Image |
Mask |
-
Segment the Head
We use head-segmentation to generate a mask of the head region in the input image. -
Analyze Demographics
DeepFace analyzes the original face to predict:- Age
- Gender
- Race (dominant race)
-
Generate Prompt
A custom prompt is built using the analyzed demographic data. For example:"A photorealistic portrait of a {age_group} {race_descriptor} {gender}, with a neutral expression, looking directly at the camera. Ultra-detailed, 8k resolution, professional photography" -
Inpaint with ControlNet
ControlNet uses the generated mask and the prompt to inpaint the head region in the style of Realistic Vision V5. -
Save the Results
The anonymized image is saved, preserving the original image background but anonymizing the head region.
- Clone the Repository
git clone https://github.com/anilegin/AnonHead.git cd AnonHead - Create a virtual environment (Optional)
python -m venv anonymized_env source ./anonymized_env/bin/activate - Install Dependencies
Make sure you’re in a Python virtual environment, then run:pip install -r requirements.txt
Note: You may need to install system-level dependencies for PyTorch, e.g., CUDA, if you plan to run on GPU.
- Install Pre-trained weights for Realistic Vision v5.0
Run:python download-weights
Run the segment script to generate a head mask from your input image:
python segment.py --im_path path/to/your/image.png- Output: The segmented mask is saved to the
./masksfolder by default.
Use the predict script to inpaint the face with a new identity:
python predict.py --image path/to/your/image.png --mask path/to/your/mask.png- Output: The anonymized image is saved under the
./resfolder.
You can pass custom prompts if you want to override the defaults:
python predict.py \
--image path/to/your/image.png \
--mask path/to/your/mask.png \
--prompt "A photorealistic portrait of a middle-aged Hispanic woman, neutral expression, looking at the camera" \
--negative_prompt "ugly, blurry, low-res"If not provided, the script uses DeepFace to detect age, race, gender, and builds an automated prompt accordingly.
- Masks: Saved in
./masks/<image_name>_mask.png - Anonymized Images: Saved in
./res/<image_name>_out.png
Example structure:
project/
├── masks
│ └── input_image_mask.png
├── res
│ └── input_image_out.png
- Realistic Vision V5 model from:
SG161222/Realistic_Vision_V5.0_noVAE - Head Segmentation from:
wiktorlazarski/head-segmentation - DeepFace for demographic analysis:
DeepFace GitHub
- Data & Model Usage: Comply with each model’s license (for Realistic Vision, DeepFace, etc.).
- Code: MIT License Copyright (c) 2025 ... Permission is hereby granted, free of charge, to any person obtaining a copy ...
Feel free to open an issue or pull request for improvements and suggestions!

