Search in sources :

Example 11 with SdkBytes

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

the class DetectModerationLabels method detectModLabels.

// snippet-start:[rekognition.java2.detect_mod_labels.main]
public static void detectModLabels(RekognitionClient rekClient, String sourceImage) {
    try {
        InputStream sourceStream = new FileInputStream(sourceImage);
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectModerationLabelsRequest moderationLabelsRequest = DetectModerationLabelsRequest.builder().image(souImage).minConfidence(60F).build();
        DetectModerationLabelsResponse moderationLabelsResponse = rekClient.detectModerationLabels(moderationLabelsRequest);
        // Display the results
        List<ModerationLabel> labels = moderationLabelsResponse.moderationLabels();
        System.out.println("Detected labels for image");
        for (ModerationLabel label : labels) {
            System.out.println("Label: " + label.name() + "\n Confidence: " + label.confidence().toString() + "%" + "\n Parent:" + label.parentName());
        }
    } catch (RekognitionException | FileNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : ModerationLabel(software.amazon.awssdk.services.rekognition.model.ModerationLabel) SdkBytes(software.amazon.awssdk.core.SdkBytes) 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) DetectModerationLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsResponse) FileInputStream(java.io.FileInputStream) DetectModerationLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsRequest)

Example 12 with SdkBytes

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

the class PPEBoundingBoxFrame method displayGear.

// snippet-start:[rekognition.java2.display_mask.main]
public static void displayGear(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
    float confidence = 80;
    byte[] data = getObjectBytes(s3, bucketName, sourceImage);
    InputStream is = new ByteArrayInputStream(data);
    try {
        ProtectiveEquipmentSummarizationAttributes summarizationAttributes = ProtectiveEquipmentSummarizationAttributes.builder().minConfidence(70F).requiredEquipmentTypesWithStrings("FACE_COVER").build();
        SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
        image = ImageIO.read(sourceBytes.asInputStream());
        // Create an Image object for the source image.
        software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectProtectiveEquipmentRequest request = DetectProtectiveEquipmentRequest.builder().image(souImage).summarizationAttributes(summarizationAttributes).build();
        DetectProtectiveEquipmentResponse result = rekClient.detectProtectiveEquipment(request);
        // Create frame and panel.
        JFrame frame = new JFrame("Detect PPE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PPEBoundingBoxFrame panel = new PPEBoundingBoxFrame(result, image, confidence);
        panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale));
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    } catch (RekognitionException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) SdkBytes(software.amazon.awssdk.core.SdkBytes) Image(software.amazon.awssdk.services.rekognition.model.Image) software.amazon.awssdk.services.rekognition.model(software.amazon.awssdk.services.rekognition.model) DetectProtectiveEquipmentResponse(software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentResponse)

Example 13 with SdkBytes

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

the class SearchFaceMatchingImageCollection method searchFaceInCollection.

