How to detect eyes in a photo in Python

Here is a quick tutorial on how to easily and automatically detect the facial features of a photograph. Let’s get to it.

First step: install the API Client.

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.ImageRecognition.git

Next, call face_locate_with_landmarks with the desired photograph.

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.FaceApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/file' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Find faces and face landmarks (eyes, eye brows, nose, mouth) in an image
api_response = api_instance.face_locate_with_landmarks(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling FaceApi->face_locate_with_landmarks: %s\n" % e)

That’s it! All done. Let’s look at an example photo:

Image for post

And here are the results, including the eyes, eyebrows, lips, nose, and more:

{
"ErrorDetails": null,
"Successful": true,
"Faces": [
{
"LeftEyebrow": [
{
"X": 801,
"Y": 666
},
{
"X": 828,
"Y": 661
},
{
"X": 852,
"Y": 664
},
{
"X": 860,
"Y": 684
},
{
"X": 728,
"Y": 698
}
],
"RightEyebrow": [
null,
null,
null,
null,
null
],
"LeftEye": [
{
"X": 610,
"Y": 642
},
{
"X": 639,
"Y": 653
},
{
"X": 652,
"Y": 682
},
{
"X": 626,
"Y": 681
},
{
"X": 598,
"Y": 670
},
{
"X": 752,
"Y": 712
}
],
"RightEye": [
{
"X": 776,
"Y": 696
},
{
"X": 802,
"Y": 702
},
{
"X": 814,
"Y": 720
},
{
"X": 798,
"Y": 730
},
{
"X": 774,
"Y": 724
},
{
"X": 574,
"Y": 879
}
],
"BottomAndSidesOfFace": [
{
"X": 406,
"Y": 637
},
{
"X": 396,
"Y": 705
},
{
"X": 395,
"Y": 772
},
{
"X": 414,
"Y": 834
},
{
"X": 449,
"Y": 890
},
{
"X": 490,
"Y": 937
},
{
"X": 535,
"Y": 978
},
{
"X": 580,
"Y": 996
},
{
"X": 619,
"Y": 994
},
{
"X": 652,
"Y": 965
},
{
"X": 688,
"Y": 935
},
{
"X": 723,
"Y": 902
},
{
"X": 756,
"Y": 867
},
{
"X": 785,
"Y": 825
},
{
"X": 808,
"Y": 782
},
{
"X": 823,
"Y": 739
},
{
"X": 538,
"Y": 581
}
],
"NoseBridge": [
{
"X": 723,
"Y": 740
},
{
"X": 719,
"Y": 783
},
{
"X": 716,
"Y": 826
},
{
"X": 651,
"Y": 828
}
],
"NoseBottom": [
{
"X": 668,
"Y": 840
},
{
"X": 684,
"Y": 852
},
{
"X": 702,
"Y": 852
},
{
"X": 716,
"Y": 845
},
{
"X": 581,
"Y": 646
}
],
"LipsInnerOutline": [
{
"X": 642,
"Y": 897
},
{
"X": 656,
"Y": 902
},
{
"X": 669,
"Y": 903
},
{
"X": 672,
"Y": 908
},
{
"X": 666,
"Y": 902
},
{
"X": 652,
"Y": 903
},
{
"X": 638,
"Y": 899
}
],
"LipsOuterOutline": [
{
"X": 614,
"Y": 877
},
{
"X": 650,
"Y": 879
},
{
"X": 663,
"Y": 890
},
{
"X": 677,
"Y": 887
},
{
"X": 684,
"Y": 896
},
{
"X": 682,
"Y": 907
},
{
"X": 668,
"Y": 925
},
{
"X": 657,
"Y": 933
},
{
"X": 641,
"Y": 933
},
{
"X": 624,
"Y": 928
},
{
"X": 597,
"Y": 912
},
{
"X": 586,
"Y": 883
}
],
"LeftX": 435,
"TopY": 534,
"RightX": 881,
"BottomY": 980
}
],
"FaceCount": 1
}

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

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