Aug 26, 2021

2 min read

How to Identify Age from an Image in Ruby

Looking for an easy way to detect age from an image without going through the long and laborious process of training an AI with Deep Learning? If so, you’re in the right place; in this quick tutorial, we will demonstrate how you can use an API in Ruby to instantly detect the age of human faces from an image, along with their position and size.

To begin, we will add the Ruby client to the Gemfile:

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

Next, we can call the function with the following code:

# 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/inputfile') # 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}"

This helpful tool will analyze all human faces in the image, regardless of whether they are facing the camera, ensuring you receive a complete representation of the data.