Extract Face Landmarks
A function extract facial landmarks.
from mlxtend.image import extract_face_landmarks
Overview
The extract_face_landmarks
function detects the faces in a given image, and then it will return the face landmark points (also known as face shape) for the first found face in the image based on dlib's face landmark detection code (http://dlib.net/face_landmark_detection_ex.cpp.html):
The face detector we use is made using the classic Histogram of Oriented Gradients (HOG) feature combined with a linear classifier, an image pyramid, and sliding window detection scheme. The pose estimator was created by using dlib's implementation of the paper: One Millisecond Face Alignment with an Ensemble of Regression Trees by Vahid Kazemi and Josephine Sullivan, CVPR 2014 and was trained on the iBUG 300-W face landmark dataset (see https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):
C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic. 300 faces In-the-wild challenge: Database and results. Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016. You can get the trained model file from: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2. Note that the license for the iBUG 300-W dataset excludes commercial use. So you should contact Imperial College London to find out if it's OK for you to use this model file in a commercial product.
References
- Kazemi, Vahid, and Josephine Sullivan. "One millisecond face alignment with an ensemble of regression trees." Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2014.
Example 1
import imageio
import matplotlib.pyplot as plt
from mlxtend.image import extract_face_landmarks
img = imageio.imread('lena.png')
landmarks = extract_face_landmarks(img)
print(landmarks.shape)
print('\n\nFirst 10 landmarks:\n', landmarks[:10])
(68, 2)
First 10 landmarks:
[[206 266]
[204 290]
[205 314]
[209 337]
[220 357]
[236 374]
[253 387]
[273 397]
[290 398]
[304 391]]
Visualization of the landmarks:
fig = plt.figure(figsize=(15, 5))
ax = fig.add_subplot(1, 3, 1)
ax.imshow(img)
ax = fig.add_subplot(1, 3, 2)
ax.scatter(landmarks[:, 0], -landmarks[:, 1], alpha=0.8)
ax = fig.add_subplot(1, 3, 3)
img2 = img.copy()
for p in landmarks:
img2[p[1]-3:p[1]+3,p[0]-3:p[0]+3,:] = (255, 255, 255)
ax.imshow(img2)
plt.show()
API
extract_face_landmarks(img, return_dtype=
Function to extract face landmarks.
Note that this function requires an installation of the Python version of the library "dlib": http://dlib.net
Parameters
-
img
: array, shape = [h, w, ?]numpy array of a face image. Supported shapes are - 3D tensors with 1 or more color channels, for example, RGB: [h, w, 3] - 2D tensors without color channel, for example, Grayscale: [h, w] return_dtype: the return data-type of the array, default: np.int32.
Returns
-
landmarks
: numpy.ndarray, shape = [68, 2]A numpy array, where each row contains a landmark/point x-y coordinates.
Examples
For usage examples, please see http://rasbt.github.io/mlxtend/user_guide/sources/image/extract_face_landmarks.ipynb