-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfaceproject.py
108 lines (77 loc) · 2.64 KB
/
faceproject.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from cv2 import cv2
import numpy as np
import os
from database import push_data
# knn
def distance(v1, v2):
return np.sqrt(((v1-v2)**2).sum())
def knn(train, test, k=5):
dist = []
for i in range(train.shape[0]):
ix = train[i, :-1]
iy = train[i, -1]
d = distance(test, ix)
dist.append([d, iy])
dk = sorted(dist, key=lambda x: x[0] > [k])
labels = np.array(dk)[:, -1]
output = np.unique(labels, return_counts=True)
index = np.argmax(output[1])
return output[0][index]
# knn
# init camera
cap = cv2.VideoCapture(0)
# face detection
face_cascade = cv2.CascadeClassifier(
"haar_cascade/haarcascade_frontalface_alt.xml")
skip = 0
dataset_path = './data/'
face_data = []
labels = []
class_id = 0 # labels for the given file
names = {} # mapping btw id - name
name_set = set()
# Data Preparation
for fx in os.listdir(dataset_path): # to load the file in the folder
if fx.endswith(".npy"):
# create a mapping btw clas label and
names[class_id] = fx[:-4]
data_item = np.load(dataset_path+fx)
face_data.append(data_item)
# create labels for class
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))
trainset = np.concatenate((face_dataset, face_labels), axis=1)
# testing
while True:
ret, frame = cap.read()
if(ret == False):
continue
faces = face_cascade.detectMultiScale(frame, 1.3, 5)
for face in faces:
x, y, w, h = face
# get the face roi
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())
# print(out)
# display a name and a rectangle around it
pred_name = names[int(out)]
# print(pred_name)
name_set.add(pred_name)
cv2.putText(frame, pred_name, (x, y-20),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "If Your name shows up Please press s", (x, y-60),
cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 200), 2, cv2.LINE_AA)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow("Faces", frame)
key = cv2.waitKey(1) & 0xFF
if(key == ord("s")):
break
for i in name_set:
push_data(str(i), "P")
cap.release()
cv2.destroyAllWindows()