Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,65 +1,42 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import
|
| 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 |
-
continue
|
| 44 |
-
|
| 45 |
-
for img_file in os.listdir(user_path):
|
| 46 |
-
img_path = os.path.join(user_path, img_file)
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
val_img = Image.open(img_path).convert('RGB')
|
| 50 |
-
except:
|
| 51 |
-
continue
|
| 52 |
-
|
| 53 |
-
val_processed = preprocess(val_img)
|
| 54 |
-
|
| 55 |
-
# Predict
|
| 56 |
-
result = model.predict([input_processed, val_processed])[0][0]
|
| 57 |
-
|
| 58 |
-
if result > highest_score:
|
| 59 |
-
highest_score = result
|
| 60 |
-
identity = user
|
| 61 |
-
|
| 62 |
-
if highest_score < detection_threshold:
|
| 63 |
-
return "User not recognized."
|
| 64 |
-
else:
|
| 65 |
-
return f"Verified: {identity} (Confidence: {highest_score:.2f})"
|
|
|
|
| 1 |
+
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load models once
|
| 8 |
+
mtcnn = MTCNN(image_size=160, margin=20)
|
| 9 |
+
model = InceptionResnetV1(pretrained='vggface2').eval()
|
| 10 |
+
|
| 11 |
+
def preprocess(image):
|
| 12 |
+
face = mtcnn(Image.fromarray(image))
|
| 13 |
+
if face is None:
|
| 14 |
+
return None
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
embedding = model(face.unsqueeze(0)).squeeze().numpy()
|
| 17 |
+
return embedding
|
| 18 |
+
|
| 19 |
+
def verify_identity(_, input_image, user_dir, threshold=0.7):
|
| 20 |
+
input_embedding = preprocess(input_image)
|
| 21 |
+
if input_embedding is None:
|
| 22 |
+
return "❌ No face detected."
|
| 23 |
+
|
| 24 |
+
best_match = None
|
| 25 |
+
best_score = 0
|
| 26 |
+
|
| 27 |
+
for user in os.listdir(user_dir):
|
| 28 |
+
user_path = os.path.join(user_dir, user)
|
| 29 |
+
for file in os.listdir(user_path):
|
| 30 |
+
img_path = os.path.join(user_path, file)
|
| 31 |
+
registered_img = np.array(Image.open(img_path))
|
| 32 |
+
emb = preprocess(registered_img)
|
| 33 |
+
if emb is None:
|
| 34 |
+
continue
|
| 35 |
+
score = np.dot(input_embedding, emb) / (np.linalg.norm(input_embedding) * np.linalg.norm(emb))
|
| 36 |
+
if score > best_score:
|
| 37 |
+
best_match, best_score = user, score
|
| 38 |
+
|
| 39 |
+
if best_score >= threshold:
|
| 40 |
+
return f"✅ Match: {best_match} (Similarity: {best_score:.2f})"
|
| 41 |
+
else:
|
| 42 |
+
return f"❌ Unknown (Best Similarity: {best_score:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|