This code works, with keras_facenet, to find all files in photos/*.jpg matching the face present in me.jpg:
import glob, os, cv2
from keras_facenet import FaceNet
embedder = FaceNet()
detections = embedder.extract("me.jpg", threshold=0.95)
me_emb = detections[0]['embedding']
for f in glob.glob("photos/*.jpg"):
print(f)
img = cv2.imread(f)
detections, crops = embedder.crop(img, threshold=0.95)
if detections == []:
continue
for j, emb in enumerate(embedder.embeddings(images=crops)):
dist = embedder.compute_distance(emb, me_emb)
if dist < 0.7:
print(f"hey, found in {f=} with {dist=}")
How do to do the same with Keras FaceNet, but with a corpus of multiple example photos me1.jpg, me2.jpg, ..., me10.jpg containing different variations of the face to be found, instead of just one example me.jpg?
Of course, in the last loop, we could compare each face to any of the n embeddings of the example images, but this wouldn't scale well if n is large.
Which better option is there?