本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon Rekognition Image 操作可以分析做為映像位元組提供的映像,或存放在 Amazon S3 儲存貯體中的映像。
這些主題提供範例,其示範透過使用從本機檔案系統載入的檔案,將映像位元組提供給 Amazon Rekognition Image API 操作。您可以使用映像輸入參數,將映像位元組傳遞至 Amazon Rekognition API 操作。在 Image
內,指定 Bytes
屬性,以傳遞 base64 編碼的映像位元組。
透過使用 Bytes
輸入參數傳遞至 Amazon Rekognition API 操作的映像,必須經過 Base64 編碼。這些範例使用的 AWS SDK,會自動以 base64 編碼映像。在呼叫 Amazon Rekognition API 操作前,您不需要再編碼映像位元組。如需詳細資訊,請參閱 映像規格。
在這個範例中,JSON 請求 DetectLabels
,來源映像位元組以 Bytes
輸入參數傳遞。
{
"Image": {
"Bytes": "/9j/4AAQSk....."
},
"MaxLabels": 10,
"MinConfidence": 77
}
下列範例使用各種 AWS SDKs和 AWS CLI 來呼叫 DetectLabels
。如需有關 DetectLabels
操作回應的資訊,請參閱 DetectLabels 回應。
對於使用者端 JavaScript 範例,請參閱 使用於 JavaScript。
偵測本機映像中的標籤
如果您尚未執行:
建立或更新具有
AmazonRekognitionFullAccess
和AmazonS3ReadOnlyAccess
許可的使用者。如需詳細資訊,請參閱步驟 1:設定 AWS 帳戶並建立使用者。安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 和 SDK AWS SDKs。
使用下列範例來呼叫
DetectLabels
操作。下列 Java 範例示範如何從本機檔案系統載入映像,並運用 detectLabels
AWS SDK 操作來偵測標籤。將 photo
的值變更為某個映像檔案的路徑和檔案名稱 (.jpg 或 .png 格式)。//Copyright 2018 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 aws.example.rekognition.image; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.List; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.AmazonClientException; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.DetectLabelsRequest; import com.amazonaws.services.rekognition.model.DetectLabelsResult; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.Label; import com.amazonaws.util.IOUtils; public class DetectLabelsLocalFile { public static void main(String[] args) throws Exception { String photo="input.jpg"; ByteBuffer imageBytes; try (InputStream inputStream = new FileInputStream(new File(photo))) { imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream)); } AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectLabelsRequest request = new DetectLabelsRequest() .withImage(new Image() .withBytes(imageBytes)) .withMaxLabels(10) .withMinConfidence(77F); try { DetectLabelsResult result = rekognitionClient.detectLabels(request); List <Label> labels = result.getLabels(); System.out.println("Detected labels for " + photo); for (Label label: labels) { System.out.println(label.getName() + ": " + label.getConfidence().toString()); } } catch (AmazonRekognitionException e) { e.printStackTrace(); } } }