How to do it...

Run the following steps to implement face recognition with the pre-trained FaceNet model you downloaded:

  1. Load the pre-trained model using the following line of code:
model = load_model('models/facenet_keras.h5')
  1. Define the following function to extract a single face from a given image. First, use the face detector module from the mtcnn library to detect the first face in the image (extract the bounding box corresponding to it) and then extract the face, resize it to the required size, and return it:
def extract_face(image_file, required_size=(160, 160)):    image = imread(image_file)    image = gray2rgb(image) if len(image.shape) < 3 else \            image[...,:3]    detector = MTCNN()    results = detector.detect_faces(image) x1, y1, width, ...

Get Python Image Processing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.