Search in sources :

Example 6 with Image

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

the class DetectLabelsS3 method getLabelsfromImage.

// snippet-start:[rekognition.java2.detect_labels_s3.main]
public static void getLabelsfromImage(RekognitionClient rekClient, String bucket, String image) {
    try {
        S3Object s3Object = S3Object.builder().bucket(bucket).name(image).build();
        Image myImage = Image.builder().s3Object(s3Object).build();
        DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder().image(myImage).maxLabels(10).build();
        DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
        List<Label> labels = labelsResponse.labels();
        System.out.println("Detected labels for the given photo");
        for (Label label : labels) {
            System.out.println(label.name() + ": " + label.confidence().toString());
        }
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) Label(software.amazon.awssdk.services.rekognition.model.Label) DetectLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) Image(software.amazon.awssdk.services.rekognition.model.Image) DetectLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse)

Example 7 with Image

use of software.amazon.awssdk.services.rekognition.model.Image 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)

Example 8 with Image

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

the class DetectCustomLabels method detectImageCustomLabels.

public static void detectImageCustomLabels(RekognitionClient rekClient, String arn, String bucket, String key) {
    try {
        S3Object s3Object = S3Object.builder().bucket(bucket).name(key).build();
        // Create an Image object for the source image
        Image s3Image = Image.builder().s3Object(s3Object).build();
        DetectCustomLabelsRequest detectCustomLabelsRequest = DetectCustomLabelsRequest.builder().image(s3Image).projectVersionArn(arn).build();
        DetectCustomLabelsResponse customLabelsResponse = rekClient.detectCustomLabels(detectCustomLabelsRequest);
        List<CustomLabel> customLabels = customLabelsResponse.customLabels();
        System.out.println("Detected labels for the given photo");
        for (CustomLabel customLabel : customLabels) {
            System.out.println(customLabel.name() + ": " + customLabel.confidence().toString());
        }
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
}
Also used : CustomLabel(software.amazon.awssdk.services.rekognition.model.CustomLabel) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) S3Object(software.amazon.awssdk.services.rekognition.model.S3Object) DetectCustomLabelsResponse(software.amazon.awssdk.services.rekognition.model.DetectCustomLabelsResponse) Image(software.amazon.awssdk.services.rekognition.model.Image) DetectCustomLabelsRequest(software.amazon.awssdk.services.rekognition.model.DetectCustomLabelsRequest)

Example 9 with Image

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

the class AnalyzePhotos method detectLabels.

