智能媒体管理(IMM)提供了高效、准确的人脸识别、人脸分组等相关功能。通过简单的API调用即可实现。以下给出一个PHP的DEMO供参考。
引入SDK
首先,从Github上获取阿里云OpenAPI的PHP版本SDK,引入相关类库,并初始化客户端。
include_once './aliyun-php-sdk-core/Config.php';
use imm\Request\V20170906 as Imm;
//初始化客户端
$iClientProfile = DefaultProfile::getProfile(
"cn-beijing", //服务区域Region,根据需要修改
"xxxxxxx", //您的AccessKey
"xxxxxxxxxxxx" //您的AccessSecret
);
$client = new DefaultAcsClient($iClientProfile);
创建Project
接下来需要创建一个Project,随后的操作均归属于该Project。Project的CU需要配置为>0的数字,CU的意义为每秒钟允许请求API的次数,即QPS。同时也是计费用的标准。具体计费详情可以参考这里。
//设置您的项目名称
$projectName = "face-group-demo-project";
$request = new Imm\PutProjectRequest();
$request->setProject($projectName);
//设置为图片专业型,以便使用人脸聚类功能
$request->setType("PhotoProfessional");
//需要设置CU为大于1的值以便使用接口
$request->setCU(1);
$response = $client->getAcsResponse($request);
print_r($response);
打出的内容应类似
stdClass Object
(
[CU] => 1
[Type] => PhotoProfessional
[CreateTime] => 2018-10-10T02:31:18Z
[RequestId] => 0B1917D5-DB51-4430-91E7-7DA727171BCF
[ModifyTime] => 2018-10-10T03:02:43Z
[Project] => face-group-demo-project
[Endpoint] => imm.cn-beijing.aliyuncs.com
[ServiceRole] =>
)
创建FaceSet
接下来创建一个FaceSet。FaceSet即人脸集合。人脸分组的操作是以FaceSet集合为单元进行分组的。不同FaceSet之间的分组是隔离的。
//创建Set
$request = new Imm\CreateFaceSetRequest();
$request->setProject($projectName);
$response = $client->getAcsResponse($request);
print_r($response);
//获取SetId
$setId = $response->SetId;
打出的内容如下。其中SetId即当前创建的FaceSet的SetId。
stdClass Object
(
[Status] => Running
[Photos] => 0
[CreateTime] => 2018-10-10T03:02:44.184Z
[RequestId] => 8A396F02-87B5-4440-B5CD-5AD7CB64F5C4
[Faces] => 0
[ModifyTime] => 2018-10-10T03:02:44.184Z
[SetId] => FACE-b8729472-d392-4667-8ad6-0b7f0a80945b
)
进行IndexFace人脸检测
随后即可使用IndexFace人脸检测接口,添加照片到该FaceSet集合中。我们上一步已经通过$setId
记录了该FaceSet的SetId
。
//调用IndexFace接口,进行人脸检测
for($i=0;$i<5;$i++){
$request = new Imm\IndexFaceRequest();
$request->setProject($projectName);
$request->setSetId($setId);
//您可以替换为您所需要图片的OSS路径
$request->setSrcUris(json_encode(["oss://imm-user-wmt-cn-beijing/faces/".$i.".bmp"]));
$response = $client->getAcsResponse($request);
print_r($response);
}
每一次IndexFace都会返回该照片的检测信息,字段意义请参考官方文档,您可以根据需要取用。举例如下。
stdClass Object
(
[SrcUris] => Array
(
[0] => oss://imm-user-wmt-cn-beijing/faces/0.bmp
)
[RequestId] => AC8D2509-5C40-454C-813D-34986563D857
[SuccessDetails] => Array
(
[0] => stdClass Object
(
[SrcUri] => oss://imm-user-wmt-cn-beijing/faces/0.bmp
[Faces] => Array
(
[0] => stdClass Object
(
[FaceAttribute] => stdClass Object
(
[Blur] => stdClass Object
(
[Blurness] => stdClass Object
(
[Value] => 12.217
[Threshold] => 50
)
)
[FaceQuality] => stdClass Object
(
[Value] => 100
[Threshold] => 70.1
)
[HeadPose] => stdClass Object
(
[RollAngle] => 2.934
[PitchAngle] => 10.529
[YawAngle] => -3.145
)
[Age] => stdClass Object
(
[Value] => 21
)
[EyeStatus] => stdClass Object
(
[RightEyeStatus] => stdClass Object
(
[DarkGlasses] => 0
[NoGlassEyeClose] => 0.001
[Occlusion] => 0.049
[NormalGlassEyeOpen] => 0.106
[NormalGlassEyeClose] => 0.001
[NoGlassEyeOpen] => 99.842
)
[LeftEyeStatus] => stdClass Object
(
[DarkGlasses] => 0.014
[NoGlassEyeClose] => 0.006
[Occlusion] => 0.504
[NormalGlassEyeOpen] => 4.954
[NormalGlassEyeClose] => 0.004
[NoGlassEyeOpen] => 94.517
)
)
[Gender] => stdClass Object
(
[Value] => Male
)
)
[FaceRectangle] => stdClass Object
(
[Top] => 17
[Height] => 89
[Width] => 89
[Left] => 4
)
[FaceId] => 66690675bd0bc2599170fc63e7f8dbd8
)
)
[PhotoId] => 77f6e9ce630b83956192e8bd4af3cf39
)
)
[FailDetails] => Array
(
)
[SetId] => FACE-b8729472-d392-4667-8ad6-0b7f0a80945b
)
进行GroupFace人脸分组
检测完毕后,可以对该FaceSet进行GroupFace操作,即人脸分组。该步可将当前FaceSet中相似的人脸归位一类。
//进行人脸分组操作
$request = new Imm\GroupFacesRequest();
$request->setProject($projectName);
$request->setSetId($setId);
$response = $client->getAcsResponse($request);
print_r($response);
结果在下方打出。我们可以看出,数组中0和2号图片被分为1组,即我们推测为同一个人的不同照片。1号和3号图片暂时没有相似的人脸,因此GroupId为0。随着之后我们再添加更多的人脸,这两张图片可能会被归类到不同组中。
stdClass Object
(
[Groups] => Array
(
[0] => stdClass Object
(
[UnGroupReason] =>
[GroupId] => 1
[FaceId] => dadddf6bdd41d02aadc9fc4f2b54ca2b
)
[1] => stdClass Object
(
[UnGroupReason] => FaceNoSimilar
[GroupId] => 0
[FaceId] => 8342bcf23d66223048e10b287293754e
)
[2] => stdClass Object
(
[UnGroupReason] =>
[GroupId] => 1
[FaceId] => 7c551fd6aae5e09d58d52fee8bf72da1
)
[3] => stdClass Object
(
[UnGroupReason] => FaceNoSimilar
[GroupId] => 0
[FaceId] => 66690675bd0bc2599170fc63e7f8dbd8
)
)
[RequestId] => 3063F833-1C71-470B-B24A-CEF4F9C14A02
[HasMore] => 0
[SetId] => FACE-b8729472-d392-4667-8ad6-0b7f0a80945b
)
通过GetFaceSetDetail获取FaceSet信息
我们可以随时通过GetFaceSetDetail接口来获取当前FaceSet的分组详情,以及照片识别的详情。
//获取当前Set的信息
$request = new Imm\GetFaceSetDetailRequest();
$request->setProject($projectName);
$request->setSetId($setId);
$response = $client->getAcsResponse($request);
print_r($response);
返回值包括GroupFace和IndexFace中的所有信息,在此不再列出。
完整代码
下面给出完整代码供复制。
<?php
include_once './aliyun-php-sdk-core/Config.php';
use imm\Request\V20170906 as Imm;
//初始化客户端
$iClientProfile = DefaultProfile::getProfile(
"cn-beijing", //服务区域Region,根据需要修改
"xxxxxxx", //您的AccessKey
"xxxxxxxxxxxx" //您的AccessSecret
);
$client = new DefaultAcsClient($iClientProfile);
//设置您的项目名称
$projectName = "face-group-demo-project";
$request = new Imm\PutProjectRequest();
$request->setProject($projectName);
//设置为图片专业型,以便使用人脸聚类功能
$request->setType("PhotoProfessional");
//需要设置CU为大于1的值以便使用接口
$request->setCU(1);
$response = $client->getAcsResponse($request);
print_r($response);
//创建Set
$request = new Imm\CreateFaceSetRequest();
$request->setProject($projectName);
$response = $client->getAcsResponse($request);
print_r($response);
//获取SetId
$setId = $response->SetId;
//调用IndexFace接口,进行人脸检测
for($i=0;$i<5;$i++){
$request = new Imm\IndexFaceRequest();
$request->setProject($projectName);
$request->setSetId($setId);
//您可以替换为您所需要图片的OSS路径
$request->setSrcUris(json_encode(["oss://imm-user-wmt-cn-beijing/faces/".$i.".bmp"]));
$response = $client->getAcsResponse($request);
print_r($response);
}
//进行人脸分组操作
$request = new Imm\GroupFacesRequest();
$request->setProject($projectName);
$request->setSetId($setId);
$response = $client->getAcsResponse($request);
print_r($response);
//获取当前Set的信息
$request = new Imm\GetFaceSetDetailRequest();
$request->setProject($projectName);
$request->setSetId($setId);
$response = $client->getAcsResponse($request);
print_r($response);
如果您有任何问题,都可以联系 @宜和 @大邪 @刘杰 处理。