-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_authentication.py
151 lines (122 loc) · 4.61 KB
/
face_authentication.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# by @codassassin
import cv2
import numpy as np
import face_recognition
import os
import FaceMeshModule as fm
path = 'Auth_face'
images = []
classNames = []
myList = os.listdir(path)
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])
def searchName(name):
with open('encode.csv', 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name in nameList:
return True
else:
return False
def fetchEncoding(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
def enterUser(name, registered):
with open('encode.csv', 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
f.writelines(f'\n{name},{registered}')
def confirmReg(name):
with open('encode.csv', 'r') as f:
myDataList = f.readlines()
flag = 0
for line in myDataList:
entry = line.split(',')
if entry[0] == name:
flag = 1
break
else:
flag = 0
if flag == 1:
return True
else:
return False
def compareEncodings(image):
small_image = cv2.resize(image, (0, 0), None, 0.25, 0.25)
small_image = cv2.cvtColor(small_image, cv2.COLOR_BGR2RGB)
face = face_recognition.face_locations(small_image)
encode = face_recognition.face_encodings(small_image, face)
match = face_recognition.compare_faces(encodingComp, encode[0])
face_dis = face_recognition.face_distance(encodingComp, encode[0])
match_index = np.argmin(face_dis)
if match[match_index]:
print("The user is registered under another name")
return True
else:
return False
encodingComp = fetchEncoding(images)
nameAuth = input('Enter your name: ')
if searchName(nameAuth):
cap = cv2.VideoCapture(0)
detector = fm.FaceMeshDetector()
while True:
success, img = cap.read()
imag, faces = detector.findFaceMesh(img)
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
flag = 1
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
matches = face_recognition.compare_faces(encodingComp, encodeFace)
faceDis = face_recognition.face_distance(encodingComp, encodeFace)
matchIndex = np.argmin(faceDis)
if matches[matchIndex]:
print("Identified as " + nameAuth)
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(img, 'Identity confirmed: ', (50, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
cv2.putText(img, nameAuth, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
flag = 1
else:
cv2.putText(img, 'Identity could not be confirmed! ', (50, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
flag = 0
if flag == 0:
break
cv2.imshow('Webcam', img)
cv2.waitKey(1)
else:
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
cv2.putText(img, "Press C to capture your image", (50, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 1)
cv2.imshow('Webcam', img)
if cv2.waitKey(1) & 0xFF == ord('c'):
break
del cap
if not classNames:
cv2.imwrite('Auth_face/' + nameAuth + '.png', img)
enterUser(nameAuth, True)
else:
if not compareEncodings(img):
cv2.imwrite('Auth_face/' + nameAuth + '.png', img)
enterUser(nameAuth, True)
if confirmReg(nameAuth):
print("User registered successfully !!")
else:
print("User could not be registered !!")