PHP人脸识别小程序接口(阿里云OpenAPI)
短信预约 -IT技能 免费直播动态提醒
这两天接的小程序用了阿里云的openapi--人脸识别接口,讲下大体流程和代码
- 先进入人脸识别文档,开通该服务
- 然后建立RAM子账号,并设置accessKey,并记下两个参数
- 使用阿里云新版php-sdk GitHub - aliyun/openapi-sdk-php: Alibaba Cloud SDK for PHP,用composer安装
-
composer require alibabacloud/sdk
我用的框架tp5 获取调用该接口的权限
-
regionId('cn-hangzhou') ->asDefaultClient();
添加人脸#
参考地址: 阿里云OpenAPI开发者门户
public function AddFace($Group, $Person, $Image, $Content){ try { $result = AlibabaCloud::rpc() ->product('FaceAPI') ->version('2018-12-03') ->action('AddFace') ->method('POST') ->host('face.cn-shanghai.aliyuncs.com') ->regionId('cn-shanghai') ->setAcceptFormat('json') ->setQueryParameters('Group', $Group) ->setQueryParameters('Person', $Person) ->setQueryParameters('Image', $Image) // ->setQueryParameters('ImageUrl', $ImageUrl) ->setQueryParameters('Content', $Content) ->request(); $message = [ 'status'=>'failed', 'message'=>$result['Message'] ]; if(!empty($result['Success']) && $result['Success'] === true){ $message = [ 'status'=>'success', 'message'=>'添加人物肖像图成功.' ]; }else if(!empty($result['Code'])){ if((strstr($result['Message'], 'Duplicate entry'))){ $message = [ 'status'=>'failed', 'message'=>'请勿重复添加人物肖像图.' ]; }else if((strstr($result['Message'], 'Image Decode Failed'))){ $message = [ 'status'=>'failed', 'message'=>'图片解码失败, 请检查上传图片的编码.' ]; }else if((strstr($result['Message'], 'NO_FACE_DETECTED'))){ $message = [ 'status'=>'failed', 'message'=>'请上传正确的人脸图片.' ]; }else if((strstr($result['Message'], 'expect 1'))){ $message = [ 'status'=>'failed', 'message'=>'请上传只有一张人脸的图片.' ]; } } return $this->response->create(['message'=>$message], 'json')->code('200'); } catch (ClientException $exception) { print_r($exception->getErrorMessage()); } catch (ServerException $exception) { print_r($exception->getErrorMessage()); } }
删除人脸#
参考地址: 阿里云OpenAPI开发者门户
public function DeleteFace($Group, $Person, $Image){ try { $result = AlibabaCloud::rpc() ->product('FaceAPI') ->version('2018-12-03') ->action('DeleteFace') ->method('POST') ->host('face.cn-shanghai.aliyuncs.com') ->regionId('cn-shanghai') ->setAcceptFormat('json') ->setQueryParameters('Group', $Group) ->setQueryParameters('Person', $Person) ->setQueryParameters('Image', $Image) ->request(); $result = $result->toArray(); if(!empty($result['Success']) && $result['Success'] === true){ $message = [ 'status'=>'success', 'message'=>'删除人物肖像图成功.' ]; }else{ $message = $result; } return $this->response->create($message, 'json')->code('200'); } catch (ClientException $exception) { print_r($exception->getErrorMessage()); } catch (ServerException $exception) { print_r($exception->getErrorMessage()); } }
列出人脸数据#
参考地址: 阿里云OpenAPI开发者门户
function ListGroup(){ try { $result = AlibabaCloud::rpc() ->product('FaceAPI') ->version('2018-12-03') ->action('ListGroup') ->method('POST') ->host('face.cn-shanghai.aliyuncs.com') ->regionId('cn-shanghai') ->setAcceptFormat('json') ->request(); $result = $result->toArray(); if(!empty($result['Success']) && $result['Success'] === true){ $message = [ 'status'=>'success', 'message'=>'获取分组成功.', 'data'=>$result['Data'], ]; }else{ $message = $result; } return $this->response->create($message, 'json')->code('200'); } catch (ClientException $exception) { print_r($exception->getErrorMessage()); } catch (ServerException $exception) { print_r($exception->getErrorMessage()); } }
人脸检索,返回的是一个score,我判断小于0.5即不是一个人#
参考地址: 阿里云OpenAPI开发者门户
function RecognizeFace($Group, $Content){ try { $result = AlibabaCloud::rpc() ->product('FaceAPI') ->version('2018-12-03') ->action('RecognizeFace') ->method('POST') ->host('face.cn-shanghai.aliyuncs.com') ->regionId('cn-shanghai') ->setAcceptFormat('json') ->setQueryParameters('Group', $Group) // ->setQueryParameters('ImageUrl', $ImageUrl) ->setQueryParameters('Content', $Content) ->request(); $result = $result->toArray(); if(!empty($result['Success']) && $result['Success'] === true){ if($result['Data'][0]['score'] > 0.5){ $message = [ 'status'=>'success', 'message'=>'验证成功.', 'data'=>$result['Data'], ]; }else{ $message = [ 'status'=>'failed', 'message'=>'验证失败.', 'data'=>$result['Data'], ]; } }else{ $message = $result; } return $message; } catch (ClientException $exception) { print_r($exception->getErrorMessage()); } catch (ServerException $exception) { print_r($exception->getErrorMessage()); } }
来源地址:https://blog.csdn.net/qq_73571591/article/details/130365225
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341