-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_face_encodings.py
70 lines (57 loc) · 1.51 KB
/
create_face_encodings.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
import face_recognition as fr
import os
import pickle
import sys
import cv2, dlib, math
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
base = r".\dataset"
names = os.listdir(base) # names of people
person_face_encodings = dict()
for name in names:
person_face_encodings[name] = []
for file_name in os.listdir(base+'/'+name):
img = fr.load_image_file(base+'/'+name+'/'+file_name)
encodings = fr.face_encodings(img, model='large')
if len(encodings):
person_face_encodings[name].append(encodings[0])
try:
f = open("./important files/face_encodings.pickle", 'wb')
pickle.dump(person_face_encodings, f)
f.close()
except:
print("Something went wrong!")
os._exit(1)
try:
f = open("./important files/face_encodings.pickle", 'rb')
person_face_encodings = pickle.load(f)
f.close()
except:
print("Something went wrong!")
os._exit(1)
target = dict()
a = 0
for p in list(person_face_encodings.keys()):
target[p] = a
a += 1
reverse_target = dict([(v,k) for k,v in target.items()])
target
X = []
y = []
for k,v in person_face_encodings.items():
for i in v:
X.append(i)
y.append(target[k])
X = np.array(X)
y = np.array(y)
knn = KNeighborsClassifier(n_neighbors=3, n_jobs=-1)
knn.fit(X,y)
try:
f = open("./important files/facenet_knn.pickle", 'wb')
pickle.dump(knn, f)
f.close()
except:
print("Something went wrong!")
os._exit(1)
print("model saved")