Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,27 +132,35 @@ def enroll_person(name: str, face_image: np.ndarray):
|
|
| 132 |
|
| 133 |
faces = recognizer.get(face_image, max_num=1)
|
| 134 |
if not faces:
|
| 135 |
-
return "No face detected!
|
| 136 |
|
| 137 |
emb = faces[0].normed_embedding
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
np.save(
|
|
|
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
-
index = faiss.IndexHNSWFlat(
|
| 152 |
-
index.
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
return f"Successfully enrolled: {name}"
|
| 156 |
|
| 157 |
# ==================== 4. GRADIO UI ====================
|
| 158 |
with gr.Blocks(title="SecureFace ID β Privacy-First Recognition") as demo:
|
|
|
|
| 132 |
|
| 133 |
faces = recognizer.get(face_image, max_num=1)
|
| 134 |
if not faces:
|
| 135 |
+
return "No face detected! Try a clearer photo."
|
| 136 |
|
| 137 |
emb = faces[0].normed_embedding
|
| 138 |
|
| 139 |
+
# Load existing data (or create new)
|
| 140 |
+
if not os.path.exists(KNOWN_EMBS_PATH):
|
| 141 |
+
all_embs = np.empty((0, 512))
|
| 142 |
+
all_names = []
|
| 143 |
+
else:
|
| 144 |
+
all_embs = np.load(KNOWN_EMBS_PATH)
|
| 145 |
+
all_names = np.load(KNOWN_NAMES_PATH, allow_pickle=True).tolist()
|
| 146 |
|
| 147 |
+
# Append new person
|
| 148 |
+
all_embs = np.vstack([all_embs, emb])
|
| 149 |
+
all_names.append(name)
|
| 150 |
|
| 151 |
+
# Save to disk
|
| 152 |
+
np.save(KNOWN_EMBS_PATH, all_embs)
|
| 153 |
+
np.save(KNOWN_NAMES_PATH, np.array(all_names))
|
| 154 |
|
| 155 |
+
# βββ THIS IS THE CRUCIAL PART βββ
|
| 156 |
+
# Rebuild FAISS index immediately
|
| 157 |
+
index = faiss.IndexHNSWFlat(512, 32)
|
| 158 |
+
index.hnsw.efSearch = 16
|
| 159 |
+
index.add(all_embs.astype('float32'))
|
| 160 |
+
known_names = all_names
|
| 161 |
+
# βββ END βββ
|
| 162 |
|
| 163 |
+
return f"Successfully enrolled: **{name}** β now instantly recognized!"
|
| 164 |
|
| 165 |
# ==================== 4. GRADIO UI ====================
|
| 166 |
with gr.Blocks(title="SecureFace ID β Privacy-First Recognition") as demo:
|