test.py
1.84 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
import dlib, cv2
import numpy as np
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')
def read_img(img_path):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def encode_face(img):
dets = detector(img, 1)
if len(dets) == 0:
return np.empty(0)
for k, d in enumerate(dets):
# print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom()))
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
return np.array(face_descriptor)
# main
img1_path = './img/matrix5.jpg' # iu
# img2_path = '/Users/visualcamp/Pictures/00502318_20180518.JPG' # suz
# img2_path = '/Users/visualcamp/Pictures/660190_v9_ba.jpg' # suz
# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/00002/frames/00000.jpg'
# img1_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/03523/frames/02190.jpg'
# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/03523/frames/00000.jpg'
# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/02534/frames/00005.jpg'
img1_path = './img/matrix5.jpg' # me
img1 = read_img(img1_path)
img1_encoded = encode_face(img1)
# img2 = read_img(img2_path)
cap = cv2.VideoCapture(0)
if not cap.isOpened():
exit()
while True:
ret, img2 = cap.read()
if not ret:
break
img2 = cv2.resize(img2, (640, img2.shape[0] * 640 // img2.shape[1]))
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img2_encoded = encode_face(img2)
if len(img2_encoded) == 0:
continue
dist = np.linalg.norm(img1_encoded - img2_encoded, axis=0)
print('%s, Distance: %s' % (dist < 0.6, dist))