Age detection of photos in PHP

This post demonstrates how to harness deep learning to determine the age of someone in a photo. Begin with a reference to our image recognition API:

"require": {
"cloudmersive/cloudmersive_imagerecognition_api_client": "^1.4",
}

Then all that’s needed is to call faceDetectAge with our image file.

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\FaceApi(


new GuzzleHttp\Client(),
$config
);
$image_file = "/path/to/file"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
$result = $apiInstance->faceDetectAge($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling FaceApi->faceDetectAge: ', $e->getMessage(), PHP_EOL;
}
?>

Here is a sample image:

Image for post
Image for post

And our output for this image:

{
"Successful": true,
"PeopleWithAge": [
{
"FaceLocation": {
"LeftX": 855,
"TopY": 348,
"RightX": 2277,
"BottomY": 2099
},
"AgeClassificationConfidence": 0.9988910555839539,
"AgeClass": "25-32"
}
],
"PeopleIdentified": 1
}

Our return includes the age range of the subject along with a confidence rating between 0 and 1.

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