How to Detect, Find Faces in an Image in PHP
Before processing images with human subjects in our applications, it’s important to know where those subjects are located — otherwise we risk inadvertently cropping or editing people out of our field of view. Thankfully, our free-to-use Face Location API will automatically locate the positions of all human faces within an image, returning their locations with specific pixel coordinates to guide downstream editing operations.
To use this API for free as described above, first register a free-tier account on our website — this will provide you with a free-tier API key(with a limit of 800 API calls per month) for authentication.
Next, install the PHP client using Composer by executing this command from the command line:
composer require cloudmersive/cloudmersive_imagerecognition_api_client
Then use the example code below to structure & complete your request:
<?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\FaceApi(
// 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.txt"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
$result = $apiInstance->faceLocate($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling FaceApi->faceLocate: ', $e->getMessage(), PHP_EOL;
}
?>
And that’s all there is to it!