Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Facial Recognition Service with Gradio UI
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import warnings
|
|
@@ -10,41 +10,49 @@ import sys
|
|
| 10 |
import numpy as np
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
|
|
|
| 13 |
|
| 14 |
# Suppress warnings
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 17 |
|
| 18 |
-
# InsightFace PyTorch backend
|
| 19 |
-
from insightface.app import FaceAnalysis
|
| 20 |
-
|
| 21 |
|
| 22 |
class FacialRecognitionService:
|
| 23 |
def __init__(self):
|
| 24 |
-
"""Initialize
|
| 25 |
-
print("Loading
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def extract_face_embedding(self, image: np.ndarray):
|
| 32 |
-
"""Extract face embedding from an uploaded image
|
| 33 |
try:
|
| 34 |
if image is None:
|
| 35 |
return None
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
img_rgb = image
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Error extracting embedding: {e}", file=sys.stderr)
|
|
@@ -53,71 +61,145 @@ class FacialRecognitionService:
|
|
| 53 |
def calculate_similarity(self, emb1, emb2):
|
| 54 |
"""Cosine similarity normalized to 0-1"""
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return float((np.dot(emb1, emb2) + 1) / 2)
|
| 61 |
except:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def match_faces(self, target_image: np.ndarray, candidate_images: list, threshold: float = 0.6):
|
|
|
|
| 65 |
matches = []
|
|
|
|
| 66 |
target_emb = self.extract_face_embedding(target_image)
|
| 67 |
if target_emb is None:
|
| 68 |
-
return "No face detected in target image"
|
| 69 |
|
| 70 |
for idx, candidate in enumerate(candidate_images):
|
|
|
|
|
|
|
|
|
|
| 71 |
candidate_emb = self.extract_face_embedding(candidate)
|
| 72 |
if candidate_emb is None:
|
| 73 |
continue
|
|
|
|
| 74 |
similarity = self.calculate_similarity(target_emb, candidate_emb)
|
| 75 |
if similarity >= threshold:
|
| 76 |
matches.append({
|
| 77 |
-
'index': idx,
|
| 78 |
'confidence': similarity,
|
| 79 |
'score': int(similarity * 100)
|
| 80 |
})
|
| 81 |
|
| 82 |
if not matches:
|
| 83 |
-
return "No matches found"
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
# Initialize service
|
|
|
|
| 89 |
service = FacialRecognitionService()
|
| 90 |
|
| 91 |
|
| 92 |
# Gradio functions
|
| 93 |
def extract_face(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
embedding = service.extract_face_embedding(image)
|
| 95 |
if embedding is None:
|
| 96 |
-
return "No face detected"
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
-
def match_faces_fn(target_image, *candidate_images):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
candidates = [img for img in candidate_images if img is not None]
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return result
|
| 104 |
|
| 105 |
|
| 106 |
# Gradio UI
|
| 107 |
-
with gr.Blocks() as demo:
|
| 108 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
with gr.Tab("Extract Embedding"):
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
btn_extract.click(fn=extract_face, inputs=input_img, outputs=output_embed)
|
| 115 |
|
| 116 |
-
with gr.Tab("Match Faces"):
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Facial Recognition Service with Gradio UI
|
| 4 |
+
Using DeepFace for Hugging Face Spaces compatibility
|
| 5 |
"""
|
| 6 |
|
| 7 |
import warnings
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
| 13 |
+
from deepface import DeepFace
|
| 14 |
|
| 15 |
# Suppress warnings
|
| 16 |
warnings.filterwarnings('ignore')
|
| 17 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class FacialRecognitionService:
|
| 21 |
def __init__(self):
|
| 22 |
+
"""Initialize DeepFace with VGG-Face model"""
|
| 23 |
+
print("Loading DeepFace model...")
|
| 24 |
+
# Pre-load model
|
| 25 |
+
try:
|
| 26 |
+
DeepFace.build_model("VGG-Face")
|
| 27 |
+
print("Model loaded β
")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Model loading warning: {e}")
|
| 30 |
|
| 31 |
def extract_face_embedding(self, image: np.ndarray):
|
| 32 |
+
"""Extract face embedding from an uploaded image"""
|
| 33 |
try:
|
| 34 |
if image is None:
|
| 35 |
return None
|
| 36 |
|
| 37 |
+
# DeepFace expects RGB
|
| 38 |
+
if len(image.shape) == 2: # Grayscale
|
| 39 |
+
img_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 40 |
+
elif image.shape[2] == 4: # RGBA
|
| 41 |
+
img_rgb = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 42 |
else:
|
| 43 |
img_rgb = image
|
| 44 |
|
| 45 |
+
# Extract embedding
|
| 46 |
+
embedding_objs = DeepFace.represent(
|
| 47 |
+
img_path=img_rgb,
|
| 48 |
+
model_name="VGG-Face",
|
| 49 |
+
enforce_detection=True,
|
| 50 |
+
detector_backend="opencv"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if len(embedding_objs) > 0:
|
| 54 |
+
return np.array(embedding_objs[0]["embedding"])
|
| 55 |
+
return None
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error extracting embedding: {e}", file=sys.stderr)
|
|
|
|
| 61 |
def calculate_similarity(self, emb1, emb2):
|
| 62 |
"""Cosine similarity normalized to 0-1"""
|
| 63 |
try:
|
| 64 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 65 |
+
similarity = cosine_similarity([emb1], [emb2])[0][0]
|
| 66 |
+
# Convert from [-1, 1] to [0, 1]
|
| 67 |
+
return float((similarity + 1) / 2)
|
|
|
|
| 68 |
except:
|
| 69 |
+
# Fallback manual calculation
|
| 70 |
+
try:
|
| 71 |
+
norm1, norm2 = np.linalg.norm(emb1), np.linalg.norm(emb2)
|
| 72 |
+
if norm1 == 0 or norm2 == 0:
|
| 73 |
+
return 0.0
|
| 74 |
+
emb1_norm, emb2_norm = emb1 / norm1, emb2 / norm2
|
| 75 |
+
return float((np.dot(emb1_norm, emb2_norm) + 1) / 2)
|
| 76 |
+
except:
|
| 77 |
+
return 0.0
|
| 78 |
|
| 79 |
def match_faces(self, target_image: np.ndarray, candidate_images: list, threshold: float = 0.6):
|
| 80 |
+
"""Match target face against candidate images"""
|
| 81 |
matches = []
|
| 82 |
+
|
| 83 |
target_emb = self.extract_face_embedding(target_image)
|
| 84 |
if target_emb is None:
|
| 85 |
+
return "β No face detected in target image"
|
| 86 |
|
| 87 |
for idx, candidate in enumerate(candidate_images):
|
| 88 |
+
if candidate is None:
|
| 89 |
+
continue
|
| 90 |
+
|
| 91 |
candidate_emb = self.extract_face_embedding(candidate)
|
| 92 |
if candidate_emb is None:
|
| 93 |
continue
|
| 94 |
+
|
| 95 |
similarity = self.calculate_similarity(target_emb, candidate_emb)
|
| 96 |
if similarity >= threshold:
|
| 97 |
matches.append({
|
| 98 |
+
'index': idx + 1,
|
| 99 |
'confidence': similarity,
|
| 100 |
'score': int(similarity * 100)
|
| 101 |
})
|
| 102 |
|
| 103 |
if not matches:
|
| 104 |
+
return "β No matches found above threshold"
|
| 105 |
|
| 106 |
+
# Sort by confidence
|
| 107 |
+
matches.sort(key=lambda x: x['confidence'], reverse=True)
|
| 108 |
+
|
| 109 |
+
result = "β
Matches Found:\n\n"
|
| 110 |
+
for m in matches:
|
| 111 |
+
result += f"πΈ Candidate {m['index']}: {m['score']}% match\n"
|
| 112 |
+
|
| 113 |
+
return result
|
| 114 |
|
| 115 |
|
| 116 |
# Initialize service
|
| 117 |
+
print("Initializing Facial Recognition Service...")
|
| 118 |
service = FacialRecognitionService()
|
| 119 |
|
| 120 |
|
| 121 |
# Gradio functions
|
| 122 |
def extract_face(image):
|
| 123 |
+
"""Extract embedding from single image"""
|
| 124 |
+
if image is None:
|
| 125 |
+
return "β Please upload an image"
|
| 126 |
+
|
| 127 |
embedding = service.extract_face_embedding(image)
|
| 128 |
if embedding is None:
|
| 129 |
+
return "β No face detected in image"
|
| 130 |
+
|
| 131 |
+
return f"β
Face detected!\n\nEmbedding size: {len(embedding)} dimensions\nModel: VGG-Face"
|
| 132 |
|
| 133 |
|
| 134 |
+
def match_faces_fn(target_image, threshold, *candidate_images):
|
| 135 |
+
"""Match faces with configurable threshold"""
|
| 136 |
+
if target_image is None:
|
| 137 |
+
return "β Please upload a target image"
|
| 138 |
+
|
| 139 |
candidates = [img for img in candidate_images if img is not None]
|
| 140 |
+
|
| 141 |
+
if len(candidates) == 0:
|
| 142 |
+
return "β Please upload at least one candidate image"
|
| 143 |
+
|
| 144 |
+
result = service.match_faces(target_image, candidates, threshold=threshold)
|
| 145 |
return result
|
| 146 |
|
| 147 |
|
| 148 |
# Gradio UI
|
| 149 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 150 |
+
gr.Markdown("""
|
| 151 |
+
# π Facial Recognition Service
|
| 152 |
+
### Powered by DeepFace (VGG-Face model)
|
| 153 |
+
|
| 154 |
+
Upload images to extract face embeddings or match faces across multiple images.
|
| 155 |
+
""")
|
| 156 |
|
| 157 |
+
with gr.Tab("π― Extract Face Embedding"):
|
| 158 |
+
gr.Markdown("Upload a single image to extract facial features.")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column():
|
| 161 |
+
input_img = gr.Image(label="Upload Image", type="numpy")
|
| 162 |
+
btn_extract = gr.Button("π Extract Embedding", variant="primary")
|
| 163 |
+
with gr.Column():
|
| 164 |
+
output_embed = gr.Textbox(label="Result", lines=5)
|
| 165 |
+
|
| 166 |
btn_extract.click(fn=extract_face, inputs=input_img, outputs=output_embed)
|
| 167 |
|
| 168 |
+
with gr.Tab("π Match Faces"):
|
| 169 |
+
gr.Markdown("Upload a target face and up to 5 candidate images to find matches.")
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
with gr.Column(scale=1):
|
| 173 |
+
target_img = gr.Image(label="π― Target Image", type="numpy")
|
| 174 |
+
threshold_slider = gr.Slider(
|
| 175 |
+
minimum=0.3,
|
| 176 |
+
maximum=0.9,
|
| 177 |
+
value=0.6,
|
| 178 |
+
step=0.05,
|
| 179 |
+
label="Match Threshold",
|
| 180 |
+
info="Higher = stricter matching"
|
| 181 |
+
)
|
| 182 |
+
btn_match = gr.Button("π Find Matches", variant="primary")
|
| 183 |
+
|
| 184 |
+
with gr.Column(scale=1):
|
| 185 |
+
output_matches = gr.Textbox(label="Match Results", lines=12)
|
| 186 |
+
|
| 187 |
+
with gr.Row():
|
| 188 |
+
candidate_imgs = [
|
| 189 |
+
gr.Image(label=f"Candidate {i+1}", type="numpy")
|
| 190 |
+
for i in range(5)
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
+
btn_match.click(
|
| 194 |
+
fn=match_faces_fn,
|
| 195 |
+
inputs=[target_img, threshold_slider] + candidate_imgs,
|
| 196 |
+
outputs=output_matches
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
gr.Markdown("""
|
| 200 |
+
---
|
| 201 |
+
**Note:** This app runs on CPU. Processing may take a few seconds per image.
|
| 202 |
+
""")
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
demo.launch()
|