Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import cv2
|
| 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 |
iface = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
-
inputs=gr.Image(type="numpy"),
|
| 36 |
-
outputs="
|
| 37 |
-
title="
|
| 38 |
-
description="Upload a
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load your trained model and database here
|
| 7 |
+
# Example: load mean face, eigenfaces, and face projections
|
| 8 |
+
mean = np.load("mean.npy")
|
| 9 |
+
eigenfaces = np.load("eigenfaces.npy")
|
| 10 |
+
projected_faces = np.load("projected_faces.npy")
|
| 11 |
+
labels = np.load("labels.npy") # Assuming you saved labels for your dataset
|
| 12 |
+
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
# Convert to grayscale
|
| 15 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 16 |
+
resized = cv2.resize(gray, (100, 100)).flatten()
|
| 17 |
+
normalized = resized.astype('float32')
|
| 18 |
+
return normalized
|
| 19 |
+
|
| 20 |
+
def project_face(face_vector, mean, eigenfaces):
|
| 21 |
+
centered_vector = face_vector - mean
|
| 22 |
+
return np.dot(eigenfaces, centered_vector)
|
| 23 |
+
|
| 24 |
+
def recognize_face(uploaded_image):
|
| 25 |
+
try:
|
| 26 |
+
if uploaded_image is None:
|
| 27 |
+
return "No image provided."
|
| 28 |
+
|
| 29 |
+
face_vector = preprocess_image(uploaded_image)
|
| 30 |
+
projected = project_face(face_vector, mean, eigenfaces)
|
| 31 |
+
|
| 32 |
+
# Compare with database
|
| 33 |
+
min_dist = float('inf')
|
| 34 |
+
matched_label = "Unknown"
|
| 35 |
+
for idx, known_proj in enumerate(projected_faces):
|
| 36 |
+
dist = np.linalg.norm(projected - known_proj)
|
| 37 |
+
if dist < min_dist:
|
| 38 |
+
min_dist = dist
|
| 39 |
+
matched_label = labels[idx]
|
| 40 |
+
|
| 41 |
+
threshold = 3000 # Adjust based on your model
|
| 42 |
+
if min_dist < threshold:
|
| 43 |
+
return f"Matched: {matched_label} (Distance: {min_dist:.2f})"
|
| 44 |
+
else:
|
| 45 |
+
return "No match found"
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error: {str(e)}"
|
| 48 |
|
| 49 |
iface = gr.Interface(
|
| 50 |
+
fn=recognize_face,
|
| 51 |
+
inputs=gr.Image(type="numpy", label="Take or upload a photo"),
|
| 52 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 53 |
+
title="Eigenface Face Recognition",
|
| 54 |
+
description="Upload or take a photo. The model will identify the person if in the database.",
|
| 55 |
+
allow_flagging="never",
|
| 56 |
)
|
| 57 |
|
| 58 |
iface.launch()
|