-
Notifications
You must be signed in to change notification settings - Fork 0
/
facial recognition.py
90 lines (58 loc) · 2.29 KB
/
facial 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
import cv2
from os.path import isfile,join
from os import listdir
import numpy as np
sample_data='C:/Users/Vijay/Desktop/Image/'
files=[x for x in listdir(sample_data) if isfile(join(sample_data,x))]
TrainData,labels=[],[]
for x,y in enumerate(files):
imgDir=sample_data+files[x]
images=cv2.imread(imgDir,cv2.IMREAD_GRAYSCALE)
TrainData.append(np.asarray(images,dtype=np.uint8))
labels.append(x)
labels=np.asarray(labels,dtype=np.int32)
model=cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(TrainData),np.asarray(labels))
print("Model Training Complete!!")
#<--training section over
#recognition part -->
#use your own file location here
face_classifier=cv2.CascadeClassifier('C:/Users/Vijay/AppData/Roaming/Python/Python37/site-packages/~v2/data/haarcascade_frontalface_default.xml')
print("Press enter or q to exit")
def detector(img,size=0.5):
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=face_classifier.detectMultiScale(gray,1.1,4)
if faces is():
return img,[]
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y),(x+w,y+h), (0,255,255),2)
region=img[y:y+h,x:x+w]
region=cv2.resize(region,(250,250))
return img,region
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
while True:
val,frame=cap.read()
image,face=detector(frame)
#error handling
try:
face=cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
result=model.predict(face)
if(result[1]<500):
conf=int(100*(1-(result[1])/300))
txt=str(conf)+'% Accuracy of User'
cv2.putText(image, txt,(150,55),cv2.FONT_HERSHEY_DUPLEX, 1,(0,200,240),2)
cv2.imshow("face", image)
if conf>80:
cv2.putText(image,"User Identified :)",(200,455),cv2.FONT_ITALIC, 1,(0,255,0),3)
cv2.imshow("face", image)
else:
cv2.putText(image,"User Unidentified !!",(200,455),cv2.FONT_ITALIC, 1,(0,0,255),3)
cv2.imshow("face", image)
except:
cv2.putText(image,"Face Not Found !!",(200,455),cv2.FONT_HERSHEY_DUPLEX, 1,(255,0,0),2)
cv2.imshow("face", image)
pass
if cv2.waitKey(1)==13 or cv2.waitKey(1)==113:
break
cap.release()
cv2.destroyAllWindows()