# Extracting face features from selfies faster

My code takes two .jpg files (two selfies) and extracts a vector of 128 features from each selfie. After this extraction we take these two vectors and get their cosine distance.

I timed the script and it takes 1.6s to load the models and 2.2s to extract the 128 features from the selfies. The time to load the models doesn't annoy me, but of course it would be nice to know if there's a way to load them faster. What I really want to do is improve the extraction of the features and get the results faster.

I tried to get the features from both images at the same time by running the function face_vector with the threading library, but apparently it's not possible to multithread dlib functions. I'm not really sure why it's this way, but it's something related with GIL. It's new to me.

I want to know if it's possible to achieve a faster execution time making code changes or if maybe I should seek better hardware (should I try to run this in the cloud, with a better PC?)

The images I'm using in this code have 80kb in size and dimension 774x1032.

My PC (pretty old): i5-3570K @ 3.4GHz, 8GB RAM.

My code:

from scipy import spatial
import numpy as np
import dlib
import cv2

return img[...,::-1]

def face_vector(face_image):
face_locations = face_detector(face_image, 1)
pose_predictor = pose_predictor_68_point
raw_landmarks = [pose_predictor(face_image, face_location) for face_location in face_locations]
return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, 1)) for raw_landmark_set in raw_landmarks]