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

PHP client for Face module in Microsoft Cognitive Services

v1.0.1 2018-08-31 14:05 UTC

This package is auto-updated.

Last update: 2024-06-15 16:46:09 UTC


README

Microsoft Cognitive Face Service APIs Client Library for PHP

The cloud-based Face API provides developers with access to advanced face algorithms. Microsoft Face algorithms enable face attribute detection and face recognition

Requirements

Installation

You can use Composer or simply Download the Release

Composer

The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

composer require subsan/microsoft-cognitive-face-php-api-client

Finally, be sure to include the autoloader:

require_once '/path/to/your-project/vendor/autoload.php';

Download the Release

If you abhor using composer, you can download the package in its entirety. The Releases page lists all stable versions. Download any file with the name microsoft-cognitive-face-php-api-client-[RELEASE_NAME].zip for a package including this library and its dependencies.

Uncompress the zip file you download, and include the autoloader in your project:

require_once '/path/to/microsoft-cognitive-face-php-api-client/vendor/autoload.php';

Examples

Basic Example

// include your composer dependencies
require_once 'vendor/autoload.php';

$client = new \Subsan\MicrosoftCognitiveFace\Client('YOUR_APP_KEY', 'YOUR_REGION');
$faces  = $client->face()->detectFacesFromImg('URL_IMAGE_WITH_FACES');

var_dump($faces);

Create person group and add new persons for all faces in image

require_once 'vendor/autoload.php';

$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++;
}

Training person group

require_once 'vendor/autoload.php';

$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));

Identity all faces in image

require_once 'vendor/autoload.php';

$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));