eyad222 commited on
Commit
4428ffd
·
verified ·
1 Parent(s): 0249b1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,7 +1,6 @@
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")
@@ -9,38 +8,50 @@ 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()
 
1
  import gradio as gr
2
  import numpy as np
3
  import cv2
 
4
 
5
  # Load model data
6
  mean = np.load("mean.npy")
 
8
  projected_faces = np.load("projected_faces.npy")
9
  labels = np.load("labels.npy")
10
 
11
+ # Resize and flatten the image
12
  def preprocess(img):
13
+ try:
14
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
15
+ except Exception as e:
16
+ raise ValueError(f"Failed to convert to grayscale: {e}")
17
 
18
+ resized = cv2.resize(gray, (100, 100))
19
+ return resized.flatten()
20
+
21
+ # Predict function with debug
22
  def predict(img):
23
+ print("📸 Raw input image:", type(img))
24
+
25
  if img is None:
26
+ return "No image provided"
27
+
28
  try:
29
+ # Ensure the input is a NumPy array
30
+ if not isinstance(img, np.ndarray):
31
+ return f"❌ Unexpected input type: {type(img)}"
32
+
33
+ # Preprocess image
34
  img_vector = preprocess(img)
35
  img_centered = img_vector - mean
36
  img_projected = np.dot(eigenfaces.T, img_centered)
37
 
38
+ # Compare with dataset
39
  distances = np.linalg.norm(projected_faces - img_projected, axis=1)
40
  min_index = np.argmin(distances)
41
  predicted_label = labels[min_index]
42
+
43
+ return f"Match found: Person {predicted_label} (distance={distances[min_index]:.2f})"
44
+
45
  except Exception as e:
46
+ return f"⚠️ Error during prediction: {str(e)}"
47
 
48
+ # Gradio interface
49
  iface = gr.Interface(
50
  fn=predict,
51
  inputs=gr.Image(type="numpy", label="Upload Face Image"),
52
  outputs=gr.Textbox(label="Prediction Result"),
53
  title="Eigenfaces Face Recognition",
54
+ description="Upload a face image (JPG/PNG) to identify the most similar person from the dataset."
55
  )
56
 
57
  iface.launch()