// Returns a list of GearItem objects that contains PPE information.
public ArrayList<GearItem> detectLabels(byte[] bytes, String key) {
    Region region = Region.US_EAST_2;
    RekognitionClient rekClient = RekognitionClient.builder().region(region).build();
    ArrayList<GearItem> gearList = new ArrayList<>();
    try {
        SdkBytes sourceBytes = SdkBytes.fromByteArray(bytes);
        // Create an Image object for the source image.
        Image souImage = Image.builder().bytes(sourceBytes).build();
        ProtectiveEquipmentSummarizationAttributes summarizationAttributes = ProtectiveEquipmentSummarizationAttributes.builder().minConfidence(80F).requiredEquipmentTypesWithStrings("FACE_COVER", "HAND_COVER", "HEAD_COVER").build();
        DetectProtectiveEquipmentRequest request = DetectProtectiveEquipmentRequest.builder().image(souImage).summarizationAttributes(summarizationAttributes).build();
        DetectProtectiveEquipmentResponse result = rekClient.detectProtectiveEquipment(request);
        List<ProtectiveEquipmentPerson> persons = result.persons();
        // Create a GearItem object.
        GearItem gear;
        for (ProtectiveEquipmentPerson person : persons) {
            List<ProtectiveEquipmentBodyPart> bodyParts = person.bodyParts();
            if (bodyParts.isEmpty()) {
                System.out.println("\tNo body parts detected");
            } else
                for (ProtectiveEquipmentBodyPart bodyPart : bodyParts) {
                    List<EquipmentDetection> equipmentDetections = bodyPart.equipmentDetections();
                    if (equipmentDetections.isEmpty()) {
                        System.out.println("\t\tNo PPE Detected on " + bodyPart.name());
                    } else {
                        for (EquipmentDetection item : equipmentDetections) {
                            gear = new GearItem();
                            gear.setKey(key);
                            String itemType = item.type().toString();
                            String confidence = item.confidence().toString();
                            String myDesc = "Item: " + item.type() + ". Confidence: " + item.confidence().toString();
                            String bodyPartDes = "Covers body part: " + item.coversBodyPart().value().toString() + ". Confidence: " + item.coversBodyPart().confidence().toString();
                            gear.setName(itemType);
                            gear.setConfidence(confidence);
                            gear.setItemDescription(myDesc);
                            gear.setBodyCoverDescription(bodyPartDes);
                            // push the object.
                            gearList.add(gear);
                        }
                    }
                }
        }
        if (gearList.isEmpty())
            return null;
        else
            return gearList;
    } catch (RekognitionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : ProtectiveEquipmentSummarizationAttributes(software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentSummarizationAttributes) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) ArrayList(java.util.ArrayList) ProtectiveEquipmentBodyPart(software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentBodyPart) Image(software.amazon.awssdk.services.rekognition.model.Image) DetectProtectiveEquipmentRequest(software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentRequest) SdkBytes(software.amazon.awssdk.core.SdkBytes) EquipmentDetection(software.amazon.awssdk.services.rekognition.model.EquipmentDetection) ProtectiveEquipmentPerson(software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentPerson) Region(software.amazon.awssdk.regions.Region) RekognitionClient(software.amazon.awssdk.services.rekognition.RekognitionClient) ArrayList(java.util.ArrayList) List(java.util.List) DetectProtectiveEquipmentResponse(software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentResponse)

Example 10 with Image

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

the class DisplayFacesFrame method displayAllFaces.

// snippet-start:[rekognition.java2.display_faces.main]
public static void displayAllFaces(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
    int height = 0;
    int width = 0;
    byte[] data = getObjectBytes(s3, bucketName, sourceImage);
    InputStream is = new ByteArrayInputStream(data);
    try {
        SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
        image = ImageIO.read(sourceBytes.asInputStream());
        width = image.getWidth();
        height = image.getHeight();
        // Create an Image object for the source image
        software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
        DetectFacesRequest facesRequest = DetectFacesRequest.builder().attributes(Attribute.ALL).image(souImage).build();
        result = rekClient.detectFaces(facesRequest);
        // Show the bounding box info for each face.
        List<FaceDetail> faceDetails = result.faceDetails();
        for (FaceDetail face : faceDetails) {
            BoundingBox box = face.boundingBox();
            float left = width * box.left();
            float top = height * box.top();
            System.out.println("Face:");
            System.out.println("Left: " + (int) left);
            System.out.println("Top: " + (int) top);
            System.out.println("Face Width: " + (int) (width * box.width()));
            System.out.println("Face Height: " + (int) (height * box.height()));
            System.out.println();
        }
        // Create the frame and panel.
        JFrame frame = new JFrame("RotateImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DisplayFacesFrame panel = new DisplayFacesFrame(image);
        panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale));
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    } catch (RekognitionException | FileNotFoundException e) {
        System.out.println(e.getMessage());
        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)

Aggregations

Image (software.amazon.awssdk.services.rekognition.model.Image)15 SdkBytes (software.amazon.awssdk.core.SdkBytes)13 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)12 InputStream (java.io.InputStream)9 FileInputStream (java.io.FileInputStream)8 FileNotFoundException (java.io.FileNotFoundException)8 software.amazon.awssdk.services.rekognition.model (software.amazon.awssdk.services.rekognition.model)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 File (java.io.File)2 List (java.util.List)2 Celebrity (software.amazon.awssdk.services.rekognition.model.Celebrity)2 ComparedFace (software.amazon.awssdk.services.rekognition.model.ComparedFace)2 DetectLabelsRequest (software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest)2 DetectLabelsResponse (software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse)2 DetectProtectiveEquipmentResponse (software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentResponse)2 Label (software.amazon.awssdk.services.rekognition.model.Label)2 ProtectiveEquipmentPerson (software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentPerson)2 RecognizeCelebritiesRequest (software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesRequest)2 RecognizeCelebritiesResponse (software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesResponse)2 S3Object (software.amazon.awssdk.services.rekognition.model.S3Object)2