To detect Personal Protective Equipment (PPE) on persons in an image, use the DetectProtectiveEquipment non-storage API operation.
You can provide the input image as an image byte array (base64-encoded image bytes) or as an Amazon S3 object, by using the AWS SDK or the AWS Command Line Interface (AWS CLI). These examples use an image stored in an Amazon S3 bucket. For more information, see Working with images.
To detect PPE on persons in an image
If you haven't already:
Create or update a user with
AmazonRekognitionFullAccess
andAmazonS3ReadOnlyAccess
permissions. For more information, see Step 1: Set up an AWS account and create a User.Install and configure the AWS CLI and the AWS SDKs. For more information, see Step 2: Set up the AWS CLI and AWS SDKs.
-
Upload an image (that contains one or more persons wearing PPE) to your S3 bucket.
For instructions, see Uploading Objects into Amazon S3 in the Amazon Simple Storage Service User Guide.
Use the following examples to call the
DetectProtectiveEquipment
operation. For information about displaying bounding boxes in an image, see Displaying bounding boxes.This example displays information about the PPE items detected on persons detected in an image.
Change the value of
amzn-s3-demo-bucket
to the name of the Amazon S3 bucket that contains your image. Change the value ofphoto
to your image file name.//Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) package com.amazonaws.samples; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.ProtectiveEquipmentBodyPart; import com.amazonaws.services.rekognition.model.S3Object; import com.amazonaws.services.rekognition.model.ProtectiveEquipmentPerson; import com.amazonaws.services.rekognition.model.ProtectiveEquipmentSummarizationAttributes; import java.util.List; import com.amazonaws.services.rekognition.model.BoundingBox; import com.amazonaws.services.rekognition.model.DetectProtectiveEquipmentRequest; import com.amazonaws.services.rekognition.model.DetectProtectiveEquipmentResult; import com.amazonaws.services.rekognition.model.EquipmentDetection; public class DetectPPE { public static void main(String[] args) throws Exception { String photo = "photo"; String bucket = "bucket"; AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); ProtectiveEquipmentSummarizationAttributes summaryAttributes = new ProtectiveEquipmentSummarizationAttributes() .withMinConfidence(80F) .withRequiredEquipmentTypes("FACE_COVER", "HAND_COVER", "HEAD_COVER"); DetectProtectiveEquipmentRequest request = new DetectProtectiveEquipmentRequest() .withImage(new Image() .withS3Object(new S3Object() .withName(photo).withBucket(bucket))) .withSummarizationAttributes(summaryAttributes); try { System.out.println("Detected PPE for people in image " + photo); System.out.println("Detected people\n---------------"); DetectProtectiveEquipmentResult result = rekognitionClient.detectProtectiveEquipment(request); List <ProtectiveEquipmentPerson> persons = result.getPersons(); for (ProtectiveEquipmentPerson person: persons) { System.out.println("ID: " + person.getId()); List<ProtectiveEquipmentBodyPart> bodyParts=person.getBodyParts(); if (bodyParts.isEmpty()){ System.out.println("\tNo body parts detected"); } else for (ProtectiveEquipmentBodyPart bodyPart: bodyParts) { System.out.println("\t" + bodyPart.getName() + ". Confidence: " + bodyPart.getConfidence().toString()); List<EquipmentDetection> equipmentDetections=bodyPart.getEquipmentDetections(); if (equipmentDetections.isEmpty()){ System.out.println("\t\tNo PPE Detected on " + bodyPart.getName()); } else { for (EquipmentDetection item: equipmentDetections) { System.out.println("\t\tItem: " + item.getType() + ". Confidence: " + item.getConfidence().toString()); System.out.println("\t\tCovers body part: " + item.getCoversBodyPart().getValue().toString() + ". Confidence: " + item.getCoversBodyPart().getConfidence().toString()); System.out.println("\t\tBounding Box"); BoundingBox box =item.getBoundingBox(); System.out.println("\t\tLeft: " +box.getLeft().toString()); System.out.println("\t\tTop: " + box.getTop().toString()); System.out.println("\t\tWidth: " + box.getWidth().toString()); System.out.println("\t\tHeight: " + box.getHeight().toString()); System.out.println("\t\tConfidence: " + item.getConfidence().toString()); System.out.println(); } } } } System.out.println("Person ID Summary\n-----------------"); //List<Integer> list=; DisplaySummary("With required equipment", result.getSummary().getPersonsWithRequiredEquipment()); DisplaySummary("Without required equipment", result.getSummary().getPersonsWithoutRequiredEquipment()); DisplaySummary("Indeterminate", result.getSummary().getPersonsIndeterminate()); } catch(AmazonRekognitionException e) { e.printStackTrace(); } } static void DisplaySummary(String summaryType,List<Integer> idList) { System.out.print(summaryType + "\n\tIDs "); if (idList.size()==0) { System.out.println("None"); } else { int count=0; for (Integer id: idList ) { if (count++ == idList.size()-1) { System.out.println(id.toString()); } else { System.out.print(id.toString() + ", "); } } } System.out.println(); } }