PHP Hello world


This Tutorial Code demonstrates a standard workflow of developing a Face++ App: recognize a new face image from a set of candidate images (Three images in this App).

First, we detect the faces from three candidate images. Each Image contains a Face. We build three new Persons for each Face and train models for each person respectively. Then, we build a new Group from these three Faces. Finally, given an input image, we recognize the Face of this image within the constructed Group.

This demo code is written by PHP, code can be downloaded here (you should complete the function getTrainingUrl and getPhotoUrl).

Create New Persons

We need to detect faces from each Image. Then we need to use the API of Person Management to create new Persons.

// the list of person_name to train and identify for
$person_names = array("1", "2", "3");
// store the face_ids obtained by detection/detect API
$face_ids = array();
// register new people, detect faces
foreach ($person_names as $person_name)
    detect($api, $person_name, $face_ids);

 *	create new person, detect faces from person's image_url
function detect(&$api, $person_name, &$face_ids) 
	// obtain photo_url to train
    $url = getTrainingUrl($person_name);

    // detect faces in this photo
    $result = $api->face_detect($url);
    // skip errors
    if (empty($result->face))
        return false;
    // skip photo with multiple faces (we are not sure which face to train)
   	if (count($result->face) > 1)
   		return false;

   	// obtain the face_id
   	$face_id = $result->face[0]->face_id;
   	$face_ids[] = $face_id;
    // delete the person if exists
   	// create a new person for this face
   	// add face into new person
   	$api->person_add_face($face_id, $person_name);

Create a New Group

Create a new Group and add all the 3 Persons to this Group.

// the name of group for testing
$group = "sample_group";
// generate a new group, add people into group
create_group($api, $group, $person_names);

 *	generate a new group with group_name, add all people into group
function create_group(&$api, $group_name, $person_names) 
	// delete the group if exists
	// create new group
   	// add new person into the group
	foreach ($person_names as $person_name)
	   	$api->group_add_person($person_name, $group_name);

Train Recognition Models

For the group, call the /train/identify to train his/her identification model.
(Notice: /train/identify is an asynchronized API. Thus, we need to wait for the completion of the model training to continue.)

// generate training model for group
train($api, $group);

 *	train identification model for group
function train(&$api, $group_name)
   	// train model
   	$session = $api->train_identify($group_name);
    if (empty($session->session_id))
        // something went wrong, skip
        return false;
    $session_id = $session->session_id;
    // wait until training process done
    while ($session=$api->info_get_session($session_id)) 

        if (!empty($session->status)) {
            if ($session->status != "INQUEUE")
	// done
    return true;

Recognize New Face

We are now ready to recognize a new face within the constructed group:

// finally, identify people in the group
identify($api, $person_names[0], $group);

 *	identify a person in group
function identify(&$api, $person_name, $group_name)
	// obtain photo_url to identify
	$url = getPhotoUrl($person_name);

	// recoginzation
	$result = $api->recognition_identify($url, $group_name);

	// skip errors
	if (empty($result->face))
		return false;
	// skip photo with multiple faces
	if (count($result->face) > 1)
		return false;
	$face = $result->face[0];
	// skip if no person returned
	if (count($face->candidate) < 1) 		return false; 		 	// print result 	foreach ($face->candidate as $candidate) 
		echo "$candidate->person_name was found in $group_name with ".
        "confidence $candidate->confidence\n";