-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML_FaceRecog_DataPrep.py
More file actions
76 lines (76 loc) · 2.53 KB
/
ML_FaceRecog_DataPrep.py
File metadata and controls
76 lines (76 loc) · 2.53 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
68
69
70
71
72
73
74
75
76
import cv2
import numpy as np
import os
offset = 20
dataset_path = "./data/"
faceData = []
labels = []
nameMap = {}
classId = 0
# Load face datasets
for f in os.listdir(dataset_path):
if f.endswith(".npy"):
nameMap[classId] = f[:-4] # Remove .npy extension
dataItem = np.load(os.path.join(dataset_path, f))
m = dataItem.shape[0]
faceData.append(dataItem)
target = classId * np.ones((m,))
labels.append(target)
classId += 1
# Combine all class data into training set
XT = np.concatenate(faceData, axis=0)
yT = np.concatenate(labels, axis=0).reshape((-1, 1))
print("Training Data Shape:", XT.shape)
print("Training Labels Shape:", yT.shape)
print("Class Map:", nameMap)
# Euclidean distance function
def dist(p, q):
return np.sqrt(np.sum((p - q) ** 2))
# KNN classifier
def knn(X, y, xt, k=5):
m = X.shape[0]
dlist = []
for i in range(m):
d = dist(X[i], xt)
dlist.append((d, y[i][0])) # Ensure scalar label
dlist = sorted(dlist, key=lambda x: x[0]) # Sort by distance
top_k = dlist[:k]
labels = [label for _, label in top_k]
labels, counts = np.unique(labels, return_counts=True)
idx = counts.argmax()
pred = labels[idx]
return int(pred)
# Initialize camera and Haar model
cam = cv2.VideoCapture(0)
model = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
while True:
success, img = cam.read()
if not success:
print("Reading Camera Failed!")
continue
faces = model.detectMultiScale(img, 1.3, 5)
for f in faces:
x, y, w, h = f
print(f)
# Clamp the region inside image bounds
x1 = max(x - offset, 0)
y1 = max(y - offset, 0)
x2 = min(x + w + offset, img.shape[1])
y2 = min(y + h + offset, img.shape[0])
cropped_face = img[y1:y2, x1:x2]
if cropped_face.size == 0:
continue
cropped_face = cv2.resize(cropped_face, (100, 100))
# Predict using KNN
classPredicted = knn(XT, yT, cropped_face.flatten())
namePredicted = nameMap[classPredicted]
print(f"Predicted: {namePredicted}")
# Draw prediction on frame
cv2.putText(img, namePredicted, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 200, 0), 2, cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Prediction Window", img)
key = cv2.waitKey(1)
if key == ord('q'):
break
cam.release()
cv2.destroyAllWindows()