这两天接的小程序用了阿里云的openapi--人脸识别接口,讲下大体流程和代码 先进入人脸识别文档,开通该服务 然后建立RAM子账号,并设置accessKey,并记下两个参数使用阿里云新版PHP-sdk GitHub - aliyun/o
这两天接的小程序用了阿里云的openapi--人脸识别接口,讲下大体流程和代码
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()); } }
参考地址: 阿里云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
--结束END--
本文标题: PHP人脸识别小程序接口(阿里云OpenAPI)
本文链接: https://lsjlt.com/news/418667.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0