Facial landmarks are a key tool in projects such as face recognition, face alignment, drowsiness detection, and even as a foundation for face swapping.
Below, we'll be utilising a 68 point facial landmark detector to plot the points onto Dwayne Johnson's face.
The only packages required for my implementation below are OpenCV and dlib (pip install dlib).
Dlib is a toolkit containing machine learning algorithms and tools for creating complex software. We specifically need it for it's frontal face detection functionality.
Installing dlib can sometimes be a little tricky, due to it's dependencies (at the very least you'll need to install cmake (pip install cmake) prior to dlib). If you have any issues, a good place to look is here.
The other key thing you'll need, is to download the shape_predictor_68_face_landmarks.dat and store it in your local directory. You can grab a copy of the file here.
Let's run through the code:
First, we'll install the packages and then set up the detector and predictor.
Next, we'll import our image, then convert it to greyscale
When we apply our detector to our image, we get a list of the faces that it detected (in our case there is only one) and the coordinates of the rectangle that surrounds each face.
Below, for each face in the image (again, we only have one but you could definitely have more than one) - we'll loop through and use our predictor to isolate the coordinates of the 68 landmarks.
As these are initially stored in a dlib object, we unpack them into a list.
We then loop through this list and plot each point onto the image, as well as the number. You'll notice that the numbers can be used to divide the face up into; jawline, eyebrows, nose, and mouth. Knowing these numbers mean you can isolate these separately if needed.
Finally, we display our image.
There you are, a pretty simple process, but the foundation of many cool computer vision techniques so valuable to know!
Andrew Jones has over 10 years experience in Analytics, Data Science, and Machine Learning within the Digital, Retail, Telecoms and Travel industries - most recently at Amazon, & Sony PlayStation