美文网首页PHP实战PHP开发各类api
微软emotion(表情识别)的api调试和应用

微软emotion(表情识别)的api调试和应用

作者: michaelgbw | 来源:发表于2016-06-06 22:38 被阅读2636次

hi,all,最近离考试还有一段时间,这里分享一些最近我调试微软的表情识别的api接口的一些心得(各种坑)。

简单介绍微软的emotion api

首先贴出api地址,微软还很贴心的给出了console的一个调试页面,有没有感动的哭了,呜呜呜。。。首先看他的官方解释: <em>情感的API测试以图像作为输入,并返回的信心在一组情绪每个人脸在图像中,以及包围盒的脸上,从脸的API。情绪检测幸福、悲伤、惊讶、愤怒,恐惧,轻蔑、厌恶或中性。这些情感是相通的跨文化普遍通过基本相同的面部表情,在确定情感的API。</em>

还提供了各种demo,一切好像都变的简单起来!

啦啦啦

好的,我们先来一段js代码,气氛搞起来,

<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        var params = {
            // Request parameters
        };
      
        $.ajax({
            url: "https://api.projectoxford.ai/emotion/v1.0/recognize?" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{subscription key}");
            },
            type: "POST",
            // Request body
            data: "{body}",
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>

简单,我们来改下相关参数,哈哈哈哈,我去,为什么老提示什么


擦擦擦

好吧,这个原来是js 的ajax跨域不能访问的原因,微软这坑X,那好用jsonp吧,这个可以吧

 $.ajax({ type: "get",
 async: false,
 url: "http://example", 
dataType: "jsonp",
 jsonp: "callback",
//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback) 
jsonpCallback:"flightHandler",
//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据 
success: function(json)
{ alert('您查询到航班信息:票价: ' + json.price + ' 元,余票: ' + json.tickets + ' 张。'); }, 
error: function(){ alert('fail'); }
 });
 });

就类似这种的,我们知道,哪怕跨域js文件中的代码(当然指符合web脚本安全策略的),web页面也是可以无条件执行的。,,wait,为什么还不行,jsonp是只支持get的,post不行,

看这里 擦擦擦

好吧,我是一个PHP程序员,那么就用宇宙最好的语言吧,哈,刚好人家也提供了demo

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://api.projectoxford.ai/emotion/v1.0/recognize');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => '{subscription key}',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_POST);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

稍等,这是啥?Http_Request2,,还有这样写demo的,这个直接我都没有,好不容易下了个,人家还require_once()了好几个。。


擦擦擦

好吧,自己写,,用curl吧,大家要没用过,就看看这篇文章写的蛮好的,
好的,直接贴出代码吧,知道你们最想看什么


$imgurl=trim($_POST['imgurl']) ? trim($_POST['imgurl']) : '';
$patharr=explode('.', $_SERVER['SCRIPT_NAME']);
$path=substr($patharr[0], 0,strrpos($patharr[0],'/'));
$finurl="http://".$_SERVER['SERVER_NAME'].$path.'/'.$imgurl;
if($imgurl=='' or !isset($imgurl)){
    return 'fail';
}
$header = array (  
     "POST HTTP/1.1",  
     "Content-Type: application/json",
     "Ocp-Apim-Subscription-Key:XXXXXXXXX",
 ); 32879366.jpg"; 
$param=array('url'=>$finurl);
$jsonparam=json_encode($param);
$ch = curl_init(); 
$url="https://api.projectoxford.ai/emotion/v1.0/recognize"; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonparam);//
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方  
curl_setopt($ch, CURLOPT_HEADER, 0);    //no取得返回头信息
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   
curl_setopt($ch, CURLOPT_TIMEOUT, 10);  
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//不直接输出response 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//如果返回的response 头部中存在Location值,就会递归请求  

try {
    $result = curl_exec($ch);
    //unlink($imgurl);
    echo $result;
 } catch (Exception $e) {
    echo "fail!".'<br>';
    var_dump(curl_error($ch)); 
 } 

好的,终于解决了最主要的问题,剩下的就简单了,页面什么的,能用就好了。

谢谢大家

最后附上项目部署的BAE地址,ps:这个地址可能随时失效,主要是欠费了。。。。。。。
http://huanyi.duapp.com/emotion.html

相关文章

网友评论

    本文标题:微软emotion(表情识别)的api调试和应用

    本文链接:https://www.haomeiwen.com/subject/uclddttx.html