-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognition.py
93 lines (69 loc) · 2.13 KB
/
face_recognition.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import cv2
import numpy as np
import os
####KNN ALGORITHM CODE#####
def dist(X1,X2):
return np.sqrt(np.sum((X1-X2)**2))
def knn(X,Query, k = 5):
m = X.shape[0]
# print(Query.shape)
vals = []
for i in range(m):
xi=X[i,:-1]
yi=X[i,-1]
# print(Query[i].shape,X[i].shape)
d = dist(Query, xi)
vals.append((d,yi))
vals = sorted(vals,key= lambda x:x[0])[:k]
vals = np.asarray(vals)
new_vals = np.unique(vals[:,1],return_counts = True)
# print(new_vals)
index = new_vals[1].argmax()
pred = new_vals[0][index]
return pred
##############################
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
skip=0
face_data=[]
dataset_path='./data/'
labels=[]
class_id=0
names={}
#face_section=np.zeros((100,100),dtype='uint8')
for fx in os.listdir(dataset_path):
if fx.endswith('.npy'):
names[class_id]=fx[:-4]
print("loaded "+fx)
data_item=np.load(dataset_path+fx)
face_data.append(data_item)
target=class_id*np.ones((data_item.shape[0],))
class_id+=1
labels.append(target)
face_dataset=np.concatenate(face_data,axis= 0)
face_labels=np.concatenate(labels,axis=0).reshape((-1,1))
print(face_dataset.shape)
print(face_labels.shape)
trainset=np.concatenate((face_dataset,face_labels),axis=1)
print(trainset.shape)
while True:
ret,frame=cap.read()
if ret==False:
continue
faces=face_cascade.detectMultiScale(frame,1.3,5)
faces=sorted(faces,key=lambda f:f[2]*f[3])
for face in faces:
x,y,w,h=face
offset=10
face_section=frame[y-offset:y+h+offset,x-offset:x+w+offset]
face_section=cv2.resize(face_section,(100,100))
out=knn(trainset,face_section.flatten())
pred_name=names[int(out)]
cv2.putText(frame,pred_name,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2,cv2.LINE_AA)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
cv2.imshow("Faces",frame)
key_pressed=cv2.waitKey(1)& 0xFF
if key_pressed== ord('s'):
break
cap.release()
cv2.destroyAllWindows()