How to detect people in a photo in PHP
2 min readAug 6, 2019
Let’s take a shortcut to detecting humans in photographs. First step — run the following from the Composer command line:
composer require cloudmersive/cloudmersive_imagerecognition_api_client
Now call recognizeDetectPeople, like you see here:
<?php
require_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\RecognizeApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
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->recognizeDetectPeople($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling RecognizeApi->recognizeDetectPeople: ', $e->getMessage(), PHP_EOL;
}
?>
And it’s as simple as that! Let’s look at an example. Take the following image:
Call our method, and we are left with this:
This will even work if the person’s face is hidden, they are out of focus or in the background. Note there is separate functionality in our image processing API that specifically applies to faces and even facial features, so be sure to check out our documentation for more info.