Extract Eyes in Face İmages With Dlib and Face Landmark Points.

OpenCV has haar cascade for eye detection and other detection things with haar cascade. Haar cascade is basically multiple weak classifiers make decision about these parts is eyes or not. İt’s pretty logical and make a lot of work but in this days it’s obsolete.

New Deep Learning base methods has better accuracy in all face parts. So, what is the easiest way do extract eyes with deep learning base approach. You guess, yes Dlib and his Face Landmark points. 68 points is enough for this task.

This Websites photograph is good for understand what is facial landmarks:

These photograph is the most important photo of all article:

Left Eyes is 37 to 40 and 39 to 42.

Right Eyes is 43 to 46 and 44 to 47.

Show me the code:

import dlib detector = dlib.get_frontal_face_detector() detect=detector(photo,1) predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat) shape=predictor(photo,detect[0])

So we have parts now. How to extract it. Easily:

shape.part(number)

For left eye:

x1=shape.part(36).x x2=shape.part(39).x y1=shape.part(37).y y2=shape.part(40).y

and cropped eyes:

lefteye=rgb[y1:y2,x1:x2]

For right eye:

x1=shape.part(42).x x2=shape.part(45).x #43 46 #44 47 y1=shape.part(43).y y2=shape.part(46).y righteye=rgb[y1:y2,x1:x2]

Also Eyes doesn’t have rectangle shape. If we want full eyes we have to some row and columns in cropped eyes for example:

righteye=rgb[y1-10:y2+10,x1-10:x2+10]

10 is not clear. you should use percentage of resolution.

Example with real image:

https://github.com/anilkay/ComputerVisionExamples/blob/master/beatian.ipynb

Thanks for reading!

Originally published at http://anilkaynr.wordpress.com on October 14, 2019.

Computer Engineer,Sociologist, CSE Master Student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store