PHP code example of subsan / microsoft-cognitive-face-php-api-client

1. Go to this page and download the library: Download subsan/microsoft-cognitive-face-php-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

subsan / microsoft-cognitive-face-php-api-client example snippets






// include your composer dependencies
tiveFace\Client('YOUR_APP_KEY', 'YOUR_REGION');
$faces  = $client->face()->detectFacesFromImg('URL_IMAGE_WITH_FACES');

var_dump($faces);



$client = new \Subsan\MicrosoftCognitiveFace\Client('YOUR_APP_KEY', 'YOUR_REGION');

// create new person group
$newPersonGroupId = uniqid();
$client->personGroup($newPersonGroupId)->create(
    new \Subsan\MicrosoftCognitiveFace\Entity\PersonGroup(
        null, 
        'Group Name',
        'Additional info'
    )
);

// get faces from image
$url   = 'URL_IMAGE_WITH_FACES';
$faces = $client->face()->detectFacesFromImg($url);

$userNumber = 1;
foreach ($faces as $face) {
    $personFaceRectangle = (new \Subsan\MicrosoftCognitiveFace\Entity\FaceRectangle())->import($face->faceRectangle);

    // create person
    $person = $client->personGroup($newPersonGroupId)->person()->create(
        new \Subsan\MicrosoftCognitiveFace\Entity\Person(
            null, 
            'User '.$userNumber
        )
    );

    // add image to person
    $client->personGroup($newPersonGroupId)->person($person->getPersonId())->addFace($url,'test',$personFaceRectangle);

    $userNumber++;
}



$client = new \Subsan\MicrosoftCognitiveFace\Client('YOUR_APP_KEY', 'YOUR_REGION');

// in previous example $newPersonGroupId
$personGroupId = 'ID_OF_CREATED_PERSON_GROUP';

// train group
$client->personGroup()->train($personGroupId);

// get train status
var_dump($client->personGroup()->getTrainStatus($personGroupId));



$client = new \Subsan\MicrosoftCognitiveFace\Client('YOUR_APP_KEY', 'YOUR_REGION');

// in previous example $newPersonGroupId
$personGroupId = 'ID_OF_CREATED_PERSON_GROUP';

// get faces from image
$url   = 'URL_IMAGE_WITH_FACES';
$faces = $client->face()->detectFacesFromImg($url);

// prepare array of faces ids
$faceIds = array();
foreach ($faces as $face) {
    $faceIds[] = $face->faceId;
}

// identify all faces
print_r($client->face()->identify($faceIds, $personGroupId));