【问题标题】:AWS Rekognition PHP SDK gives invalid image encoding errorAWS Rekognition PHP SDK 给出了无效的图像编码错误
【发布时间】:2017-06-22 15:51:50
【问题描述】:

我正在使用 PHP 开发工具包上传要在 AWS Rekognition 中解析的本地文件(不是 S3)。但是,图像 blob 不起作用,我收到消息:InvalidImageFormatException: "Invalid image encoding"

我尝试了多张图片 (the docs say JPEGs and PNGs are accepted),但都没有效果。

我的代码是:

$client = new RekognitionClient($credentials);

$im = file_get_contents('/app/image1.png');
$imdata = base64_encode($im);

$result = $client->detectLabels(
    [
       'Image' => [
          'Bytes' => $imdata,
       ]
    ]
);

我的编码是否正确? docs 相当模糊。

我发现了一些关于“无图像内容”的问题,但没有发现关于无效格式的问题。

有什么想法吗?谢谢!

【问题讨论】:

  • 检查 base64_encode() 是否返回 FALSE。另外,尝试旧式数组:detectLabels(array('Image'=>array('Bytes'=>$imdata)))
  • @SergeyKovalev 是的,base64_encode() 确实按预期返回了数据,不幸的是,更改数组样式也不起作用:/
  • 如果你跳过base64_encode() 部分怎么办?
  • 你不应该使用 base64_encode()。没有它也可以工作。
  • @nibty 那我要传递给请求什么?除了 Imagick 的 blob 之外,没有什么可以用?

标签: php amazon-web-services amazon-rekognition


【解决方案1】:

我最终使用了Imagick 而不是base64_encode 路由。我怀疑这不是最好的方法,但它确实很好用!

$client = new RekognitionClient($credentials);

$image = new Imagick('/app/image1.png');
$imdata = $image->getImageBlob();

$result = $client->detectLabels(
    [
       'Image' => [
          'Bytes' => $imdata,
       ]
    ]
);

【讨论】:

  • 你不需要任何库来做到这一点。和刚才调用file_get_contents('/app/image1.png')完全一样。
【解决方案2】:

看来您不应该应用 base64 编码。 SDK 为 blob 执行此操作。

https://github.com/aws/aws-sdk-php/blob/master/src/Api/Serializer/JsonBody.php:

        case 'blob':
            return base64_encode($value);

【讨论】:

  • 我无法让它工作,它给了我同样的错误......
  • 看起来文档很混乱。他们应该说“你不能”而不是“可能不需要”。
【解决方案3】:
    $s3 = new \Aws\Rekognition\RekognitionClient([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'credentials' => [
            'key'    => 'BKxxxxxxxx',
            'secret' => 'GYxxxxxxxxxxxxxxxxxx'
        ]
    ]);

    $result = $s3->detectLabels([
        'Image' => [ // REQUIRED
            'Bytes' => file_get_contents("http://img13.deviantart.net/5a3b/i/2010/249/b/a/__michelangelo__s_flying_horse___by_dark_oak_trails-d2y5iej.jpg"),
        ],
        'MaxLabels' => 10,
        'MinConfidence' => 90,
    ]);

不需要在使用他们的 SDK 时对图像进行 base64_enconde。

http://docs.aws.amazon.com/rekognition/latest/dg/API_Image.html

如果您使用 AWS 开发工具包,您的代码可能不需要对图像字节进行编码...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多