-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerec.py
146 lines (117 loc) · 4.49 KB
/
facerec.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
# following https://www.superdatascience.com/opencv-face-detection/
# import opencv
import cv2
import time
import numpy as np
import requests
# import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import dlib
#convert between BGR (used by opencv?) and RGB (used by matplotlib)
def xywh_to_dlibrect(xywh):
x,y,w,h = xywh
return dlib.rectangle(x,y,x+w,y+h)
def dlibrect_to_xywh(d):
return (d.left(), d.top(), d.width(), d.height())
def convertToRGB(img):
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def extract_face(f_cascade, colored_img, scaleFactor=1.1):
# make copy of given image so it isnt modified
img_copy = colored_img.copy()
# convert to grayscale
gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
# find faces
faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=5, minSize=(100,100))
# do stuff with faces
#for (x,y,w,h) in faces:
# cv2.rectangle(img_copy, (x,y), (x+w,y+h), (0,255,0), 2)
# print(faces);
# assume one face
if (len(faces) == 0):
#print("No face found")
return None
return faces[0]
pose_predictor_68_point = dlib.shape_predictor('data/shape_predictor_68_face_landmarks.dat')
pose_predictor_5_point = dlib.shape_predictor('data/shape_predictor_5_face_landmarks.dat')
face_encoder = dlib.face_recognition_model_v1('data/dlib_face_recognition_resnet_model_v1.dat')
def get_face_landmarks(img, loc):
return pose_predictor_5_point(img,xywh_to_dlibrect(loc))
def get_face_encoding(img, loc):
# img: image
# loc: bounding box for face
face_landmarks = get_face_landmarks(img,loc)
enc10 = np.array(face_encoder.compute_face_descriptor(img, face_landmarks, 10))
#print(np.linalg.norm(enc10-np.array(face_encoder.compute_face_descriptor(img, face_landmarks, 1))))
return enc10
test3 = cv2.imread('lucas.jpg')
test2 = cv2.imread('navid.jpg')
test1 = cv2.imread('jacob.jpg')
test4 = cv2.imread('zucc.jpg')
test5 = cv2.imread('zucc2.jpg')
test6 = cv2.imread('jacob2.jpg')
test7 = cv2.imread('human04.jpg')
names = ["jacob", "navid", "lucas", "mark", 'timothy']
test = [test1, test2, test3, test4, test6]
haar_face_cascade = cv2.CascadeClassifier('dat/haarcascade_frontalface_alt.xml')
lbp_face_cascade = cv2.CascadeClassifier('dat/lbpcascade_frontalface.xml')
ex1 = extract_face(lbp_face_cascade, test1)
enc1 = get_face_encoding(test1,ex1 )
enc2 = get_face_encoding(test2, extract_face(lbp_face_cascade, test2))
enc3 = get_face_encoding(test3, extract_face(lbp_face_cascade, test3))
enc4 = get_face_encoding(test4, extract_face(lbp_face_cascade, test4))
enc5 = get_face_encoding(test5, extract_face(lbp_face_cascade, test5))
enc6 = get_face_encoding(test6, extract_face(lbp_face_cascade, test6))
enc7 = get_face_encoding(test7, extract_face(lbp_face_cascade, test7))
enc = [enc1, enc2, enc3, enc4, enc6]
rect = patches.Rectangle(ex1[0:2],ex1[2],ex1[3],linewidth=1,edgecolor='r',facecolor='none')
fig,ax = plt.subplots(1)
#print('jacob-navid: ',np.linalg.norm(enc1-enc2))
#print('lucas-navid: ',np.linalg.norm(enc2-enc3))
#print('jacob-lucas: ',np.linalg.norm(enc1-enc3))
#print('zucc-tim: ',np.linalg.norm(enc4-enc6))
ax.imshow(test1, cmap='gray')
ax.add_patch(rect)
#plt.show()
cap = cv2.VideoCapture(0)
finalframe = None
finalfacerect = None;
parity = True
facerect = (0,0,0,0)
start = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
start = time.time()
finalframe = frame
finalfacerect = facerect
break
else:
cv2.putText(frame, "Press Q to Take Picture", (30, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255))
facerect = extract_face(lbp_face_cascade, frame)
if facerect is None:
cv2.imshow('vid', frame)
# print(frame)
parity = not parity
continue
x,y,w,h = facerect
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0),2)
cv2.imshow('vid', frame)
parity = not parity
cv2.imshow('final image', finalframe)
finalenc = get_face_encoding(finalframe, finalfacerect)
minNorm = 0.45
minIndex = -1;
for i in range(5):
print(names[i] + '-image: ',np.linalg.norm(enc[i]-finalenc))
if np.linalg.norm(enc[i]-finalenc) < minNorm:
minNorm = np.linalg.norm(enc[i]-finalenc)
minIndex = i
if minIndex == -1: print("No match")
else: print("Match: ", names[minIndex])
url = 'http://35.235.88.148:3000/check'
files = {'file': finalenc}
r = requests.post(url, files = files)
data = r.json()['isAllowed']
print(data)