How to detect eyes in a photo in Ruby

Here’s the easy way to use one of our APIs to identify and locate the eyes and other facial features in a photo. First up, add the API to your Gemfile.

gem 'cloudmersive-image-recognition-api-client', '~> 1.3.2'

With that out of the way, all that’s left is to call face_locate_with_landmarks:

# load the gem
require 'cloudmersive-image-recognition-api-client'
# setup authorization
CloudmersiveImageRecognitionApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveImageRecognitionApiClient::FaceApi.newimage_file = File.new("/path/to/file") # File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.begin
#Find faces and face landmarks (eyes, eye brows, nose, mouth) in an image
result = api_instance.face_locate_with_landmarks(image_file)
p result
rescue CloudmersiveImageRecognitionApiClient::ApiError => e
puts "Exception when calling FaceApi->face_locate_with_landmarks: #{e}"
end

Let’s look at an example using the following image.

Image for post
Image for post

Our output for this image:

{
"ErrorDetails": null,
"Successful": true,
"Faces": [
{
"LeftEyebrow": [
{
"X": 1164,
"Y": 458
},
{
"X": 1227,
"Y": 439
},
{
"X": 1295,
"Y": 442
},
{
"X": 1352,
"Y": 470
},
{
"X": 1057,
"Y": 565
}
],
"RightEyebrow": [
null,
null,
null,
null,
null
],
"LeftEye": [
{
"X": 906,
"Y": 582
},
{
"X": 948,
"Y": 576
},
{
"X": 991,
"Y": 597
},
{
"X": 953,
"Y": 609
},
{
"X": 910,
"Y": 617
},
{
"X": 1160,
"Y": 565
}
],
"RightEye": [
{
"X": 1195,
"Y": 529
},
{
"X": 1240,
"Y": 522
},
{
"X": 1285,
"Y": 538
},
{
"X": 1250,
"Y": 558
},
{
"X": 1204,
"Y": 565
},
{
"X": 1014,
"Y": 914
}
],
"BottomAndSidesOfFace": [
{
"X": 810,
"Y": 722
},
{
"X": 829,
"Y": 800
},
{
"X": 857,
"Y": 874
},
{
"X": 897,
"Y": 943
},
{
"X": 952,
"Y": 1002
},
{
"X": 1019,
"Y": 1054
},
{
"X": 1090,
"Y": 1093
},
{
"X": 1168,
"Y": 1098
},
{
"X": 1246,
"Y": 1074
},
{
"X": 1314,
"Y": 1022
},
{
"X": 1373,
"Y": 958
},
{
"X": 1419,
"Y": 882
},
{
"X": 1443,
"Y": 800
},
{
"X": 1452,
"Y": 713
},
{
"X": 1450,
"Y": 623
},
{
"X": 1440,
"Y": 532
},
{
"X": 804,
"Y": 560
}
],
"NoseBridge": [
{
"X": 1064,
"Y": 621
},
{
"X": 1069,
"Y": 676
},
{
"X": 1076,
"Y": 731
},
{
"X": 1034,
"Y": 788
}
],
"NoseBottom": [
{
"X": 1064,
"Y": 792
},
{
"X": 1094,
"Y": 793
},
{
"X": 1126,
"Y": 782
},
{
"X": 1157,
"Y": 770
},
{
"X": 876,
"Y": 608
}
],
"LipsInnerOutline": [
{
"X": 1082,
"Y": 901
},
{
"X": 1112,
"Y": 901
},
{
"X": 1142,
"Y": 893
},
{
"X": 1214,
"Y": 880
},
{
"X": 1144,
"Y": 893
},
{
"X": 1114,
"Y": 901
},
{
"X": 1083,
"Y": 902
}
],
"LipsOuterOutline": [
{
"X": 1042,
"Y": 889
},
{
"X": 1077,
"Y": 868
},
{
"X": 1107,
"Y": 873
},
{
"X": 1136,
"Y": 859
},
{
"X": 1183,
"Y": 868
},
{
"X": 1237,
"Y": 875
},
{
"X": 1193,
"Y": 919
},
{
"X": 1153,
"Y": 944
},
{
"X": 1121,
"Y": 953
},
{
"X": 1089,
"Y": 954
},
{
"X": 1052,
"Y": 944
},
{
"X": 1034,
"Y": 912
}
],
"LeftX": 770,
"TopY": 413,
"RightX": 1412,
"BottomY": 1055
}
],
"FaceCount": 1
}

We are provided with the vertices for each feature, including the eyes, nose, mouth, and eyebrows. This is also compatible with photos containing multiple people.

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