How to compare and match faces in PHP

Cloudmersive
1 min readAug 2, 2019

--

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet