Age detection of photos in Python

Cloudmersive
1 min readAug 30, 2019

--

Let’s keep this very simple. First, pip install the API client.

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.ImageRecognition.git

Next, call face_detect_age and provide it with an input image.

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.FaceApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/file' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Detect the age of people in an image
api_response = api_instance.face_detect_age(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling FaceApi->face_detect_age: %s\n" % e)

Pretty easy, huh? Let’s look at an example:

Our return:

{
"Successful": true,
"PeopleWithAge": [
{
"FaceLocation": {
"LeftX": 200,
"TopY": 107,
"RightX": 403,
"BottomY": 335
},
"AgeClassificationConfidence": 0.9,
"AgeClass": "25-32",
"Age": 25.75040626525879
}
],
"PeopleIdentified": 1
}

You will notice that the function supports multiple people, provides their location within the photo, age range, a best guess at their exact age, and confidence rating of the assessment.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet