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 |
-
Using
|
| 5 |
"""
|
| 6 |
|
| 7 |
import warnings
|
|
@@ -10,7 +10,7 @@ import sys
|
|
| 10 |
import numpy as np
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
| 13 |
-
|
| 14 |
|
| 15 |
# Suppress warnings
|
| 16 |
warnings.filterwarnings('ignore')
|
|
@@ -19,71 +19,91 @@ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
|
| 19 |
|
| 20 |
class FacialRecognitionService:
|
| 21 |
def __init__(self):
|
| 22 |
-
"""Initialize
|
| 23 |
-
print("
|
| 24 |
-
#
|
| 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
|
| 33 |
try:
|
| 34 |
if image is None:
|
| 35 |
return None
|
| 36 |
|
| 37 |
-
#
|
| 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 |
-
#
|
| 46 |
-
|
| 47 |
-
img_path=img_rgb,
|
| 48 |
-
model_name="VGG-Face",
|
| 49 |
-
enforce_detection=True,
|
| 50 |
-
detector_backend="opencv"
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
if len(
|
| 54 |
-
return
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error extracting embedding: {e}", file=sys.stderr)
|
| 59 |
return None
|
| 60 |
|
| 61 |
def calculate_similarity(self, emb1, emb2):
|
| 62 |
-
"""
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Convert from [-1, 1] to [0, 1]
|
| 67 |
return float((similarity + 1) / 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except:
|
| 69 |
-
|
| 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
|
|
@@ -91,8 +111,16 @@ class FacialRecognitionService:
|
|
| 91 |
candidate_emb = self.extract_face_embedding(candidate)
|
| 92 |
if candidate_emb is None:
|
| 93 |
continue
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if similarity >= threshold:
|
| 97 |
matches.append({
|
| 98 |
'index': idx + 1,
|
|
@@ -101,14 +129,16 @@ class FacialRecognitionService:
|
|
| 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 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
return result
|
| 114 |
|
|
@@ -126,9 +156,9 @@ def extract_face(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\
|
| 132 |
|
| 133 |
|
| 134 |
def match_faces_fn(target_image, threshold, *candidate_images):
|
|
@@ -146,47 +176,67 @@ def match_faces_fn(target_image, threshold, *candidate_images):
|
|
| 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
|
| 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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 165 |
|
| 166 |
btn_extract.click(fn=extract_face, inputs=input_img, outputs=output_embed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
with gr.Tab("π Match Faces"):
|
| 169 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 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=
|
| 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 |
|
|
@@ -195,10 +245,57 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 195 |
inputs=[target_img, threshold_slider] + candidate_imgs,
|
| 196 |
outputs=output_matches
|
| 197 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
gr.Markdown("""
|
| 200 |
---
|
| 201 |
-
|
|
|
|
|
|
|
| 202 |
""")
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Facial Recognition Service with Gradio UI
|
| 4 |
+
Using face_recognition library for maximum Hugging Face Spaces compatibility
|
| 5 |
"""
|
| 6 |
|
| 7 |
import warnings
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
| 13 |
+
import face_recognition
|
| 14 |
|
| 15 |
# Suppress warnings
|
| 16 |
warnings.filterwarnings('ignore')
|
|
|
|
| 19 |
|
| 20 |
class FacialRecognitionService:
|
| 21 |
def __init__(self):
|
| 22 |
+
"""Initialize face recognition service"""
|
| 23 |
+
print("Face Recognition Service ready β
")
|
| 24 |
+
self.model = "large" # 'large' (more accurate) or 'small' (faster)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def extract_face_embedding(self, image: np.ndarray):
|
| 27 |
+
"""Extract 128-dimensional face embedding from an image"""
|
| 28 |
try:
|
| 29 |
if image is None:
|
| 30 |
return None
|
| 31 |
|
| 32 |
+
# Convert to RGB if needed (face_recognition requires RGB)
|
| 33 |
if len(image.shape) == 2: # Grayscale
|
| 34 |
img_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 35 |
elif image.shape[2] == 4: # RGBA
|
| 36 |
img_rgb = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 37 |
+
elif image.shape[2] == 3: # BGR (from OpenCV)
|
| 38 |
+
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 39 |
else:
|
| 40 |
img_rgb = image
|
| 41 |
|
| 42 |
+
# Detect face locations
|
| 43 |
+
face_locations = face_recognition.face_locations(img_rgb, model=self.model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
if len(face_locations) == 0:
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
# Get face encodings (embeddings)
|
| 49 |
+
face_encodings = face_recognition.face_encodings(img_rgb, face_locations, model=self.model)
|
| 50 |
+
|
| 51 |
+
if len(face_encodings) == 0:
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
# Return the first (or largest) face encoding
|
| 55 |
+
if len(face_locations) > 1:
|
| 56 |
+
# Find largest face
|
| 57 |
+
areas = [(loc[2] - loc[0]) * (loc[1] - loc[3]) for loc in face_locations]
|
| 58 |
+
largest_idx = np.argmax(areas)
|
| 59 |
+
return face_encodings[largest_idx]
|
| 60 |
+
|
| 61 |
+
return face_encodings[0]
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
print(f"Error extracting embedding: {e}", file=sys.stderr)
|
| 65 |
return None
|
| 66 |
|
| 67 |
def calculate_similarity(self, emb1, emb2):
|
| 68 |
+
"""Calculate cosine similarity normalized to 0-1 range"""
|
| 69 |
try:
|
| 70 |
+
# Normalize embeddings
|
| 71 |
+
norm1 = np.linalg.norm(emb1)
|
| 72 |
+
norm2 = np.linalg.norm(emb2)
|
| 73 |
+
|
| 74 |
+
if norm1 == 0 or norm2 == 0:
|
| 75 |
+
return 0.0
|
| 76 |
+
|
| 77 |
+
emb1_norm = emb1 / norm1
|
| 78 |
+
emb2_norm = emb2 / norm2
|
| 79 |
+
|
| 80 |
+
# Cosine similarity
|
| 81 |
+
similarity = np.dot(emb1_norm, emb2_norm)
|
| 82 |
+
|
| 83 |
# Convert from [-1, 1] to [0, 1]
|
| 84 |
return float((similarity + 1) / 2)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error calculating similarity: {e}", file=sys.stderr)
|
| 87 |
+
return 0.0
|
| 88 |
+
|
| 89 |
+
def calculate_face_distance(self, emb1, emb2):
|
| 90 |
+
"""Calculate Euclidean distance (lower is more similar)"""
|
| 91 |
+
try:
|
| 92 |
+
distance = np.linalg.norm(emb1 - emb2)
|
| 93 |
+
return float(distance)
|
| 94 |
except:
|
| 95 |
+
return float('inf')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
def match_faces(self, target_image: np.ndarray, candidate_images: list, threshold: float = 0.6, use_distance: bool = False):
|
| 98 |
"""Match target face against candidate images"""
|
| 99 |
matches = []
|
| 100 |
|
| 101 |
+
# Extract target embedding
|
| 102 |
target_emb = self.extract_face_embedding(target_image)
|
| 103 |
if target_emb is None:
|
| 104 |
return "β No face detected in target image"
|
| 105 |
|
| 106 |
+
# Compare with each candidate
|
| 107 |
for idx, candidate in enumerate(candidate_images):
|
| 108 |
if candidate is None:
|
| 109 |
continue
|
|
|
|
| 111 |
candidate_emb = self.extract_face_embedding(candidate)
|
| 112 |
if candidate_emb is None:
|
| 113 |
continue
|
| 114 |
+
|
| 115 |
+
if use_distance:
|
| 116 |
+
# Use face_recognition's built-in distance metric
|
| 117 |
+
distance = self.calculate_face_distance(target_emb, candidate_emb)
|
| 118 |
+
# Convert distance to similarity score (0.6 distance = ~60% match)
|
| 119 |
+
similarity = max(0, 1 - (distance / 1.2)) # Normalize to 0-1
|
| 120 |
+
else:
|
| 121 |
+
# Use cosine similarity
|
| 122 |
+
similarity = self.calculate_similarity(target_emb, candidate_emb)
|
| 123 |
+
|
| 124 |
if similarity >= threshold:
|
| 125 |
matches.append({
|
| 126 |
'index': idx + 1,
|
|
|
|
| 129 |
})
|
| 130 |
|
| 131 |
if not matches:
|
| 132 |
+
return f"β No matches found above {int(threshold * 100)}% threshold"
|
| 133 |
|
| 134 |
+
# Sort by confidence (highest first)
|
| 135 |
matches.sort(key=lambda x: x['confidence'], reverse=True)
|
| 136 |
|
| 137 |
+
result = "β
**Matches Found:**\n\n"
|
| 138 |
for m in matches:
|
| 139 |
+
confidence_bar = "β" * int(m['score'] / 10) + "β" * (10 - int(m['score'] / 10))
|
| 140 |
+
result += f"πΈ **Candidate {m['index']}:** {m['score']}%\n"
|
| 141 |
+
result += f" {confidence_bar}\n\n"
|
| 142 |
|
| 143 |
return result
|
| 144 |
|
|
|
|
| 156 |
|
| 157 |
embedding = service.extract_face_embedding(image)
|
| 158 |
if embedding is None:
|
| 159 |
+
return "β No face detected in image\n\nTips:\n- Ensure face is clearly visible\n- Face should be well-lit\n- Try a different angle"
|
| 160 |
|
| 161 |
+
return f"β
**Face detected successfully!**\n\nπ Embedding Details:\n- Dimensions: {len(embedding)}\n- Model: dlib (HOG + CNN)\n- Encoding: 128-D vector\n\nThis embedding can be used for facial recognition and comparison."
|
| 162 |
|
| 163 |
|
| 164 |
def match_faces_fn(target_image, threshold, *candidate_images):
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
# Gradio UI
|
| 179 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Facial Recognition Service") as demo:
|
| 180 |
gr.Markdown("""
|
| 181 |
# π Facial Recognition Service
|
| 182 |
+
### Powered by dlib's state-of-the-art face recognition
|
| 183 |
|
| 184 |
Upload images to extract face embeddings or match faces across multiple images.
|
| 185 |
+
- 128-dimensional face encodings
|
| 186 |
+
- High accuracy facial recognition
|
| 187 |
+
- CPU-optimized for Hugging Face Spaces
|
| 188 |
""")
|
| 189 |
|
| 190 |
with gr.Tab("π― Extract Face Embedding"):
|
| 191 |
+
gr.Markdown("""
|
| 192 |
+
Upload a single image to extract facial features. The system will:
|
| 193 |
+
- Detect the face in the image
|
| 194 |
+
- Extract a 128-dimensional embedding vector
|
| 195 |
+
- Return the embedding information
|
| 196 |
+
""")
|
| 197 |
+
|
| 198 |
with gr.Row():
|
| 199 |
with gr.Column():
|
| 200 |
+
input_img = gr.Image(label="Upload Image", type="numpy", height=400)
|
| 201 |
+
btn_extract = gr.Button("π Extract Embedding", variant="primary", size="lg")
|
| 202 |
with gr.Column():
|
| 203 |
+
output_embed = gr.Textbox(label="Result", lines=10, max_lines=15)
|
| 204 |
|
| 205 |
btn_extract.click(fn=extract_face, inputs=input_img, outputs=output_embed)
|
| 206 |
+
|
| 207 |
+
gr.Markdown("""
|
| 208 |
+
**Tips for best results:**
|
| 209 |
+
- Use clear, well-lit photos
|
| 210 |
+
- Face should be visible and not obstructed
|
| 211 |
+
- Front-facing photos work best
|
| 212 |
+
""")
|
| 213 |
|
| 214 |
with gr.Tab("π Match Faces"):
|
| 215 |
+
gr.Markdown("""
|
| 216 |
+
Upload a target face and up to 5 candidate images to find matches.
|
| 217 |
+
The system compares facial features and returns similarity scores.
|
| 218 |
+
""")
|
| 219 |
|
| 220 |
with gr.Row():
|
| 221 |
with gr.Column(scale=1):
|
| 222 |
+
target_img = gr.Image(label="π― Target Image", type="numpy", height=300)
|
| 223 |
threshold_slider = gr.Slider(
|
| 224 |
minimum=0.3,
|
| 225 |
+
maximum=0.95,
|
| 226 |
value=0.6,
|
| 227 |
step=0.05,
|
| 228 |
label="Match Threshold",
|
| 229 |
+
info="Higher = stricter matching (0.6 recommended)"
|
| 230 |
)
|
| 231 |
+
btn_match = gr.Button("π Find Matches", variant="primary", size="lg")
|
| 232 |
|
| 233 |
with gr.Column(scale=1):
|
| 234 |
+
output_matches = gr.Textbox(label="Match Results", lines=15, max_lines=20)
|
| 235 |
|
| 236 |
+
gr.Markdown("### πΈ Candidate Images")
|
| 237 |
with gr.Row():
|
| 238 |
candidate_imgs = [
|
| 239 |
+
gr.Image(label=f"Candidate {i+1}", type="numpy", height=200)
|
| 240 |
for i in range(5)
|
| 241 |
]
|
| 242 |
|
|
|
|
| 245 |
inputs=[target_img, threshold_slider] + candidate_imgs,
|
| 246 |
outputs=output_matches
|
| 247 |
)
|
| 248 |
+
|
| 249 |
+
gr.Markdown("""
|
| 250 |
+
**Similarity Scoring:**
|
| 251 |
+
- 90-100%: Very high confidence match
|
| 252 |
+
- 70-89%: High confidence match
|
| 253 |
+
- 60-69%: Good match
|
| 254 |
+
- Below 60%: Low confidence
|
| 255 |
+
""")
|
| 256 |
+
|
| 257 |
+
with gr.Tab("βΉοΈ About"):
|
| 258 |
+
gr.Markdown("""
|
| 259 |
+
## About This Service
|
| 260 |
+
|
| 261 |
+
This facial recognition system uses **dlib's face recognition model**, which provides:
|
| 262 |
+
|
| 263 |
+
- **High Accuracy**: 99.38% accuracy on the Labeled Faces in the Wild benchmark
|
| 264 |
+
- **128-D Embeddings**: Compact representation of facial features
|
| 265 |
+
- **Robust Detection**: Works with various lighting conditions and angles
|
| 266 |
+
- **Privacy-Focused**: All processing happens in your browser session
|
| 267 |
+
|
| 268 |
+
### How It Works
|
| 269 |
+
|
| 270 |
+
1. **Face Detection**: Locates faces in uploaded images
|
| 271 |
+
2. **Feature Extraction**: Generates 128-dimensional embedding vectors
|
| 272 |
+
3. **Similarity Comparison**: Compares embeddings using cosine similarity
|
| 273 |
+
4. **Threshold Filtering**: Returns matches above the confidence threshold
|
| 274 |
+
|
| 275 |
+
### Use Cases
|
| 276 |
+
|
| 277 |
+
- Identity verification
|
| 278 |
+
- Duplicate photo detection
|
| 279 |
+
- Face clustering in photo libraries
|
| 280 |
+
- Security and access control systems
|
| 281 |
+
|
| 282 |
+
### Technical Details
|
| 283 |
+
|
| 284 |
+
- **Model**: dlib ResNet-based face recognition
|
| 285 |
+
- **Detection**: HOG + CNN face detector
|
| 286 |
+
- **Embedding Size**: 128 dimensions
|
| 287 |
+
- **Computing**: CPU-optimized (no GPU required)
|
| 288 |
+
|
| 289 |
+
---
|
| 290 |
+
|
| 291 |
+
**Note:** This app runs entirely on CPU. Processing time: ~1-3 seconds per image.
|
| 292 |
+
""")
|
| 293 |
|
| 294 |
gr.Markdown("""
|
| 295 |
---
|
| 296 |
+
<div style="text-align: center; color: #666; font-size: 0.9em;">
|
| 297 |
+
π Privacy: All processing happens on the server. Images are not stored.
|
| 298 |
+
</div>
|
| 299 |
""")
|
| 300 |
|
| 301 |
if __name__ == "__main__":
|