Update README.md
Browse files
README.md
CHANGED
|
@@ -7,96 +7,85 @@ tags:
|
|
| 7 |
- face-verification
|
| 8 |
- biometrics
|
| 9 |
- deep-learning
|
| 10 |
-
pipeline_tag: image-
|
| 11 |
model-index:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
---
|
| 25 |
|
| 26 |
-
# 👥 Facial Recognition & Verification
|
|
|
|
| 27 |
|
| 28 |
-
**
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
pretrained face recognition models. Given two photographs, it extracts fixed
|
| 32 |
-
length embeddings and computes their similarity to decide whether they depict
|
| 33 |
-
the same person. The project is designed for demonstration and research
|
| 34 |
-
purposes and is not intended for biometric authentication in critical
|
| 35 |
-
applications.
|
| 36 |
|
| 37 |
---
|
| 38 |
|
| 39 |
## 🧭 Overview
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
or identity classification. It features automatic dataset splitting,
|
| 44 |
-
transfer learning with ResNet18 or EfficientNet‑B0, mixed precision and
|
| 45 |
-
extensive logging【689067851530192†L16-L27】. While powerful, it focuses on
|
| 46 |
-
classification rather than verification. This project refactors that work
|
| 47 |
-
into a face verification system. Instead of predicting a discrete label,
|
| 48 |
-
we map each face into a 512‑dimensional embedding space and measure how
|
| 49 |
-
close two embeddings are.
|
| 50 |
|
| 51 |
---
|
| 52 |
|
| 53 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
an Inception‑ResNet network pretrained on the VGGFace2 dataset. The model
|
| 57 |
-
provides a 512‑dimensional embedding for each detected face【547754386862401†L54-L63】.
|
| 58 |
-
During verification, cosine similarity between two embeddings is computed. A
|
| 59 |
-
similarity close to one indicates matching faces; a low similarity indicates
|
| 60 |
-
different people.
|
| 61 |
|
| 62 |
---
|
| 63 |
|
| 64 |
-
##
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
benchmark of celebrity face pairs widely used to assess verification
|
| 68 |
-
algorithms. Each pair is labelled as **same** or **different**. FaceNet
|
| 69 |
-
achieves approximately 99 % accuracy on LFW when fine‑tuned【547754386862401†L54-L63】.
|
| 70 |
-
Although the dataset is not included here due to licensing, you can evaluate
|
| 71 |
-
your model by downloading LFW from public sources and adapting the code.
|
| 72 |
|
| 73 |
---
|
| 74 |
|
| 75 |
## ⚙️ Usage
|
| 76 |
|
| 77 |
-
Install dependencies
|
| 78 |
-
|
| 79 |
```bash
|
| 80 |
python3 -m venv venv
|
| 81 |
source venv/bin/activate
|
| 82 |
pip install -r requirements.txt
|
| 83 |
```
|
| 84 |
|
| 85 |
-
Run the
|
| 86 |
-
|
| 87 |
```bash
|
| 88 |
python app.py
|
| 89 |
```
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
returned.
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
in
|
|
|
|
| 100 |
|
| 101 |
```python
|
| 102 |
from PIL import Image
|
|
@@ -104,6 +93,7 @@ from src.verify_faces import verify_images
|
|
| 104 |
|
| 105 |
img1 = Image.open('path/to/photo1.jpg')
|
| 106 |
img2 = Image.open('path/to/photo2.jpg')
|
|
|
|
| 107 |
similarity, is_same = verify_images(img1, img2, threshold=0.8)
|
| 108 |
print(f"Cosine similarity: {similarity:.3f}")
|
| 109 |
print("Same person" if is_same else "Different people")
|
|
@@ -112,39 +102,37 @@ print("Same person" if is_same else "Different people")
|
|
| 112 |
---
|
| 113 |
|
| 114 |
## 📈 Performance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
< 0.5 for non‑matching pairs【547754386862401†L54-L63】. Your mileage may
|
| 119 |
-
vary depending on image quality and lighting conditions. For production
|
| 120 |
-
systems, consider fine‑tuning on domain‑specific data and adjusting the
|
| 121 |
-
threshold.
|
| 122 |
|
| 123 |
---
|
| 124 |
|
| 125 |
## ⚠️ Limitations
|
| 126 |
-
|
| 127 |
-
- **
|
| 128 |
-
|
| 129 |
-
deploy this system for critical decisions (e.g. law enforcement, hiring,
|
| 130 |
-
access control) without careful evaluation.
|
| 131 |
-
- **Privacy:** Handling biometric data requires compliance with data
|
| 132 |
-
protection laws (e.g. GDPR). Always anonymise and secure sensitive
|
| 133 |
-
images and embeddings.
|
| 134 |
-
- **Security:** This demo does not include anti‑spoofing or liveness
|
| 135 |
-
detection. Simple photographs may fool the system.
|
| 136 |
|
| 137 |
---
|
| 138 |
|
| 139 |
## 📜 License
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
details.
|
| 143 |
|
| 144 |
---
|
| 145 |
|
| 146 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
contact [[email protected]](mailto:[email protected]).
|
|
|
|
| 7 |
- face-verification
|
| 8 |
- biometrics
|
| 9 |
- deep-learning
|
| 10 |
+
pipeline_tag: image-feature-extraction
|
| 11 |
model-index:
|
| 12 |
+
- name: Facial Recognition & Verification (Martin Badrous)
|
| 13 |
+
results:
|
| 14 |
+
- task:
|
| 15 |
+
type: image-feature-extraction
|
| 16 |
+
name: Face Verification
|
| 17 |
+
dataset:
|
| 18 |
+
name: LFW
|
| 19 |
+
type: face-images
|
| 20 |
+
metrics:
|
| 21 |
+
- name: Accuracy
|
| 22 |
+
type: accuracy
|
| 23 |
+
value: 0.99
|
| 24 |
---
|
| 25 |
|
| 26 |
+
# 👥 Facial Recognition & Verification
|
| 27 |
+
**Author:** Martin Badrous
|
| 28 |
|
| 29 |
+
This repository exposes a practical **face-verification pipeline** built on top of pretrained face recognition models.
|
| 30 |
+
Given two photographs, it extracts fixed-length embeddings and computes their similarity to decide whether they depict the same person.
|
| 31 |
+
The project is designed for **demonstration and research** purposes and is **not intended for biometric authentication in critical applications**.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
---
|
| 34 |
|
| 35 |
## 🧭 Overview
|
| 36 |
+
The original [Facial Recognition GitHub repository](https://github.com/martinbadrous/Facial-Recognition) provides a modern PyTorch training pipeline for **facial expression or identity classification**.
|
| 37 |
+
It features automatic dataset splitting, transfer learning with ResNet18 or EfficientNet-B0, mixed precision and extensive logging.
|
| 38 |
+
While powerful, it focuses on classification rather than verification.
|
| 39 |
|
| 40 |
+
This Hugging Face version **refactors that work into a face verification system**.
|
| 41 |
+
Instead of predicting a discrete label, we map each face into a **512-dimensional embedding space** and measure how close two embeddings are.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
---
|
| 44 |
|
| 45 |
+
## 🧱 Model Architecture
|
| 46 |
+
We use the **FaceNet** architecture — an *Inception-ResNet network* pretrained on the **VGGFace2** dataset.
|
| 47 |
+
The model provides a **512-dimensional embedding** for each detected face.
|
| 48 |
+
During verification, **cosine similarity** between two embeddings is computed:
|
| 49 |
+
|
| 50 |
+
- A similarity close to **1.0** → same person
|
| 51 |
+
- A similarity close to **0.0** → different people
|
| 52 |
|
| 53 |
+
**Reference model:** [py-feat/facenet](https://huggingface.co/py-feat/facenet)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
---
|
| 56 |
|
| 57 |
+
## 🧩 Dataset
|
| 58 |
+
Evaluation is based on the **Labeled Faces in the Wild (LFW)** dataset — a benchmark of celebrity face pairs widely used for assessing verification algorithms.
|
| 59 |
+
Each pair is labelled as *same* or *different*.
|
| 60 |
+
FaceNet achieves **≈ 99 % accuracy** on LFW when fine-tuned on VGGFace2.
|
| 61 |
|
| 62 |
+
Although LFW is not included here (due to licensing), you can evaluate the model by downloading it from public sources and reusing the provided code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
---
|
| 65 |
|
| 66 |
## ⚙️ Usage
|
| 67 |
|
| 68 |
+
### 1️⃣ Install dependencies
|
|
|
|
| 69 |
```bash
|
| 70 |
python3 -m venv venv
|
| 71 |
source venv/bin/activate
|
| 72 |
pip install -r requirements.txt
|
| 73 |
```
|
| 74 |
|
| 75 |
+
### 2️⃣ Run the demo locally
|
|
|
|
| 76 |
```bash
|
| 77 |
python app.py
|
| 78 |
```
|
| 79 |
|
| 80 |
+
The Gradio interface will open in your browser.
|
| 81 |
+
Upload two images — the app will detect faces, extract embeddings, and show whether they belong to the same person, along with a similarity score.
|
| 82 |
+
If no face is detected, an appropriate message will be displayed.
|
|
|
|
| 83 |
|
| 84 |
+
---
|
| 85 |
|
| 86 |
+
## 🧠 Verification API
|
| 87 |
+
The core logic resides in the `src` package.
|
| 88 |
+
You can import and use these utilities programmatically:
|
| 89 |
|
| 90 |
```python
|
| 91 |
from PIL import Image
|
|
|
|
| 93 |
|
| 94 |
img1 = Image.open('path/to/photo1.jpg')
|
| 95 |
img2 = Image.open('path/to/photo2.jpg')
|
| 96 |
+
|
| 97 |
similarity, is_same = verify_images(img1, img2, threshold=0.8)
|
| 98 |
print(f"Cosine similarity: {similarity:.3f}")
|
| 99 |
print("Same person" if is_same else "Different people")
|
|
|
|
| 102 |
---
|
| 103 |
|
| 104 |
## 📈 Performance
|
| 105 |
+
Pretrained **FaceNet** models typically achieve:
|
| 106 |
+
|
| 107 |
+
| Metric | Typical Value |
|
| 108 |
+
|---------|----------------|
|
| 109 |
+
| Accuracy (LFW) | ≈ 99 % |
|
| 110 |
+
| Cosine Similarity (same) | > 0.8 |
|
| 111 |
+
| Cosine Similarity (different) | < 0.5 |
|
| 112 |
|
| 113 |
+
Performance may vary depending on image quality, resolution, and lighting.
|
| 114 |
+
For production systems, fine-tune on domain-specific data and calibrate your similarity threshold.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
---
|
| 117 |
|
| 118 |
## ⚠️ Limitations
|
| 119 |
+
- **Bias & Fairness:** Pretrained facial models may exhibit demographic bias — they can perform better on certain ethnicities or genders. Evaluate thoroughly before deployment.
|
| 120 |
+
- **Privacy:** Handle biometric data in compliance with privacy laws (GDPR, HIPAA, etc.). Never store embeddings without consent.
|
| 121 |
+
- **Security:** This demo lacks spoofing or liveness detection — printed photos or digital screens can fool it.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
---
|
| 124 |
|
| 125 |
## 📜 License
|
| 126 |
+
This project is licensed under the **MIT License**.
|
| 127 |
+
See the [LICENSE](./LICENSE) file for details.
|
|
|
|
| 128 |
|
| 129 |
---
|
| 130 |
|
| 131 |
+
## 📚 Citation & Contact
|
| 132 |
+
If you use this project in academic work, please cite the original FaceNet paper.
|
| 133 |
+
|
| 134 |
+
> Schroff et al., *FaceNet: A Unified Embedding for Face Recognition and Clustering*, CVPR 2015.
|
| 135 |
+
> DOI: [10.1109/CVPR.2015.7298682](https://doi.org/10.1109/CVPR.2015.7298682)
|
| 136 |
|
| 137 |
+
📩 **Contact:** [email protected]
|
| 138 |
+
🧠 **Project Page:** [Hugging Face – Facial-Recognition-Verification](https://huggingface.co/martinbadrous/Facial-Recognition-Verification)
|
|
|