Age detection of photos in PHP
1 min readAug 3, 2019
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:
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.