-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.py
More file actions
96 lines (64 loc) · 1.92 KB
/
detection.py
File metadata and controls
96 lines (64 loc) · 1.92 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import numpy as np
import os
##################### KNN CODE ########################
def dist(x1,x2):
return np.sqrt(np.sum((x2-x1)**2))
def knn(x_train,y_train,q_point,k=11):
dist_val = []
m = x_train.shape[0]
for ix in range(m):
tempdist = dist(q_point,x_train[ix,:])
dist_val.append((tempdist,y_train[ix]))
dist_val = sorted(dist_val)
dist_val = dist_val[:k]
# print(dist_val)
y = np.array(dist_val)
# print(y.shape)
te = np.unique(y[:,1],return_counts=True)
# print(te)
idx = te[1].argmax()
# print(idx)
prediction = te[0][idx]
return prediction
#####################################################
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
cap = cv2.VideoCapture(0)
skip = 0
face_data = []
dataset_path = './data/'
labels = []
class_id = 0
name = {}
for fx in os.listdir(dataset_path):
if(fx.endswith('.npy')):
data_item = np.load(dataset_path+fx)
face_data.append(data_item)
target = class_id* np.ones((data_item.shape[0]))
name[class_id] = fx[:-4]
class_id+=1
labels.append(target)
face_dataset = np.concatenate(face_data,axis=0)
face_labels = np.concatenate(labels,axis=0)
print(face_dataset.shape)
print(face_labels.shape)
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
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(face_dataset,face_labels,face_section.flatten())
predicted_name = name[out]
cv2.putText(frame,predicted_name,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(255,30,50),2,cv2.LINE_AA)
cv2.rectangle(frame,(x,y),(x+w,y+h),(30,230,200),2)
cv2.imshow('Faces',frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()