-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_recognition_module.py
More file actions
67 lines (56 loc) · 2.18 KB
/
face_recognition_module.py
File metadata and controls
67 lines (56 loc) · 2.18 KB
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
67
import cv2
import numpy as np
import os
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
def extract_face(image_path):
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
img = cv2.imread(image_path)
if img is None:
return None
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
return gray[y:y+h, x:x+w]
return None
def prepare_dataset(dataset_path):
X, y = [], []
label_dict = {}
label = 0
for person_name in os.listdir(dataset_path):
person_path = os.path.join(dataset_path, person_name)
if not os.path.isdir(person_path):
continue
label_dict[label] = person_name
for image_name in os.listdir(person_path):
image_path = os.path.join(person_path, image_name)
face = extract_face(image_path)
if face is not None:
face_resized = cv2.resize(face, (100, 100)).flatten()
X.append(face_resized)
y.append(label)
label += 1
return np.array(X), np.array(y), label_dict
def train_models(X, y):
if len(X) == 0:
raise ValueError("Training data is empty. Check your dataset.")
# PCA components
n_components = min(50, X.shape[0], X.shape[1])
pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)
# LDA components n_classes - 1 components
lda_components = min(len(np.unique(y)) - 1, X_pca.shape[1])
lda = LDA(n_components=lda_components)
X_lda = lda.fit_transform(X_pca, y)
return pca, lda, X_lda
def recognize_face(image_path, pca, lda, X_lda, y, label_dict):
face = extract_face(image_path)
if face is None:
return "No face detected"
face_resized = cv2.resize(face, (100, 100)).flatten()
face_pca = pca.transform([face_resized])
face_lda = lda.transform(face_pca)
distances = np.linalg.norm(X_lda - face_lda, axis=1)
min_index = np.argmin(distances)
predicted_label = y[min_index]
return label_dict[predicted_label]