Amazon Rekognition“请求的图像格式无效”

时间:2019-09-20 13:11:50

标签: amazon-web-services go amazon-s3 aws-sdk

总是从CompareFaces()方法中得到相同的错误。我尝试了不同的图像格式,图像大小,图像类型,各种编码(或全都不编码)。没有任何帮助我。请给我一个提示,该怎么办或指出我的错误。谢谢!

go版本go1.12.7 linux / amd64,AWSSDKVersion =“ 1.23.17”

import (
"bytes"
"encoding/base64"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rekognition"
"github.com/aws/aws-sdk-go/service/s3"
"io/ioutil"
"log"
"net/http"
"os"
)

func main() {
creds := credentials.NewCredentials(
    &credentials.StaticProvider{Value: credentials.Value{
        AccessKeyID:     "", // insert creds here
        SecretAccessKey: "",
    }},
)

var myBucket = "my-very-unusual-and-odd-test-bucket"

imgFile, _ := os.Open("path.JPEG")
defer imgFile.Close()

buf, _ := ioutil.ReadAll(imgFile)
base64Text := []byte(base64.StdEncoding.EncodeToString(buf))

awsSess, _ := session.NewSession(&aws.Config{
    Credentials: creds,
    Region:      aws.String("us-west-2"),
    LogLevel:    aws.LogLevel(aws.LogDebug),
})

svc := s3.New(awsSess)
bucket := &s3.CreateBucketInput{
    Bucket: aws.String(myBucket),
}
svc.CreateBucket(bucket)

svc.PutObject(&s3.PutObjectInput{
    Bucket:             aws.String(myBucket),
    Key:                aws.String(myBucket + "/photo"),
    Body:               bytes.NewReader(base64Text),
    ContentLength:      aws.Int64(int64(len(base64Text))),
    ContentType:        aws.String(http.DetectContentType(base64Text)),
    ContentDisposition: aws.String("attachment"),
})

input := &s3.ListObjectsInput{
    Bucket:  aws.String(myBucket),
    MaxKeys: aws.Int64(50),
}

objects, _ := svc.ListObjects(input)
svcR := rekognition.New(awsSess)

for _, object := range objects.Contents {
    key := *object.Key

    input := &rekognition.CompareFacesInput{
        SimilarityThreshold: aws.Float64(90.000000),
        SourceImage: &rekognition.Image{
            Bytes: base64Text,
        },
        TargetImage: &rekognition.Image{
            S3Object: &rekognition.S3Object{
                Bucket: aws.String(myBucket),
                Name:   aws.String(key),
            },
        },
    }

    _, err := svcR.CompareFaces(input)
    if err != nil {
        log.Fatal(err)
    }
}}

0 个答案:

没有答案