1

我一直在使用来自 Amazon 的新 rekognition API,但在从 IntelliJ 运行他们的示例Java 应用程序时遇到问题。我正在使用 Maven 构建项目,并将 AWS 开发工具包包含在我pom.xml的如下:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>${aws-java-sdk.version}</version>
</dependency>

据我所知,我的应用程序似乎在这里的某个地方失败了:

AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials)
        .withEndpoint("s3-us-west-2.amazonaws.com");
rekognitionClient.setSignerRegionOverride("us-west-2");
try {
    DetectFacesResult result = rekognitionClient.detectFaces(request);
    ObjectMapper objectMapper = new ObjectMapper();
    System.out.println("Result = " + objectMapper.writeValueAsString(result));
} catch (AmazonRekognitionException e) {
    e.printStackTrace();
}

...我得到的错误是:

com.amazonaws.services.rekognition.model.AmazonRekognitionException: null (Service: AmazonRekognition; Status Code: 400; Error Code: null; Request ID: null)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1545)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1183)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:964)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:676)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:650)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:633)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$300(AmazonHttpClient.java:601)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:583)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:447)
    at com.amazonaws.services.rekognition.AmazonRekognitionClient.doInvoke(AmazonRekognitionClient.java:1130)
    at com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:1106)
    at com.amazonaws.services.rekognition.AmazonRekognitionClient.detectFaces(AmazonRekognitionClient.java:599)
    at com.github.jhenningsgaard.DetectFaces.main(DetectFaces.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我还应该注意,我在 AWS CLI 中运行了操作(见下文)并且成功。

aws rekognition detect-faces \
--image '{"S3Object":{"Bucket":"Bucketname","Name":"s3ObjectKey"}}' \
--attributes "ALL" \
--region us-east-1 \
--profile adminuser
4

2 回答 2

1

您可以使用以下代码创建 rekognition 客户端的实例:

AWSCredentials credentials;
try {
    credentials = new ProfileCredentialsProvider("default").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 a valid format.", e);
}
AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials)
        .withEndpoint("rekognition.eu-west-1.amazonaws.com");
rekognitionClient.setSignerRegionOverride("eu-west-1");
于 2017-01-29T21:30:26.617 回答
0

以下是使用software.amazon.awssdk.services.rekognition.RekognitionClient对象(API 的第 2 版)比较两张面孔的完整代码示例。

您可以在 AWS Docs中找到更多使用RekognitionClient版本 2 的示例。

此外,要从 IntelliJ 运行这些示例,您需要确保拥有正确的 POM。在 AWS Github 示例中,始终列出相应的 POM。

package com.example.rekognition;

// snippet-start:[rekognition.java2.compare_faces.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
import software.amazon.awssdk.services.rekognition.model.Image;
import software.amazon.awssdk.services.rekognition.model.CompareFacesRequest;
import software.amazon.awssdk.services.rekognition.model.CompareFacesResponse;
import software.amazon.awssdk.services.rekognition.model.CompareFacesMatch;
import software.amazon.awssdk.services.rekognition.model.ComparedFace;
import software.amazon.awssdk.services.rekognition.model.BoundingBox;
import software.amazon.awssdk.core.SdkBytes;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
// snippet-end:[rekognition.java2.compare_faces.import]


public class CompareFaces {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "CompareFaces - how to compare two faces in two images\n\n" +
                "Usage: CompareFaces <pathSource> <pathTarget>\n\n" +
                "Where:\n" +
                "pathSource - the path to the source image (ie, C:\\AWS\\pic1.png) \n " +
                "pathTarget - the path to the target image (ie, C:\\AWS\\pic2.png) \n\n";

        Float similarityThreshold = 70F;
        String sourceImage = args[0];
        String targetImage = args[1];

        Region region = Region.US_EAST_2;
        RekognitionClient rekClient = RekognitionClient.builder()
                .region(region)
                .build();

        compareTwoFaces(rekClient, similarityThreshold, sourceImage, targetImage);
   }

    // snippet-start:[rekognition.java2.compare_faces.main]
    public static void compareTwoFaces(RekognitionClient rekClient, Float similarityThreshold, String sourceImage, String targetImage) {

        try {
            InputStream sourceStream = new FileInputStream(new File(sourceImage));
            InputStream tarStream = new FileInputStream(new File(targetImage));

            SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
            SdkBytes targetBytes = SdkBytes.fromInputStream(tarStream);

            // Create an Image object for the source image
            Image souImage = Image.builder()
            .bytes(sourceBytes)
            .build();

            // Create an Image object for the target image
            Image tarImage = Image.builder()
                    .bytes(targetBytes)
                    .build();

            // Create a CompareFacesRequest object
            CompareFacesRequest facesRequest = CompareFacesRequest.builder()
                    .sourceImage(souImage)
                    .targetImage(tarImage)
                    .similarityThreshold(similarityThreshold)
                    .build();

            // Compare the two images
            CompareFacesResponse compareFacesResult = rekClient.compareFaces(facesRequest);

            // Display results
            List<CompareFacesMatch> faceDetails = compareFacesResult.faceMatches();
            for (CompareFacesMatch match: faceDetails){
                ComparedFace face= match.face();
                BoundingBox position = face.boundingBox();
                System.out.println("Face at " + position.left().toString()
                        + " " + position.top()
                        + " matches with " + face.confidence().toString()
                        + "% confidence.");

            }
            List<ComparedFace> uncompared = compareFacesResult.unmatchedFaces();

            System.out.println("There was " + uncompared.size()
                    + " face(s) that did not match");
            System.out.println("Source image rotation: " + compareFacesResult.sourceImageOrientationCorrection());
            System.out.println("target image rotation: " + compareFacesResult.targetImageOrientationCorrection());

        } catch(RekognitionException | FileNotFoundException e) {
            System.out.println("Failed to load source image " + sourceImage);
            System.exit(1);
        }
        // snippet-end:[rekognition.java2.compare_faces.main]
    }
}

您可以在此处找到 Java V2 的完整示例:https ://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/rekognition

于 2020-06-18T13:25:42.920 回答