l
File size: 2,047 Bytes
bc5f502
6524593
0249b1e
46f40e7
6524593
46f40e7
 
 
 
6524593
 
 
0249b1e
 
46f40e7
0249b1e
4428ffd
 
46f40e7
4428ffd
 
0249b1e
46f40e7
 
 
 
4428ffd
46f40e7
 
 
 
 
 
4428ffd
46f40e7
 
0249b1e
46f40e7
4428ffd
6524593
0249b1e
 
 
 
46f40e7
0249b1e
 
 
46f40e7
4428ffd
46f40e7
4428ffd
6524593
46f40e7
bc5f502
4428ffd
bc5f502
0249b1e
46f40e7
0249b1e
46f40e7
 
bc5f502
 
 
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
import numpy as np
import cv2
import os

# Load Haar cascade for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

# Load precomputed model data
mean = np.load("mean.npy")
eigenfaces = np.load("eigenfaces.npy")
projected_faces = np.load("projected_faces.npy")
labels = np.load("labels.npy")

# Preprocess the image: detect face, crop, grayscale, resize, flatten
def preprocess(img):
    try:
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        gray = cv2.equalizeHist(gray)  # Improve lighting
    except Exception as e:
        raise ValueError(f"Failed to convert to grayscale: {e}")

    # Detect face
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
    if len(faces) == 0:
        raise ValueError("❌ No face detected in the image.")

    # Crop the first face found
    x, y, w, h = faces[0]
    face_roi = gray[y:y+h, x:x+w]
    resized_face = cv2.resize(face_roi, (100, 100))

    return resized_face.flatten()

# Predict function
def predict(img):
    if img is None:
        return "❌ No image provided."

    try:
        img_vector = preprocess(img)
        img_centered = img_vector - mean
        img_projected = np.dot(eigenfaces.T, img_centered)

        # Calculate Euclidean distances
        distances = np.linalg.norm(projected_faces - img_projected, axis=1)
        min_index = np.argmin(distances)
        predicted_label = labels[min_index]
        confidence = distances[min_index]

        return f"✅ Match: Person {predicted_label} (distance = {confidence:.2f})"

    except Exception as e:
        return f"⚠️ Error: {str(e)}"

# Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="numpy", label="Upload or Capture a Face"),
    outputs=gr.Textbox(label="Prediction Result"),
    title="Enhanced Eigenfaces Face Recognition",
    description="Improved with face detection and lighting normalization. Upload a clear frontal face image."
)

iface.launch()