Files
uniface/examples/07_face_anonymization.ipynb
2026-04-27 20:51:50 +09:00

202 lines
6.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Face Anonymization with UniFace\n",
"\n",
"<div style=\"display:flex; flex-wrap:wrap; align-items:center;\">\n",
" <a style=\"margin-right:10px; margin-bottom:6px;\" href=\"https://pepy.tech/projects/uniface\"><img alt=\"PyPI Downloads\" src=\"https://static.pepy.tech/personalized-badge/uniface?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads\"></a>\n",
" <a style=\"margin-right:10px; margin-bottom:6px;\" href=\"https://pypi.org/project/uniface/\"><img alt=\"PyPI Version\" src=\"https://img.shields.io/pypi/v/uniface.svg\"></a>\n",
" <a style=\"margin-right:10px; margin-bottom:6px;\" href=\"https://opensource.org/licenses/MIT\"><img alt=\"License\" src=\"https://img.shields.io/badge/License-MIT-blue.svg\"></a>\n",
" <a style=\"margin-bottom:6px;\" href=\"https://github.com/yakhyo/uniface\"><img alt=\"GitHub Stars\" src=\"https://img.shields.io/github/stars/yakhyo/uniface.svg?style=social\"></a>\n",
"</div>\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",
"---\n",
"\n",
"\n",
"This notebook demonstrates face anonymization using various blur methods for privacy protection.\n",
"\n",
"## 1. Install UniFace\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -q \"uniface[cpu]\"\n",
"\n",
"# Clone repo for assets (Colab only)\n",
"import os\n",
"if 'COLAB_GPU' in os.environ or 'COLAB_RELEASE_TAG' in os.environ:\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": "markdown",
"metadata": {},
"source": [
"## 2. Import Libraries\n"
]
},
{
"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": [
"## 3. 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/test.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": [
"## 4. 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": [
"## 5. 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(conf_thresh=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": [
"## 6. Summary\n",
"\n",
"This notebook demonstrated:\n",
"\n",
"- ✅ Five different blur methods (gaussian, pixelate, blackout, elliptical, median)\n",
"- ✅ Automatic face detection and blurring\n",
"\n",
"### Recommended Methods\n",
"\n",
"| Use Case | Method | Parameters |\n",
"|----------|--------|------------|\n",
"| News media / Publishing | `pixelate` | `pixel_blocks=10-15` |\n",
"| Social media | `gaussian` or `elliptical` | `blur_strength=3-5` |\n",
"| Maximum privacy | `blackout` | `color=(0,0,0)` |\n",
"| Natural appearance | `elliptical` | `blur_strength=3, margin=20` |\n",
"\n",
"### Further Resources\n",
"\n",
"- [UniFace Documentation](https://github.com/yakhyo/uniface)\n",
"- [Other Examples](https://github.com/yakhyo/uniface/tree/main/examples)\n"
]
}
],
"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
}