认知服务面对api使用本地图像 - PHP

时间:2016-08-02 11:09:39

标签: php microsoft-cognitive

我正在尝试使用PHP将本地图像上传到face API,我使用示例代码调用api,当我使用JSON时它很好。但是当改为octet-stream时它会卡住。我通过引用“https://pear.php.net/manual/en/package.http.http-request2.request.php

来使用以下代码
$image = "images/face-021.jpg";
$fp = fopen($image, 'r+');
$request->addUpload('stuff', $fp, 'custom.name', 'application/octet-stream');

我从api得到以下回复。

stdClass Object
(
[error] => stdClass Object
    (
        [code] => InvalidImageSize
        [message] => Image size is too small or too big.
    )
)

在将二进制数据传递给api之前我错过了一些步骤吗?

图像尺寸仅为956 KB,尺寸为2448 x 2448

2 个答案:

答案 0 :(得分:1)

Face API不支持多部分MIME有效负载。您应该直接在请求正文中发送图像。

$image = 'images/face-021.jpg';
$fp = fopen($image, 'rb');
$request->setBody($fp);
$request->setHeader('Content-Type', 'application/octet-stream');
$request->setMethod(HTTP_Request2::METHOD_POST);

答案 1 :(得分:0)

function microimg() { 
    //$GLOBALS['telegram']->ssendmessage($GLOBALS['userid'], $urlImage);      
    $request = new Http_Request2('https://westus.api.cognitive.microsoft.com/face/v1.0/detect');
    $url1 = $request->getUrl();

    $headers = array(`enter code here`
        'Content-Type' => 'application/json',
        'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxx'
    );

    $request->setHeader($headers);

    $parameters = array(
    // Request parameters
    'returnFaceId' => 'true',
    'returnFaceLandmarks' => 'false',
    'returnFaceAttributes' => 'age',
    );

    $parameters = array();
    $url1->setQueryVariables($parameters);
    $request->setMethod(HTTP_Request2::METHOD_POST);

    //$request->setBody('{"url":  "http://miladddddddddddddd.ml/fillter_image/photos/file_93.jpg" }');
    $request->setBody('{"url":  "'.$GLOBALS['fileurl'].'" }');
    $err = $request->setBody;
    try {
        $response = $request->send();
        echo $response->getBody();
        $ed = $response->getBody();
        $GLOBALS['telegram']->ssendmessage($GLOBALS['userid'],$ed);
         $GLOBALS['telegram']->ssendmessage($GLOBALS['userid'], $GLOBALS['fileurl']);      
    } catch (HttpException $ex) {
        echo $ex;
    }
}


microimg($urlimg);