aws / aws-sdk-java

The official AWS SDK for Java 1.x. The AWS SDK for Java 2.x is available here: https://github.com/aws/aws-sdk-java-v2/
https://aws.amazon.com/sdkforjava
Apache License 2.0
4.11k stars 2.83k forks source link

Request has invalid parameters Compare Faces (API) #971

Closed rakeshkeerthi closed 7 years ago

rakeshkeerthi commented 7 years ago

Hi,

I'm trying out the sample odes provided by Amazon for Compare Faces (API). and the code is as below.

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClient;
import com.amazonaws.services.rekognition.model.CompareFacesRequest;
import com.amazonaws.services.rekognition.model.CompareFacesResult;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.S3Object;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CompareFacesExample {
    public static final String S3_BUCKET = "imagesbucketsample";

    public static void main(String[] args) throws Exception {

        AWSCredentials credentials;
        try {
            credentials = new ProfileCredentialsProvider("adminuser").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                    + "Please make sure that your credentials file is at the correct "
                    + "location (/Users/<userid>/.aws/credentials), and is in valid format.", e);
        }

        AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials)
                .withEndpoint("https://rekognition.us-east-1.amazonaws.com");
        rekognitionClient.setSignerRegionOverride("us-east-1");
        ObjectMapper objectMapper = new ObjectMapper();

        Image source = getImageUtil(S3_BUCKET, "src.jpg");
        Image target = getImageUtil(S3_BUCKET, "tgt.jpg");
        Float similarityThreshold = 70F;
        CompareFacesResult compareFacesResult = callCompareFaces(source, target, similarityThreshold,
                rekognitionClient);

        System.out.println(objectMapper.writeValueAsString(compareFacesResult));
    }

    private static CompareFacesResult callCompareFaces(Image sourceImage, Image targetImage, Float similarityThreshold,
            AmazonRekognition amazonRekognition) {
        System.out.println(sourceImage.getS3Object().getName()+", "+targetImage.getS3Object().getName());
        CompareFacesRequest compareFacesRequest = new CompareFacesRequest().withSourceImage(sourceImage)
                .withTargetImage(targetImage).withSimilarityThreshold(similarityThreshold);
        return amazonRekognition.compareFaces(compareFacesRequest);
    }

    private static Image getImageUtil(String bucket, String key) {
        return new Image().withS3Object(new S3Object().withBucket(bucket).withName(key));
    }
}

When I run this code, I get the below exception.

Exception in thread "main" com.amazonaws.services.rekognition.model.InvalidParameterException: Request has Invalid Parameters (Service: AmazonRekognition; Status Code: 400; Error Code: InvalidParameterException; Request ID: a036112b-d23b-11e6-8a06-21f42d3a685a) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1586) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1254) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672) at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518) at com.amazonaws.services.rekognition.AmazonRekognitionClient.doInvoke(AmazonRekognitionClient.java:1131) at com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:1107) at com.amazonaws.services.rekognition.AmazonRekognitionClient.compareFaces(AmazonRekognitionClient.java:345) at CompareFacesExample.callCompareFaces(CompareFacesExample.java:45) at CompareFacesExample.main(CompareFacesExample.java:34)

Out of curiosity I printed the names of the source and target files, these are same as uploaded in the S3 Bucket. Please let me know where am I going wrong and how can I fix this.

Thanks & Regards, Rakesh

kiiadi commented 7 years ago

@rakeshkeerthi hrm - that's not a very helpful exception; unfortunately the API docs don't seem to give any more detail as to what the problem is.

Can you try without the similarity threshold and see if that helps?

If that doesn't work can you post the wire logs (see here for enabling them) and we can see what the request actually looks like on the wire and go from there?

rakeshkeerthi commented 7 years ago

Hi @kiiadi , I'm sorry to say that this is not working even after removing the threshold. So I went to your second suggestion.

I'm not sure if the below data is what you have asked for.

I've followed the steps given in the document and the result is

