def encode(detector, shape_predictor, model, image, win=None):
"""Encodes faces from a single image into a 128 dim descriptor.
Args:
detector: dlib face detector object
shape_predictor: dlib shape predictor object
model: dlib convnet model
image: image as numpy array
win: dlib window object for vizualization if VIZ flag == 1
Returns:
list of descriptors (np array) for each face detected in image
"""
# dlib comments:
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
descriptors = []
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = sp(img, d)
# Draw the face landmarks on the screen so we can see what face is currently being processed.
if win is not None:
win.clear_overlay()
win.set_image(img)
win.add_overlay(d)
win.add_overlay(shape)
dlib.hit_enter_to_continue()
# Compute the 128D vector that describes the face in img identified by shape
face_descriptor = facerec.compute_face_descriptor(img, shape)
descriptors.append(np.asarray(list(face_descriptor)))
return descriptors