-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recogn.py
65 lines (44 loc) · 2.12 KB
/
face_recogn.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
import face_recognition
import os
import numpy as np
import pickle
import cv2
from config import cfg
class FaceRec():
def __init__(self, filepath='.\Project', embedding_path='.\embeddings.pkl'):
self.faces = []
self.personName = []
for i in os.listdir(filepath):
self.personName.append(os.path.splitext(i)[0])
print('--------------------------------')
print(self.personName)
print('--------------------------------')
def MatchTheFace(self,img, embedding_path='.\embeddings.pkl'):
try:
with open(embedding_path, 'rb') as file:
embeddings = pickle.load(file)
except Exception as e:
print(e)
facesInFrame = face_recognition.face_locations(img)
encodeFacesInFrame = face_recognition.face_encodings(img, facesInFrame)
for encodeFace, faceloc in zip(encodeFacesInFrame, facesInFrame) :
matches = face_recognition.compare_faces(embeddings, encodeFace, tolerance=cfg["tolerance"])
facedis = face_recognition.face_distance(embeddings, encodeFace)
#print(facedis)
matchIndex = np.argmin(facedis)
if matches[matchIndex] :
name = self.personName[matchIndex]
#self.currentUser.append(name)
print(name)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(img,1.3,3)
if len(faces)==1:
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0),5)
#cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
#cv2.rectangle(frame, (x1, y2-25), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(img , name, (x+6, y-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
if name in self.personName:
#print(name)
return True, name
return False, ''