The First Face++ PHP Program

Run the Program

This instruction will show how to detect one new face among three given face images. Click here to download the Face++ PHP demo. Unzip the file and you'll see two files FacePPClientDemo.php and HelloWorld.php. Please
  1. Modify API Key and API Secret to make sure the program runs correctly. (API Key and API Secret can be obtained in your application management system)
  2. It's optional to modify get TrainingUrl and get PhotoUrl to redirect the program input to your desired images.
  3. Under command line, run "php HelloWorld.php" to view the results.
The program does the following things in order,
  1. Detect faces in three input images.
  2. Use the three faces to create three persons.
  3. Add three people to a group, which will be used as the candidate library for future detection.
  4. Input a new image and find the most similar face in the group.

Reading the Program

Create a Person

The program first detects input images to get the face locations and other attributes. Then creates Person and add the face to the Person.
// 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 Group

The program creates a group and adds the created persons into the 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 the Detection Model

Call /train/identify with the group as input parameter. Thus the API is ready for future detection. (Notice: /train/identify is an asychronized method. The user should wait until the training is finished to call the train function.)
// 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;

Identify Face in New Image

After detecting a face in a new image, we can identify it within the group now. // 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";