第一个Face++ PHP程序

说明

本教程编写了一个示例性的Face++ App,用以实现在3张已知的人脸图片中识别一个新的人脸图片。

首先,检测出三张输入图片中的Face。然后,使用这三个Face各自创建1个新的Person。这三个Person将构建一个新的Group以作为候选的识别库。最后,输入一张新Image,并在已构建的Group中识别这个新Image中的Face。

程序使用php编写,完整代码可于此处下载(需要修改api_key, api_secret, getTrainingUrl, getPhotoUrl后才能正常运行)

运行

下载解压后,您将看到两个文件:FacePPClientDemo.php和HelloWorld.php。您可以按如下步骤执行:

1. 编辑HelloWorld.php,写入API_Key和API_Secret。

2. (可选)修改getTrainingUrl中的网址前缀为您想要的训练图片网址前缀。

3. 终端下,运行命令php HelloWorld.php,即可看到执行结果。

程序解读

创建新的Person

待识别图片首先需要经过初步检测,找出图片中Face的位置及属性。然后,再使用API中的Person管理创建新的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
    $api->person_delete($person_name);
   	// create a new person for this face
   	$api->person_create($person_name);
   	// add face into new person
   	$api->person_add_face($face_id, $person_name);
}

构建新的Group

注册一个新的Group,并将之前创建的Person加入这个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
	$api->group_delete($group_name);
	// create new group
	$api->group_create($group_name);
   	// add new person into the group
	foreach ($person_names as $person_name)
	   	$api->group_add_person($person_name, $group_name);
}

训练识别模型

对刚创建的group,调用/train/identify来训练识别特征以待识别。
(注意:/train/identify是一个异步方法,因此在调用train函数后我们需要等待训练完毕才能继续执行后续代码。)

// 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)) 
    {
        sleep(1);

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

识别新图中的Face

在对新图中的Face进行以上提及的检测,注册和训练之后,我们就可以在已构建Group中识别新图中出现的Face了:

// 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";
}