2017-01-05 00:05:34,921 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST / HTTP/1.1[\r][\n]" 2017-01-05 00:05:34,923 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: rekognition.us-east-1.amazonaws.com[\r][\n]" 2017-01-05 00:05:34,923 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: AWS4-HMAC-SHA256 Credential=AKIAJIMX6AV5XIEMXGVA/20170104/us-east-1/rekognition/aws4_request, SignedHeaders=amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;user-agent;x-amz-date;x-amz-target, Signature=0d35d51e4f44a1df817b52fd52fd984295a045eb8a8ea5234f947ff65c02a74e[\r][\n]" 2017-01-05 00:05:34,923 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "X-Amz-Date: 20170104T183531Z[\r][\n]" 2017-01-05 00:05:34,923 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: aws-sdk-java/1.11.75 Windows_10/10.0 Java_HotSpot(TM)_64-Bit_Server_VM/25.111-b14/1.8.0_111[\r][\n]" 2017-01-05 00:05:34,923 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "amz-sdk-invocation-id: 91ab336b-0416-b6d6-db68-89697dcb05ec[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "amz-sdk-retry: 0/0/500[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "X-Amz-Target: RekognitionService.CompareFaces[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/x-amz-json-1.1[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 153[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]" 2017-01-05 00:05:34,924 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"SourceImage":{"S3Object":{"Bucket":"imagesbucketsample","Name":"src.jpg"}},"TargetImage":{"S3Object":{"Bucket":"imagesbucketsample","Name":"tgt.jpg"}}}" 2017-01-05 00:05:36,332 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: application/x-amz-json-1.1[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Wed, 04 Jan 2017 18:35:35 GMT[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "x-amzn-RequestId: 9488583d-d2ac-11e6-b2fb-9d06f52afaed[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 81[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: keep-alive[\r][\n]" 2017-01-05 00:05:36,333 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]" 2017-01-05 00:05:36,334 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{"__type":"InvalidParameterException","Message":"Request has Invalid Parameters"}" Exception in thread "main" com.amazonaws.services.rekognition.model.InvalidParameterException: Request has Invalid Parameters (Service: AmazonRekognition; Status Code: 400; Error Code: InvalidParameterException; Request ID: 9488583d-d2ac-11e6-b2fb-9d06f52afaed) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1586) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1254) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672) at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518) at com.amazonaws.services.rekognition.AmazonRekognitionClient.doInvoke(AmazonRekognitionClient.java:1131) at com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:1107) at com.amazonaws.services.rekognition.AmazonRekognitionClient.compareFaces(AmazonRekognitionClient.java:345) at CompareFacesExample.callCompareFaces(CompareFacesExample.java:49) at CompareFacesExample.main(CompareFacesExample.java:38)

If I'm going wrong, can you please help me in setting up the log?

spfink commented 7 years ago

Are the pictures you are trying actual faces?

rakeshkeerthi commented 7 years ago

Hey,

Thank you @kiiadi and @spfink . The issue was with me, the images are not that accurate.

Thanks Again.

kiiadi commented 7 years ago

We'll try to work with the service team to get more information on the error messages to assist you in debugging issues in future.

nikthakkar commented 7 years ago

I'm also facing the same issue. Some of the requests are failing saying InvalidParameter Exception. I still couldn't figure out as to why they are failing. I'm using a local folder to supply the images for comparing them against the first image in the folder. Any directions in what causes/reasons result in failing of a request would be very helpful. P.S: All the images are under the limitations listed here.

rixrix commented 6 years ago

Hi, encountered this issue today, although it seems obvious that we're comparing faces from source and target, so if my source image contains boxes instead of faces I would have expected to get an empty result rather than InvalidParameterException. The error message implies that I've missed something from the required API parameters. thanks

sample log

  message: 'Request has Invalid Parameters',
  code: 'InvalidParameterException',
  time: 2017-10-28T10:57:14.512Z,
  requestId: '******',
  statusCode: 400,
  retryable: false,
  retryDelay: 41.314294391341754 }
'InvalidParameterException'^C
rixrix commented 6 years ago

opps, I'm on the wrong channel - I'm using Node.js btw but I guess it doesn't matter, no ?

shorea commented 6 years ago

@rixrix, we generally recommend asking your question on stack overflow using the tag amazon-rekognition which is monitored by the Rekognition service team or opening a support case through your AWS account.

kumarabhishek0809 commented 4 years ago

if you are CompareFacesRequest and any one of the request does not has Person Image it is giving me same exception, only came to know with various combinations.

lcnlvrz-revelacion commented 2 years ago

Sorry but this is just so contraintuitive. Just return an empty FaceMatches list/array, it would be really helpful

SwhiteMHC commented 10 months ago

I just encountered this issue. I agree with @rixrix, this error message is not clear. Is it best practice to run detectFaces() before trying a compareFaces()?