Search in sources :

Example 1 with CompareFacesMatch

use of software.amazon.awssdk.services.rekognition.model.CompareFacesMatch in project aws-doc-sdk-examples by awsdocs.

the class CompareFaces method compareTwoFaces.

// snippet-start:[rekognition.java2.compare_faces.main]
public static void compareTwoFaces(RekognitionClient rekClient, Float similarityThreshold, String sourceImage, String targetImage) {
    try {
        InputStream sourceStream = new FileInputStream(sourceImage);
        InputStream tarStream = new FileInputStream(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();
        Image tarImage = Image.builder().bytes(targetBytes).build();
        CompareFacesRequest facesRequest = CompareFacesRequest.builder().sourceImage(souImage).targetImage(tarImage).similarityThreshold(similarityThreshold).build();
        // Compare the two images.
        CompareFacesResponse compareFacesResult = rekClient.compareFaces(facesRequest);
        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);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Image(software.amazon.awssdk.services.rekognition.model.Image) CompareFacesMatch(software.amazon.awssdk.services.rekognition.model.CompareFacesMatch) ComparedFace(software.amazon.awssdk.services.rekognition.model.ComparedFace) FileInputStream(java.io.FileInputStream) CompareFacesResponse(software.amazon.awssdk.services.rekognition.model.CompareFacesResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) BoundingBox(software.amazon.awssdk.services.rekognition.model.BoundingBox) CompareFacesRequest(software.amazon.awssdk.services.rekognition.model.CompareFacesRequest)

Aggregations

FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 BoundingBox (software.amazon.awssdk.services.rekognition.model.BoundingBox)1 CompareFacesMatch (software.amazon.awssdk.services.rekognition.model.CompareFacesMatch)1 CompareFacesRequest (software.amazon.awssdk.services.rekognition.model.CompareFacesRequest)1 CompareFacesResponse (software.amazon.awssdk.services.rekognition.model.CompareFacesResponse)1 ComparedFace (software.amazon.awssdk.services.rekognition.model.ComparedFace)1 Image (software.amazon.awssdk.services.rekognition.model.Image)1 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)1