UniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.
Python computer-visiondeep-learningface-alignmentface-analysisface-anonymizationface-anti-spoofingface-attributesface-detectionface-meshface-parsingface-quality-assessmentface-recognitionface-segmentationface-trackingfacial-landmarksgaze-estimationhead-pose-estimationonnxruntimeportrait-mattingpython
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Face Anonymization with UniFace\n",
"\n",
"[](https://colab.research.google.com/github/yakhyo/uniface/blob/main/examples/07_face_anonymization.ipynb) [](https://www.kaggle.com/code/yakhyokhuja/face-anonymization-with-uniface)\n",
"\n",
"Hide faces in an image using five blur methods.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/yakhyo/uniface/main/assets/demo/anonymization.jpg\" width=\"100%\">\n",
"\n",
"---\n",
"\n",
"**UniFace** is a lightweight, production-ready Python library for face detection, recognition, tracking, landmark analysis, face parsing, gaze estimation, and face attributes.\n",
"\n",
"GitHub: [github.com/yakhyo/uniface](https://github.com/yakhyo/uniface) | Docs: [yakhyo.github.io/uniface](https://yakhyo.github.io/uniface)\n",
"\n",
"## Setup\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -q \"uniface[cpu]\"\n",
"\n",
"# Clone repo for assets (Colab / Kaggle only)\n",
"import os\n",
"if any(k in os.environ for k in ('COLAB_GPU', 'COLAB_RELEASE_TAG', 'KAGGLE_KERNEL_RUN_TYPE')):\n",
" if not os.path.exists('uniface'):\n",
" !git clone --depth 1 https://github.com/yakhyo/uniface.git\n",
" os.chdir('uniface/examples')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import IPython.display as display\n",
"from PIL import Image\n",
"import numpy as np\n",
"\n",
"import uniface\n",
"from uniface.detection import RetinaFace\n",
"from uniface.privacy import BlurFace\n",
"\n",
"print(f'UniFace version: {uniface.__version__}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Load Test Image\n",
"\n",
"We'll use a test image to demonstrate face anonymization.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load image\n",
"image_path = '../assets/source/anon_group.jpg'\n",
"image = cv2.imread(image_path)\n",
"\n",
"# Display original image\n",
"pil_image = Image.open(image_path)\n",
"print(f'Image size: {image.shape[:2]}')\n",
"pil_image\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Quick Start: Anonymization\n",
"\n",
"Detect faces and blur them using `BlurFace`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Detect faces and anonymize\n",
"detector = RetinaFace()\n",
"blurrer = BlurFace(method=\"pixelate\")\n",
"\n",
"faces = detector.detect(image.copy())\n",
"anonymized = blurrer.anonymize(image.copy(), faces)\n",
"\n",
"# Display result\n",
"output = cv2.cvtColor(anonymized, cv2.COLOR_BGR2RGB)\n",
"display.display(Image.fromarray(output))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Compare All Blur Methods\n",
"\n",
"UniFace provides 5 different blur methods. Let's compare them:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Initialize detector\n",
"detector = RetinaFace(confidence_threshold=0.5)\n",
"faces = detector.detect(image)\n",
"print(f'Detected {len(faces)} faces')\n",
"\n",
"# Test all blur methods\n",
"methods = ['gaussian', 'pixelate', 'blackout', 'elliptical', 'median']\n",
"\n",
"for method in methods:\n",
" blurrer = BlurFace(method=method)\n",
" anonymized = blurrer.anonymize(image.copy(), faces)\n",
"\n",
" output = cv2.cvtColor(anonymized, cv2.COLOR_BGR2RGB)\n",
" print(f'\\n{method.upper()}:')\n",
" display.display(Image.fromarray(output))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notes\n",
"\n",
"- The blur is written into the pixels and cannot be undone. Pass `image.copy()` if you need the\n",
" original afterwards.\n",
"- Anything the detector misses stays visible, so recall matters more here than in most tasks."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}