How to detect eyes in a photo in Javascript

Mapping out facial landmarks (features) is the first step toward making photo adjustments and running filters. Today is not going to be about how to Deep Learn an AI to perform this task. In fact, we have already done that for you. All you need to do is take advantage of this using a simple API function call. Let’s look at how to do this.

Import the API client via this script tag:

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-image-client.js"></script>

Call our function:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveImageApiClient.FaceApi();var imageFile = "/path/to/file"; // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.faceLocateWithLandmarks(imageFile, callback);

Done! Here’s the details that will be provided for you by the API:

{
"Successful": true,
"Faces": [
{
"LeftX": 0,
"TopY": 0,
"RightX": 0,
"BottomY": 0,
"LeftEyebrow": [
{
"X": 0,
"Y": 0
}
],
"RightEyebrow": [
{
"X": 0,
"Y": 0
}
],
"LeftEye": [
{
"X": 0,
"Y": 0
}
],
"RightEye": [
{
"X": 0,
"Y": 0
}
],
"BottomAndSidesOfFace": [
{
"X": 0,
"Y": 0
}
],
"NoseBridge": [
{
"X": 0,
"Y": 0
}
],
"NoseBottom": [
{
"X": 0,
"Y": 0
}
],
"LipsInnerOutline": [
{
"X": 0,
"Y": 0
}
],
"LipsOuterOutline": [
{
"X": 0,
"Y": 0
}
]
}
],
"FaceCount": 0,
"ErrorDetails": "string"
}
Image for post
Image for post

Written by

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