How to compare and match faces in PHP

Let’s take a look at how to quickly compare and match faces using Cloudmersive’s Image Recognition API.

Add this reference to your library:

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

Then call faceCompare with our input image (may include multiple faces) and match image (should only have one face):

<?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
);
$input_image = "/path/to/file"; // \SplFileObject | Image file to perform the operation on; this image can contain one or more faces which will be matched against face provided in the second image. Common file formats such as PNG, JPEG are supported.
$match_face = "/path/to/file"; // \SplFileObject | Image of a single face to compare and match against.
try {
$result = $apiInstance->faceCompare($input_image, $match_face);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling FaceApi->faceCompare: ', $e->getMessage(), PHP_EOL;
}
?>

Here is an example result:

{
"ErrorDetails": null,
"Successful": true,
"Faces": [
{
"LeftX": -86,
"TopY": 56,
"RightX": 627,
"BottomY": 698,
"HighConfidenceMatch": true,
"MatchScore": 0.8687548935413361
}
],
"FaceCount": 1
}

We are returned the location of the best match, its match score between 0 and 1, and whether it is a high confidence match (0.8 score or higher). This data can then be used for face login, photo matching, or a variety of other applications.

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