Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# Load
|
| 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")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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=
|
| 51 |
-
inputs=gr.Image(type="numpy", label="
|
| 52 |
-
outputs=gr.Textbox(label="Prediction"),
|
| 53 |
-
title="
|
| 54 |
-
description="Upload
|
| 55 |
-
allow_flagging="never",
|
| 56 |
)
|
| 57 |
|
| 58 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Load model data
|
|
|
|
| 7 |
mean = np.load("mean.npy")
|
| 8 |
eigenfaces = np.load("eigenfaces.npy")
|
| 9 |
projected_faces = np.load("projected_faces.npy")
|
| 10 |
+
labels = np.load("labels.npy")
|
| 11 |
+
|
| 12 |
+
# Preprocessing
|
| 13 |
+
def preprocess(img):
|
| 14 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 15 |
+
img = cv2.resize(img, (100, 100)) # or your dataset size
|
| 16 |
+
img_flat = img.flatten()
|
| 17 |
+
return img_flat
|
| 18 |
+
|
| 19 |
+
# Prediction function
|
| 20 |
+
def predict(img):
|
| 21 |
+
if img is None:
|
| 22 |
+
return "No image provided"
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
+
img_vector = preprocess(img)
|
| 26 |
+
img_centered = img_vector - mean
|
| 27 |
+
img_projected = np.dot(eigenfaces.T, img_centered)
|
| 28 |
+
|
| 29 |
+
distances = np.linalg.norm(projected_faces - img_projected, axis=1)
|
| 30 |
+
min_index = np.argmin(distances)
|
| 31 |
+
predicted_label = labels[min_index]
|
| 32 |
+
|
| 33 |
+
return f"Match found: Person {predicted_label} (distance={distances[min_index]:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
+
return f"Error processing image: {str(e)}"
|
| 36 |
|
| 37 |
+
# Gradio UI
|
| 38 |
iface = gr.Interface(
|
| 39 |
+
fn=predict,
|
| 40 |
+
inputs=gr.Image(type="numpy", label="Upload Face Image"),
|
| 41 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 42 |
+
title="Eigenfaces Face Recognition",
|
| 43 |
+
description="Upload a face image to identify the most similar person from the dataset."
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
iface.launch()
|