本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
若要偵測映像中人員的個人防護裝備 (PPE),請使用 DetectProtectiveEquipment 的非儲存 API 作業。
您可以使用 AWS SDK 或 AWS Command Line Interface (AWS CLI),以映像位元組陣列 (Base64 編碼映像位元組) 或 Amazon S3 物件的方式提供輸入映像。這些範例使用存放在 Amazon S3 儲存貯體中的映像。如需詳細資訊,請參閱使用映像。
若要偵測映像中人物身上的個人防護裝備
如果您尚未執行:
建立或更新具有
AmazonRekognitionFullAccess
和AmazonS3ReadOnlyAccess
許可的使用者。如需詳細資訊,請參閱步驟 1:設定 AWS 帳戶並建立使用者。安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 和 SDK AWS SDKs。
-
將 (含有一或多個配備個人防護裝備的人員) 的映像上傳至您的 S3 儲存貯體。
如需指示說明,請參閱《Amazon Simple Storage Service 使用者指南》中的上傳物件至 Amazon S3。
使用下列範例來呼叫
DetectProtectiveEquipment
操作。如需有關在映像中顯示邊界邊框的資訊,請參閱 顯示週框方塊。此範例顯示在映像中偵測到的人員身上偵測到的個人防護裝備專案的相關資訊。
將
amzn-s3-demo-bucket
的值變更為包含映像檔案的 Amazon S3 儲存貯體名稱。變更映像檔案名稱的photo
值。//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(); } }