How to detect people in a photo in PHP

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:

Image for post
Image for post

Call our method, and we are left with this:

Image for post
Image for post

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.

Written by

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

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