Member-only story
OpenCV Series — 8 — Virtual Makeup — Augment Sunglasses on eyes

In this post, we are going to see how we can use Dlib’s face detection landmarks to augment the sunglasses to a person’s image. In my previous Dlib posts i had explained where to download the Dlib pre-trained 68 point face prediction model and the code to detect the landmark points on a facial image.
Now, we will go through the steps involved in identifying the right points in a face image to place the sunglasses on the eyes.
Step: 1:
Detect the landmark points in an image

Step: 2:
Detect the distance between points.

Identify the distance between Points[0] and Points[16] to get the length of the face and round it to the nearest hundred value.
import math
dist = points[16][0] - points[0][0]# Round the distance between Point[0] and Point[16] landmark
# on the x-axis to the nearest 100 values
# (i.e a distance 278 will be rounded to 300)
dist = int(math.ceil(dist / 100.0)) * 100
Step : 3:
Resize the sunglasses image based on the distance identified so it fits right between Points[0] and Points[16]
- read the sunglasses image file with alpha channel
glasses = cv2.imread("glasses1.png",-1)

dw = dist
dh = int(dw * 0.50)
glassesResize = cv2.resize(glasses,(dw,dh))
Step: 4:
Extract the RGB channels and the alpha channel to separate variables
glassesOriginal = glassesResize[:,:,0:3]glassesOriginal = cv2.cvtColor(glassesOriginal,cv2.COLOR_BGR2RGB)maskGlasses = glassesResize[:,:,3]