Search in sources :

Example 1 with DetectProtectiveEquipmentResponse

use of software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentResponse 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 2 with DetectProtectiveEquipmentResponse

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

Aggregations

SdkBytes (software.amazon.awssdk.core.SdkBytes)2 DetectProtectiveEquipmentResponse (software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentResponse)2 Image (software.amazon.awssdk.services.rekognition.model.Image)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Region (software.amazon.awssdk.regions.Region)1 RekognitionClient (software.amazon.awssdk.services.rekognition.RekognitionClient)1 software.amazon.awssdk.services.rekognition.model (software.amazon.awssdk.services.rekognition.model)1 DetectProtectiveEquipmentRequest (software.amazon.awssdk.services.rekognition.model.DetectProtectiveEquipmentRequest)1 EquipmentDetection (software.amazon.awssdk.services.rekognition.model.EquipmentDetection)1 ProtectiveEquipmentBodyPart (software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentBodyPart)1 ProtectiveEquipmentPerson (software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentPerson)1 ProtectiveEquipmentSummarizationAttributes (software.amazon.awssdk.services.rekognition.model.ProtectiveEquipmentSummarizationAttributes)1 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1