eyad222 commited on
Commit
e9e1d02
·
verified ·
1 Parent(s): f699e72

Upload 9 files

Browse files
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import joblib
5
+ from sklearn.metrics.pairwise import euclidean_distances
6
+
7
+ # Load model files
8
+ mean_face = np.load("model/mean_face.npy")
9
+ eigenfaces = np.load("model/eigenfaces.npy")
10
+ projections = np.load("model/projections.npy")
11
+ labels = np.load("model/labels.npy")
12
+ label_names = np.load("model/label_names.npy")
13
+ with open("model/img_shape.txt", "r") as f:
14
+ img_h, img_w = map(int, f.read().strip().split())
15
+
16
+ # Distance threshold (tune based on performance)
17
+ THRESHOLD = 8000
18
+
19
+ def recognize_face_from_frame(frame):
20
+ # Convert to grayscale
21
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
22
+ # Resize to training size
23
+ img = cv2.resize(img, (img_w, img_h))
24
+ # Flatten and normalize
25
+ img_flat = img.flatten()
26
+ img_centered = img_flat - mean_face
27
+ # Project to eigenface space
28
+ projected = np.dot(img_centered, eigenfaces.T)
29
+
30
+ # Compare with stored projections
31
+ dists = euclidean_distances([projected], projections)[0]
32
+ min_dist = np.min(dists)
33
+ min_index = np.argmin(dists)
34
+
35
+ if min_dist < THRESHOLD:
36
+ predicted_name = label_names[labels[min_index]]
37
+ return f"🧠 Predicted: {predicted_name} (distance: {min_dist:.2f})"
38
+ else:
39
+ return "😕 Unknown face"
40
+
41
+ # Web UI
42
+ iface = gr.Interface(
43
+ fn=recognize_face_from_frame,
44
+ inputs=gr.Image(source="webcam", streaming=False),
45
+ outputs="text",
46
+ title="🔍 EigenFace Recognition",
47
+ description="Scan your face. If you're in the dataset, the system will recognize you."
48
+ )
49
+
50
+ iface.launch()
model/eigenfaces.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:901fe3c76ff23cce149af0198bf054afa335a23f6059c5caa1a8fa0750d4cd61
3
+ size 4000128
model/img_shape.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ 100 100
model/label_names.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:95a47b3adf7d2655e7510db1ec8c8a3fe9cf8570685570e4dad049f9256b0ddb
3
+ size 608
model/labels.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b5231638be0dbd85128e0f8684dc477f9e475b26f156714074b4f1f7853f942
3
+ size 3328
model/mean_face.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d0bc76df55e69cb65f8195ba8d672e9f68dbe6eb1464cb3183f4f81a41e4ee6e
3
+ size 80128
model/pca_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2d1a6e21a20ba2f98d48636d0285b231b1bf78b848947edab26db11a91b8f2ec
3
+ size 4082126
model/projections.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d5aa77c51e5bb15bc006728e721a7d5697d499d24c3565e2cc532310215b5d12
3
+ size 160128
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ opencv-python
4
+ scikit-learn
5
+ joblib