// snippet-start:[rekognition.java2.search_faces_collection.main]
public static void searchFaceInCollection(RekognitionClient rekClient, String collectionId, String sourceImage) {
    try {
        InputStream sourceStream = new FileInputStream(new File(sourceImage));
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        Image souImage = Image.builder().bytes(sourceBytes).build();
        SearchFacesByImageRequest facesByImageRequest = SearchFacesByImageRequest.builder().image(souImage).maxFaces(10).faceMatchThreshold(70F).collectionId(collectionId).build();
        SearchFacesByImageResponse imageResponse = rekClient.searchFacesByImage(facesByImageRequest);
        // Display the results.
        System.out.println("Faces matching in the collection");
        List<FaceMatch> faceImageMatches = imageResponse.faceMatches();
        for (FaceMatch face : faceImageMatches) {
            System.out.println("The similarity level is  " + face.similarity());
            System.out.println();
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SearchFacesByImageRequest(software.amazon.awssdk.services.rekognition.model.SearchFacesByImageRequest) FileNotFoundException(java.io.FileNotFoundException) SearchFacesByImageResponse(software.amazon.awssdk.services.rekognition.model.SearchFacesByImageResponse) Image(software.amazon.awssdk.services.rekognition.model.Image) File(java.io.File) FaceMatch(software.amazon.awssdk.services.rekognition.model.FaceMatch) FileInputStream(java.io.FileInputStream)

Example 14 with SdkBytes

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

the class DetectFaces method detectFacesinImage.

// snippet-start:[rekognition.java2.detect_faces.main]
public static void detectFacesinImage(RekognitionClient rekClient, String sourceImage) {
    try {
        InputStream sourceStream = new FileInputStream(new File(sourceImage));
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        // Create an Image object for the source image.
        Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectFacesRequest facesRequest = DetectFacesRequest.builder().attributes(Attribute.ALL).image(souImage).build();
        DetectFacesResponse facesResponse = rekClient.detectFaces(facesRequest);
        List<FaceDetail> faceDetails = facesResponse.faceDetails();
        for (FaceDetail face : faceDetails) {
            AgeRange ageRange = face.ageRange();
            System.out.println("The detected face is estimated to be between " + ageRange.low().toString() + " and " + ageRange.high().toString() + " years old.");
            System.out.println("There is a smile : " + face.smile().value().toString());
        }
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AgeRange(software.amazon.awssdk.services.rekognition.model.AgeRange) FileNotFoundException(java.io.FileNotFoundException) Image(software.amazon.awssdk.services.rekognition.model.Image) FileInputStream(java.io.FileInputStream) SdkBytes(software.amazon.awssdk.core.SdkBytes) FaceDetail(software.amazon.awssdk.services.rekognition.model.FaceDetail) DetectFacesResponse(software.amazon.awssdk.services.rekognition.model.DetectFacesResponse) File(java.io.File) DetectFacesRequest(software.amazon.awssdk.services.rekognition.model.DetectFacesRequest)

Example 15 with SdkBytes

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

the class DetectPPE method displayGear.

// snippet-start:[rekognition.java2.detect_ppe.main]
public static void displayGear(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
    byte[] data = getObjectBytes(s3, bucketName, sourceImage);
    InputStream is = new ByteArrayInputStream(data);
    try {
        ProtectiveEquipmentSummarizationAttributes summarizationAttributes = ProtectiveEquipmentSummarizationAttributes.builder().minConfidence(80F).requiredEquipmentTypesWithStrings("FACE_COVER", "HAND_COVER", "HEAD_COVER").build();
        SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
        software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectProtectiveEquipmentRequest request = DetectProtectiveEquipmentRequest.builder().image(souImage).summarizationAttributes(summarizationAttributes).build();
        DetectProtectiveEquipmentResponse result = rekClient.detectProtectiveEquipment(request);
        List<ProtectiveEquipmentPerson> persons = result.persons();
        for (ProtectiveEquipmentPerson person : persons) {
            System.out.println("ID: " + person.id());
            List<ProtectiveEquipmentBodyPart> bodyParts = person.bodyParts();
            if (bodyParts.isEmpty()) {
                System.out.println("\tNo body parts detected");
            } else
                for (ProtectiveEquipmentBodyPart bodyPart : bodyParts) {
                    System.out.println("\t" + bodyPart.name() + ". Confidence: " + bodyPart.confidence().toString());
                    List<EquipmentDetection> equipmentDetections = bodyPart.equipmentDetections();
                    if (equipmentDetections.isEmpty()) {
                        System.out.println("\t\tNo PPE Detected on " + bodyPart.name());
                    } else {
                        for (EquipmentDetection item : equipmentDetections) {
                            System.out.println("\t\tItem: " + item.type() + ". Confidence: " + item.confidence().toString());
                            System.out.println("\t\tCovers body part: " + item.coversBodyPart().value().toString() + ". Confidence: " + item.coversBodyPart().confidence().toString());
                            System.out.println("\t\tBounding Box");
                            BoundingBox box = item.boundingBox();
                            System.out.println("\t\tLeft: " + box.left().toString());
                            System.out.println("\t\tTop: " + box.top().toString());
                            System.out.println("\t\tWidth: " + box.width().toString());
                            System.out.println("\t\tHeight: " + box.height().toString());
                            System.out.println("\t\tConfidence: " + item.confidence().toString());
                            System.out.println();
                        }
                    }
                }
        }
        System.out.println("Person ID Summary\n-----------------");
        DisplaySummary("With required equipment", result.summary().personsWithRequiredEquipment());
        DisplaySummary("Without required equipment", result.summary().personsWithoutRequiredEquipment());
        DisplaySummary("Indeterminate", result.summary().personsIndeterminate());
    } catch (RekognitionException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) SdkBytes(software.amazon.awssdk.core.SdkBytes) ByteArrayInputStream(java.io.ByteArrayInputStream) Image(software.amazon.awssdk.services.rekognition.model.Image) ProtectiveEquipmentPerson(software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentPerson) software.amazon.awssdk.services.rekognition.model(software.amazon.awssdk.services.rekognition.model) List(java.util.List)

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