-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface-id-experiment.py
64 lines (49 loc) · 1.87 KB
/
face-id-experiment.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
import argparse
import glob
import os
import pickle
import cv2
import face_recognition as fr
import imutils
import numpy as np
from dlib import rectangle
def encode_unknown(img):
f = fr.load_image_file(img)
return fr.face_encodings(f)[0]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--trained_faces", required=True, help="path to trained_face.dat")
parser.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(parser.parse_args())
trained = pickle.load(open(args["trained_faces"], "rb"))
trained_name = list(trained.keys())
trained_enc = list(trained.values())
os.chdir("test-indexed")
for file in glob.glob("*.JPG"):
image = cv2.imread(file, 1)
image = imutils.resize(image, 1080)
face_locations = fr.face_locations(image)
unknown_face_encodings = fr.face_encodings(image, face_locations)
face_names = []
for face in unknown_face_encodings:
matches = fr.compare_faces(trained_enc, face, tolerance=0.45)
name = "unknown"
face_distances = fr.face_distance(trained_enc, face)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
# name = trained_name[best_match_index]
name = 'me'
face_names.append(name)
if 'me' in face_names:
face_temp = face_locations[face_names.index('me')]
elif len(face_locations) > 0:
face_temp = face_locations[0]
else:
continue
face = rectangle(face_temp[3], face_temp[0], face_temp[1], face_temp[2])
cv2.rectangle(image, (face.left()-20, face.top()-20), (face.right()+20, face.bottom()+20), (255, 0, 0), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
print("done")
if __name__ == "__main__":
main()