File size: 4,642 Bytes
346df8e 4f8e650 346df8e 4f8e650 346df8e 4f8e650 346df8e 20c47cf 346df8e 4f8e650 20c47cf 346df8e 20c47cf 346df8e 4f8e650 346df8e 20c47cf 346df8e 20c47cf 346df8e 20c47cf 346df8e 20c47cf 346df8e 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf 4f8e650 20c47cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
---
license: mit
language:
- en
metrics:
- accuracy
- f1
- precision
- recall
pipeline_tag: image-classification
tags:
- face_recognition
- svm
- facenet
- computer_vision
- streamlit
- cpu_friendly
datasets:
- AI-Solutions-KK/face_recognition_demo_dataset
---
# ๐ง Face Recognition Model (CNN Embeddings + SVM)
**Domain-specific face recognition model** using:
- **FaceNet (InceptionResnetV1)** to extract 512-D face embeddings
- **SVM classifier** for identity recognition
- **Centroid baseline** for cosine-similarity checks / open-set support
Designed to run efficiently on **CPU**, ideal for lightweight deployment and Streamlit apps.
---
## ๐ฆ Artifacts in This Repository
| File | Description |
|-------------------|---------------------------------------------------------|
| `svc_model.pkl` | Trained SVM classifier on FaceNet embeddings (105 classes) |
| `centroids.npy` | Class centroids (mean embeddings per identity) |
| `classes.npy` | List of identity labels (class order used by the SVM) |
| `README.md` | Model documentation |
---
## ๐ Load Model from Hugging Face
```python
from huggingface_hub import hf_hub_download
import joblib
import numpy as np
REPO_ID = "AI-Solutions-KK/face_recognition"
svc_path = hf_hub_download(REPO_ID, "svc_model.pkl")
centroids_path = hf_hub_download(REPO_ID, "centroids.npy")
classes_path = hf_hub_download(REPO_ID, "classes.npy")
svc_model = joblib.load(svc_path)
centroids = np.load(centroids_path)
class_names = np.load(classes_path, allow_pickle=True)
print("Model loaded successfully. Classes:", len(class_names))
```
---
## ๐ฎ Simple Inference Example (Using FaceNet Embeddings)
```python
from huggingface_hub import hf_hub_download
import joblib, numpy as np, cv2, torch
from facenet_pytorch import InceptionResnetV1, MTCNN
REPO_ID = "AI-Solutions-KK/face_recognition"
# Load classifier + metadata
svc_path = hf_hub_download(REPO_ID, "svc_model.pkl")
classes_path = hf_hub_download(REPO_ID, "classes.npy")
obj = joblib.load(svc_path)
svc_model = obj["clf"]
normalizer = obj["norm"]
label_encoder = obj["le"]
class_names = np.load(classes_path, allow_pickle=True)
# Load FaceNet backbone + face detector
device = "cpu"
mtcnn = MTCNN(keep_all=False, device=device)
facenet = InceptionResnetV1(pretrained="vggface2").eval().to(device)
def get_embedding(img_path: str) -> np.ndarray:
img_bgr = cv2.imread(img_path)
if img_bgr is None:
raise ValueError(f"Could not read image: {img_path}")
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
face = mtcnn(img_rgb)
if face is None:
raise ValueError("No face detected.")
if face.dim() == 3:
face = face.unsqueeze(0)
with torch.no_grad():
emb = facenet(face.to(device)).cpu().numpy()
return emb
def predict_face(img_path: str):
emb = get_embedding(img_path)
emb_norm = normalizer.transform(emb)
probs = svc_model.predict_proba(emb_norm)[0]
idx = np.argmax(probs)
label = label_encoder.inverse_transform([idx])[0]
confidence = float(probs[idx])
return label, confidence
# -------- RUN ----------
img_path = "test.jpg"
label, prob = predict_face(img_path)
print("Predicted Identity:", label)
print("Confidence Score:", prob)
```
---
## โ ๏ธ Important: Domain-Specific / Closed-Set Model
- This SVM is trained on **105 specific identities** from the dataset
`AI-Solutions-KK/face_recognition_dataset`.
- It will **always** predict one of these 105 classes, even for unseen people.
- For **new datasets / new identities**, you must retrain:
1. Compute new embeddings
2. Train SVM
3. Save: `svc_model.pkl`, `classes.npy`, `centroids.npy`
---
## ๐ Related Repositories & Live Demo
- **Dataset Repo**
https://huggingface.co/datasets/AI-Solutions-KK/face_recognition_dataset
- **Demo App (Hugging Face)**
https://huggingface.co/spaces/AI-Solutions-KK/face_recognition_model_demo_app
- **Stable Public Streamlit App**
https://facerecognition-tq32v5qkt4ltslejzwymw8.streamlit.app/
- **Full Training Code & Documentation**
https://github.com/AI-Solutions-KK/face_recognition_cnn_svm
---
## ๐งโ๐ง Train on Your Own Dataset
1. Prepare dataset (`root/class_name/image.jpg`)
2. Extract embeddings (FaceNet or your own)
3. Train SVM or cosine classifier
4. Save:
- `svc_model.pkl`
- `classes.npy`
- `centroids.npy`
Then plug into your own app or the provided Streamlit demo.
---
## ๐ค Author
**Karan (AI-Solutions-KK)**
|