Age detection of photos in Ruby

Determining someone’s age automatically from a photo is much easier than you might think. Let’s do it in two steps.

First step, add this to your Gemfile:

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

Second step, call face_detect_age:

# 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'
api_instance = CloudmersiveImageRecognitionApiClient::FaceApi.newimage_file ="/path/to/file.txt") # File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.begin
#Detect the age of people in an image
result = api_instance.face_detect_age(image_file)
p result
rescue CloudmersiveImageRecognitionApiClient::ApiError => e
puts "Exception when calling FaceApi->face_detect_age: #{e}"

And that’s all you have to do! Let’s look at an example. Here’s an image to try:

Image for post
Image for post

And our response from the API:

"Successful": true,
"PeopleWithAge": [
"FaceLocation": {
"LeftX": 139,
"TopY": 37,
"RightX": 325,
"BottomY": 273
"AgeClassificationConfidence": 0.9895910620689392,
"AgeClass": "25-32"
"PeopleIdentified": 1

We are given the location of each face (in this case one, but it works with multiple), the approximate age range, and a confidence rating of this age range between 0 and 1. In this case it is almost guaranteed that the photo’s subject is between 25 and 32.

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