人脸注册
tom = [...]
jack = [...]
np.savez('faces.npz', tom=tom, jack=jack)
人脸加载
def load_embeddings(path):
arrs = np.load(path)
names, embeddings = [], []
for name, embedding in arrs.items():
names.append(name)
embeddings.append(embedding)
embeddings = np.vstack(embeddings)
return np.array(names), embeddings
人脸比对
def compare(embedding, embeddings):
return np.sqrt(np.sum(((embeddings - embedding) ** 2), axis=1))
人脸识别
def recognize(embedding, embeddings, names):
result = compare(embedding, embeddings)
index = np.argsort(result)[::-1]
score = result[index][0]
if score < 1.1:
return names[index][0], score
else:
return 'unknown', score
应用
names, embeddings = load_embeddings('faces.npz')
while 1:
faces = [...]
for face in faces:
embedding = [...]
name = recognize(embedding, embeddings, names)