zoukankan      html  css  js  c++  java
  • PHP使用微软认知服务Face API


    下面主要介绍基于PHP语言,基于guzzle类库,调用微软最新推出的认知服务:人脸识别

    实验环境:

    • IDE:Eclipse for PHP Developers
    • Version: Neon.1 Release (4.6.1)
    • Server:WampServer Version 2.5
    • HttpClient:guzzle

    1. 使用composer安装Guzzle

    • composer.json文件
     {
       "require": {
          "guzzlehttp/guzzle": "~6.0"
       }
    }
    
    • composer.phar下载
    • 打开控制台指令,在项目目录下运行如下指令完成包的安装
    php composer.phar install
    

    2. 证书安装

    注意:默认的http客户端工具一般到需要证书的认证,如果没有导入相应的证书一般会报SSL证书认证错误。

    • 下载证书到本地,下载地址,将其保存到相应的位置。
    • 为了让证书生效需要相应修改php.ini,修改方法如下:
    curl.cainfo =<filepath>/cacert.pem
    

    3. PHP代码示例

    <?php
    use GuzzleHttpPsr7Request;
    use GuzzleHttpClient;
    
    require_once 'vendorautoload.php';
    
    $client = new Client();
    
    $headers = ['Content-Type' => 'application/json','Ocp-Apim-Subscription-Key' => '{key}'];
    $body = '{"url":"{image's url}"}';
    
    $request = new Request('POST','https://api.cognitive.azure.cn/face/v1.0/detect', $headers , $body);
    
    $response = $client->send($request);
    
    echo $response->getBody();
    ?>
    

    4. 运行结果示例

    [{"faceId":"b22efa8f-0e34-4344-8346-ca8b79d29a27","faceRectangle":{"top":181,"left":184,"width":257,"height":257}}]
    
    
    

    5. 读取本地图片

    <?php
    
    use GuzzleHttpPsr7Request;
    use GuzzleHttpClient;
    
    require_once 'vendorautoload.php';
    
    $client = new Client();
    
    $headers = ['Content-Type' => 'application/octet-stream','Ocp-Apim-Subscription-Key' => 'bd8e4ce12f444c639ac9c214d70ac72c'];
    
    $myfile = fopen("tt.jpg", "r") or die("Unable to open file!");
    
    $request = new Request('POST','https://api.cognitive.azure.cn/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age', $headers , $myfile);
    
    $response = $client->send($request);
    
    echo $response->getBody();
    
    fclose($myfile);
    ?>
    

    6. 更多参考

  • 相关阅读:
    map的初级应用
    RB-Tree删除详解
    RB-Tree插入过程详解
    红黑树操作详解——很形象的过程
    一个数据结构可视化的神奇网址——形象理解
    关于B树B+树的详细解释——绝对精彩
    c++入门之函数指针和函数对象
    树的平衡之AVL树——错过文末你会后悔,信我
    二叉查找树的删除
    1 vmware 如何联网,以及行命令令初步
  • 原文地址:https://www.cnblogs.com/taro/p/8329231.html
Copyright © 2011-2022 走看看