How to detect people in a photo in Ruby

Cloudmersive
2 min readAug 20, 2019

--

Today’s post is just a quick lesson in how to use one of our APIs to locate people in photographs. To begin we simply add the client to our Gemfile:

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

And then all that’s left is calling recognize_detect_people:

# 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::RecognizeApi.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
#Detect people, including locations, in an image
result = api_instance.recognize_detect_people(image_file)
p result
rescue CloudmersiveImageRecognitionApiClient::ApiError => e
puts "Exception when calling RecognizeApi->recognize_detect_people: #{e}"
end

And that’s really all that there is to it! Let’s look at an example result using the following photo:

Our return will look like this:

{
"Successful": true,
"Objects": [
{
"ObjectClassName": "person",
"Height": 1375,
"Width": 1186,
"Score": 0.9021745920181274,
"X": 45,
"Y": 10
},
{
"ObjectClassName": "person",
"Height": 1498,
"Width": 2127,
"Score": 0.6978331804275513,
"X": 1200,
"Y": 43
}
],
"ObjectCount": 2
}

As you can see we are given the number of people in the photo, their coordinates, dimensions, and confidence scores for each.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet