返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >PHP人脸识别小程序接口(阿里云OpenAPI)
  • 641
分享到

PHP人脸识别小程序接口(阿里云OpenAPI)

阿里云云计算php 2023-09-26 16:09:34 641人浏览 泡泡鱼
摘要

这两天接的小程序用了阿里云的openapi--人脸识别接口,讲下大体流程和代码 先进入人脸识别文档,开通该服务  然后建立RAM子账号,并设置accessKey,并记下两个参数使用阿里云新版PHP-sdk GitHub - aliyun/o

这两天接的小程序用了阿里云的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

--结束END--

本文标题: PHP人脸识别小程序接口(阿里云OpenAPI)

本文链接: https://lsjlt.com/news/418667.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作