Search in sources :

Example 26 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes 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)

Example 27 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes 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)

Example 28 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class DetectText method detectTextLabels.

// snippet-start:[rekognition.java2.detect_text.main]
public static void detectTextLabels(RekognitionClient rekClient, String sourceImage) {
    try {
        InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        // Create an Image object for the source image
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectTextRequest textRequest = DetectTextRequest.builder().image(souImage).build();
        DetectTextResponse textResponse = rekClient.detectText(textRequest);
        List<TextDetection> textCollection = textResponse.textDetections();
        System.out.println("Detected lines and words");
        for (TextDetection text : textCollection) {
            System.out.println("Detected: " + text.detectedText());
            System.out.println("Confidence: " + text.confidence().toString());
            System.out.println("Id : " + text.id());
            System.out.println("Parent Id: " + text.parentId());
            System.out.println("Type: " + text.type());
            System.out.println();
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : TextDetection(software.amazon.awssdk.services.rekognition.model.TextDetection) SdkBytes(software.amazon.awssdk.core.SdkBytes) DetectTextRequest(software.amazon.awssdk.services.rekognition.model.DetectTextRequest) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DetectTextResponse(software.amazon.awssdk.services.rekognition.model.DetectTextResponse) FileNotFoundException(java.io.FileNotFoundException) Image(software.amazon.awssdk.services.rekognition.model.Image) FileInputStream(java.io.FileInputStream)

Example 29 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class RotateImage method recognizeAllCelebrities.

// snippet-start:[rekognition.java2.recognize_image_orientation.main]
public static void recognizeAllCelebrities(RekognitionClient rekClient, String sourceImage) {
    try {
        BufferedImage image = null;
        InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        image = ImageIO.read(sourceBytes.asInputStream());
        int height = image.getHeight();
        int width = image.getWidth();
        Image souImage = Image.builder().bytes(sourceBytes).build();
        RecognizeCelebritiesRequest request = RecognizeCelebritiesRequest.builder().image(souImage).build();
        RecognizeCelebritiesResponse result = rekClient.recognizeCelebrities(request);
        List<Celebrity> celebs = result.celebrityFaces();
        System.out.println(celebs.size() + " celebrity(s) were recognized.\n");
        for (Celebrity celebrity : celebs) {
            System.out.println("Celebrity recognized: " + celebrity.name());
            System.out.println("Celebrity ID: " + celebrity.id());
            ComparedFace face = celebrity.face();
            ShowBoundingBoxPositions(height, width, face.boundingBox(), result.orientationCorrectionAsString());
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) BufferedImage(java.awt.image.BufferedImage) Image(software.amazon.awssdk.services.rekognition.model.Image) ComparedFace(software.amazon.awssdk.services.rekognition.model.ComparedFace) BufferedImage(java.awt.image.BufferedImage) RecognizeCelebritiesResponse(software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) RecognizeCelebritiesRequest(software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesRequest) Celebrity(software.amazon.awssdk.services.rekognition.model.Celebrity)

Example 30 with SdkBytes

use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.

the class CreateFunction method createLambdaFunction.

// snippet-start:[lambda.java2.create.main]
public static void createLambdaFunction(LambdaClient awsLambda, String functionName, String filePath, String role, String handler) {
    try {
        InputStream is = new FileInputStream(filePath);
        SdkBytes fileToUpload = SdkBytes.fromInputStream(is);
        FunctionCode code = FunctionCode.builder().zipFile(fileToUpload).build();
        CreateFunctionRequest functionRequest = CreateFunctionRequest.builder().functionName(functionName).description("Created by the Lambda Java API").code(code).handler(handler).runtime(Runtime.JAVA8).role(role).build();
        CreateFunctionResponse functionResponse = awsLambda.createFunction(functionRequest);
        System.out.println("The function ARN is " + functionResponse.functionArn());
    } catch (LambdaException | FileNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : CreateFunctionResponse(software.amazon.awssdk.services.lambda.model.CreateFunctionResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) CreateFunctionRequest(software.amazon.awssdk.services.lambda.model.CreateFunctionRequest) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FunctionCode(software.amazon.awssdk.services.lambda.model.FunctionCode) FileNotFoundException(java.io.FileNotFoundException) LambdaException(software.amazon.awssdk.services.lambda.model.LambdaException) FileInputStream(java.io.FileInputStream)

Aggregations

SdkBytes (software.amazon.awssdk.core.SdkBytes)37 InputStream (java.io.InputStream)14 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)13 Image (software.amazon.awssdk.services.rekognition.model.Image)13 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ByteBuffer (java.nio.ByteBuffer)9 Properties (java.util.Properties)9 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)9 MimeBodyPart (javax.mail.internet.MimeBodyPart)9 MimeMessage (javax.mail.internet.MimeMessage)9 MimeMultipart (javax.mail.internet.MimeMultipart)9 Region (software.amazon.awssdk.regions.Region)9 RawMessage (software.amazon.awssdk.services.ses.model.RawMessage)9 SendRawEmailRequest (software.amazon.awssdk.services.ses.model.SendRawEmailRequest)9 SesException (software.amazon.awssdk.services.ses.model.SesException)9 DataHandler (javax.activation.DataHandler)6 DataSource (javax.activation.DataSource)6