Search in sources :

Example 1 with IndexFacesRequest

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

the class AddFacesToCollection method addToCollection.

// snippet-start:[rekognition.java2.add_faces_collection.main]
public static void addToCollection(RekognitionClient rekClient, String collectionId, String sourceImage) {
    try {
        InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        Image souImage = Image.builder().bytes(sourceBytes).build();
        IndexFacesRequest facesRequest = IndexFacesRequest.builder().collectionId(collectionId).image(souImage).maxFaces(1).qualityFilter(QualityFilter.AUTO).detectionAttributes(Attribute.DEFAULT).build();
        IndexFacesResponse facesResponse = rekClient.indexFaces(facesRequest);
        // Display the results.
        System.out.println("Results for the image");
        System.out.println("\n Faces indexed:");
        List<FaceRecord> faceRecords = facesResponse.faceRecords();
        for (FaceRecord faceRecord : faceRecords) {
            System.out.println("  Face ID: " + faceRecord.face().faceId());
            System.out.println("  Location:" + faceRecord.faceDetail().boundingBox().toString());
        }
        List<UnindexedFace> unindexedFaces = facesResponse.unindexedFaces();
        System.out.println("Faces not indexed:");
        for (UnindexedFace unindexedFace : unindexedFaces) {
            System.out.println("  Location:" + unindexedFace.faceDetail().boundingBox().toString());
            System.out.println("  Reasons:");
            for (Reason reason : unindexedFace.reasons()) {
                System.out.println("Reason:  " + reason);
            }
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : IndexFacesRequest(software.amazon.awssdk.services.rekognition.model.IndexFacesRequest) 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) FileInputStream(java.io.FileInputStream) Reason(software.amazon.awssdk.services.rekognition.model.Reason) SdkBytes(software.amazon.awssdk.core.SdkBytes) IndexFacesResponse(software.amazon.awssdk.services.rekognition.model.IndexFacesResponse) FaceRecord(software.amazon.awssdk.services.rekognition.model.FaceRecord) UnindexedFace(software.amazon.awssdk.services.rekognition.model.UnindexedFace)

Aggregations

FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 FaceRecord (software.amazon.awssdk.services.rekognition.model.FaceRecord)1 Image (software.amazon.awssdk.services.rekognition.model.Image)1 IndexFacesRequest (software.amazon.awssdk.services.rekognition.model.IndexFacesRequest)1 IndexFacesResponse (software.amazon.awssdk.services.rekognition.model.IndexFacesResponse)1 Reason (software.amazon.awssdk.services.rekognition.model.Reason)1 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)1 UnindexedFace (software.amazon.awssdk.services.rekognition.model.UnindexedFace)1