Detecting facial landmarks with face_recognition

The landmarks_detection_fr.py script shows you how to both detect and draw facial landmarks using the face_recognition package.

In order to detect landmarks, the face_recognition.face_landmarks() function is called, as follows:

# Detect 68 landmarks:
face_landmarks_list_68 = face_recognition.face_landmarks(rgb)

This function returns a dictionary of facial landmarks (for example, eyes and nose) for each face in the image. For example, if we print the detected landmarks, the output is as follows:

[{'chin': [(113, 251), (111, 283), (115, 315), (122, 346), (136, 376), (154, 402), (177, 425), (203, 442), (231, 447), (260, 442), (285, 426), (306, 403), (323, 377), (334, 347), (340, 315), (343, 282), (343, 251)], 'left_eyebrow': [(123, 223), (140, 211), (163, 208), (185, 211), (206, 220)], 'right_eyebrow': [(240, 221), (263, 212), (288, 209), (312, 211), (332, 223)], 'nose_bridge': [(225, 249), (225, 272), (225, 295), (226, 319)], 'nose_tip': [(201, 337), (213, 340), (226, 343), (239, 339), (252, 336)], 'left_eye': [(144, 248), (158, 239), (175, 240), (188, 254), (173, 255), (156, 254)], 'right_eye': [(262, 254), (276, 240), (293, 239), (308, 248), (295, 254), (278, 255)], 'top_lip': [(185, 377), (200, 370), (216, 364), (226, 367), (238, 364), (255, 370), (274, 377), (267, 378), (238, 378), (227, 380), (215, 379), (192, 378)], 'bottom_lip': [(274, 377), (257, 391), (240, 399), (228, 400), (215, 398), (200, 391), (185, 377), (192, 378), (215, 381), (227, 382), (239, 380), (267, 378)]}]

The final step is to draw the detected landmarks:

# Draw all detected landmarks:
for face_landmarks in face_landmarks_list_68:
for facial_feature in face_landmarks.keys():
for p in face_landmarks[facial_feature]:
cv2.circle(image_68, p, 2, (0, 255, 0), -1)

It should be noted that the signature of the face_recognition.face_landmarks() method is as follows:

face_landmarks(face_image, face_locations=None, model="large")

Therefore, by default, the 68 feature points are detected. If model="small", only 5 feature points will be detected:

# Detect 5 landmarks:
face_landmarks_list_5 = face_recognition.face_landmarks(rgb, None, "small")

If we print face_landmarks_list_5, we get the following output:

[{'nose_tip': [(227, 343)], 'left_eye': [(145, 248), (191, 253)], 'right_eye': [(307, 248), (262, 252)]}]

In this case, the dictionary only contains facial feature locations for both eyes and the tip of the nose. 

The output of the landmarks_detection_fr.py script can be seen in the following screenshot:

In the screenshot above, you can see the result of drawing both the detected 68 and 5 facial landmarks using face_recognition package.