eyad222 commited on
Commit
6524593
·
verified ·
1 Parent(s): c4aabb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -34
app.py CHANGED
@@ -1,41 +1,58 @@
1
  import gradio as gr
2
- import numpy as np
3
  import cv2
4
- from sklearn.metrics.pairwise import euclidean_distances
5
-
6
- # Load model files
7
- mean_face = np.load("model/mean_face.npy")
8
- eigenfaces = np.load("model/eigenfaces.npy")
9
- projections = np.load("model/projections.npy")
10
- labels = np.load("model/labels.npy")
11
- label_names = np.load("model/label_names.npy")
12
- with open("model/img_shape.txt", "r") as f:
13
- img_h, img_w = map(int, f.read().strip().split())
14
-
15
- THRESHOLD = 8000
16
-
17
- def recognize_face_from_frame(frame):
18
- img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
19
- img = cv2.resize(img, (img_w, img_h))
20
- img_flat = img.flatten()
21
- img_centered = img_flat - mean_face
22
- projected = np.dot(img_centered, eigenfaces.T)
23
- dists = euclidean_distances([projected], projections)[0]
24
- min_dist = np.min(dists)
25
- min_index = np.argmin(dists)
26
-
27
- if min_dist < THRESHOLD:
28
- predicted_name = label_names[labels[min_index]]
29
- return f"🧠 Predicted: {predicted_name} (distance: {min_dist:.2f})"
30
- else:
31
- return "😕 Unknown face"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  iface = gr.Interface(
34
- fn=recognize_face_from_frame,
35
- inputs=gr.Image(type="numpy"),
36
- outputs="text",
37
- title="🔍 EigenFace Recognition",
38
- description="Upload a face image (webcam capture available on some devices)."
 
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()