You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace ali\alibabacloud\ivision\facebody;
use ali\oss\Oss;
use AlibabaCloud\SDK\Facebody\V20191230\Models\CompareFaceRequest;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectLivingFaceAdvanceRequest;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectLivingFaceRequest\tasks;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectVideoLivingFaceAdvanceRequest;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectVideoLivingFaceRequest;
use AlibabaCloud\SDK\Facebody\V20191230\Models\DetectLivingFaceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
//use AlibabaCloud\SDK\ViapiUtils\ViapiUtils;
use GuzzleHttp\Psr7\Stream;
class DetectLivingFace extends Base
{
/**
* 人脸活体检测
* @param array $image 待检测的图像URL地址
* @param array $image ['url'] 待检测的图像URL地址
* @param array $image ['data'] 图像Base64编码字符串。当与URL方式共存时URL方式优先
* @param boolean $is_same_region 是否上海地域
* @date 2022-12-15
*/
public function detectLivingFace($image = [], $is_same_region = true)
{
$client = self::createClient();
// 是同一地域(上海)
if ($is_same_region) {
foreach ($image as $k => $v) {
if (isset($v['url']) && !empty($v['url'])) {
$task[$k]['imageURL'] = $v['url'];
}
if (isset($v['data']) && !empty($v['data'])) {
$task[$k]['imageData'] = $v['data'];
}
$tasks[$k] = new tasks($task[$k]);
}
$detectLivingFaceRequest = new DetectLivingFaceRequest([
"tasks" => $tasks
]);
} else {
foreach ($image as $k => $v) {
if (isset($v['url']) && !empty($v['url'])) {
$file = fopen($v['url'], 'rb');
$stream = new Stream($file);
$task[$k]['imageURLObject'] = $stream;
}
if (isset($v['data']) && !empty($v['data'])) {
$task[$k]['imageData'] = $v['data'];
}
$tasks[$k] = new tasks($task[$k]);
}
$detectLivingAdvanceFaceRequest = new DetectLivingFaceAdvanceRequest([
"tasks" => $tasks
]);
}
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
if($is_same_region){
$result = $client->detectLivingFaceWithOptions($detectLivingFaceRequest, $runtime);
}else{
$result = $client->detectLivingFaceAdvance($detectLivingAdvanceFaceRequest, $runtime);
}
$result_array = $result->toMap();
$result = $result_array['body'];
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
if ($error->getErrorInfo()) {
$result = $error->getErrorInfo();
$result = $result['data'];
} else {
$result = [
'Code' => -1,
'Message' => $error->message
];
}
}
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog($task, $result, 'alibaba_cloud_ivision_face_body_detect_living_face_uid_' . $uid);
return $result;
}
/**
* 视频活体检测
* @param string $video_url 待检测人脸的视频URL
* @param boolean $is_same_region 是否上海地域
* @date 2022-12-15
*/
public function detectVideoLivingFace($video_url, $is_same_region = true)
{
$client = self::createClient();
// 是同一地域(上海)
if ($is_same_region) {
$task = [
"videoUrl" => $video_url
];
$detectVideoLivingFaceRequest = new DetectVideoLivingFaceRequest($task);
} else {
$file = fopen($video_url, 'rb');
$stream = new Stream($file);
$task = [
"videoUrlObject" => $stream
];
$detectVideoLivingFaceAdvanceRequest = new DetectVideoLivingFaceAdvanceRequest($task);
}
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
if($is_same_region){
$result = $client->detectVideoLivingFaceWithOptions($detectVideoLivingFaceRequest, $runtime);
}else{
$result = $client->detectVideoLivingFaceAdvance($detectVideoLivingFaceAdvanceRequest, $runtime);
}
$result_array = $result->toMap();
$result = $result_array['body'];
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
if ($error->getErrorInfo()) {
$result = $error->getErrorInfo();
$result = $result['data'];
} else {
$result = [
'Code' => -1,
'Message' => $error->message
];
}
}
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog($task, $result, 'alibaba_cloud_ivision_face_body_detect_viceo_living_face_uid_' . $uid);
return $result;
}
}