本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon Rekognition Image 可以分析存放在 Amazon S3 儲存貯體中的映像,或做為映像位元組提供的映像。
在本主題中,您會使用 DetectLabels API 操作,來偵測存放在 Amazon S3 儲存貯體之映像 (JPEG 或 PNG) 中的物件、概念與場景。您可以使用映像輸入參數,將映像傳遞至 Amazon Rekognition Image API 操作。在 Image
內,您指定 S3Object 物件屬性以參考存放在 S3 儲存貯體中的映像。存放在 Amazon S3 儲存貯體中的映像位元組,不需要 Base64 編碼。如需詳細資訊,請參閱 映像規格。
範例請求
在此範例中,JSON 要求 DetectLabels
,而來源映像 (input.jpg
) 是從名為 amzn-s3-demo-bucket
的 Amazon S3 儲存貯體載入。含有 S3 物件的 S3 儲存貯體區域必須符合您用於 Amazon Rekognition Image 操作的區域。
{ "Image": { "S3Object": { "Bucket": "
amzn-s3-demo-bucket
", "Name": "input.jpg" } }, "MaxLabels": 10, "MinConfidence": 75 }
下列範例使用各種 AWS SDKs和 AWS CLI 來呼叫 DetectLabels
。如需有關 DetectLabels
操作回應的資訊,請參閱 DetectLabels 回應。
偵測映像中的標籤
如果您尚未執行:
建立或更新具有
AmazonRekognitionFullAccess
和AmazonS3ReadOnlyAccess
許可的使用者。如需詳細資訊,請參閱步驟 1:設定 AWS 帳戶並建立使用者。安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 和 SDK AWS SDKs。請確定您已為呼叫 API 操作的使用者授予程式設計存取的適當權限,請參閱 授與程式設計存取權 以取得如何執行此操作的指示。
-
將包含一個或多個物件的映像 (例如樹、房子和船) 上傳至您的 S3 儲存貯體。映像的格式必須是 .jpg 或 .png 格式。
如需指示說明,請參閱《Amazon Simple Storage Service 使用者指南》中的上傳物件至 Amazon S3。
-
使用下列範例來呼叫
DetectLabels
操作。此範例顯示一份在輸入映像中偵測到的標籤清單。將
bucket
與photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。//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 com.amazonaws.samples; 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.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.services.rekognition.model.S3Object; import java.util.List; public class DetectLabels { public static void main(String[] args) throws Exception { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectLabelsRequest request = new DetectLabelsRequest() .withImage(new Image() .withS3Object(new S3Object() .withName(photo).withBucket(bucket))) .withMaxLabels(10) .withMinConfidence(75F); 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(); } } }
回應範例
DetectLabels
的回應是映像中偵測到的一系列標籤,以及偵測所依據的可信度層級。
當您對映像執行 DetectLabels
作業時,Amazon Rekognition 會傳回類似下列範例回應的輸出。
回應顯示操作偵測到多個標籤,包括人員、車輛和汽車。每個標籤都有一個相關的可信度等級。例如,偵測演算法對於映像中包含人員的可信度為 98.991432%。
回應也包含 Parents
陣列中標籤的上階標籤。例如 Automobile (汽車) 標籤有兩個名為 Vehicle (車輛) 和 Transportation (運輸) 的 父標籤。
常見物件標籤的回應包含輸入映像上標籤位置的週框方塊資訊。例如,人員標籤具有一個實例陣列,其中包含兩個週框方塊。這些是在映像中偵測到的兩個人員位置。
欄位 LabelModelVersion
包含 DetectLabels
所使用之偵測模型的版本編號。
如需使用此 DetectLabels
操作的詳細資訊,請參閱 偵測物件和概念。
{
{
"Labels": [
{
"Name": "Vehicle",
"Confidence": 99.15271759033203,
"Instances": [],
"Parents": [
{
"Name": "Transportation"
}
]
},
{
"Name": "Transportation",
"Confidence": 99.15271759033203,
"Instances": [],
"Parents": []
},
{
"Name": "Automobile",
"Confidence": 99.15271759033203,
"Instances": [],
"Parents": [
{
"Name": "Vehicle"
},
{
"Name": "Transportation"
}
]
},
{
"Name": "Car",
"Confidence": 99.15271759033203,
"Instances": [
{
"BoundingBox": {
"Width": 0.10616336017847061,
"Height": 0.18528179824352264,
"Left": 0.0037978808395564556,
"Top": 0.5039216876029968
},
"Confidence": 99.15271759033203
},
{
"BoundingBox": {
"Width": 0.2429988533258438,
"Height": 0.21577216684818268,
"Left": 0.7309805154800415,
"Top": 0.5251884460449219
},
"Confidence": 99.1286392211914
},
{
"BoundingBox": {
"Width": 0.14233611524105072,
"Height": 0.15528248250484467,
"Left": 0.6494812965393066,
"Top": 0.5333095788955688
},
"Confidence": 98.48368072509766
},
{
"BoundingBox": {
"Width": 0.11086395382881165,
"Height": 0.10271988064050674,
"Left": 0.10355594009160995,
"Top": 0.5354844927787781
},
"Confidence": 96.45606231689453
},
{
"BoundingBox": {
"Width": 0.06254628300666809,
"Height": 0.053911514580249786,
"Left": 0.46083059906959534,
"Top": 0.5573825240135193
},
"Confidence": 93.65448760986328
},
{
"BoundingBox": {
"Width": 0.10105438530445099,
"Height": 0.12226245552301407,
"Left": 0.5743985772132874,
"Top": 0.534368634223938
},
"Confidence": 93.06217193603516
},
{
"BoundingBox": {
"Width": 0.056389667093753815,
"Height": 0.17163699865341187,
"Left": 0.9427769780158997,
"Top": 0.5235804319381714
},
"Confidence": 92.6864013671875
},
{
"BoundingBox": {
"Width": 0.06003860384225845,
"Height": 0.06737709045410156,
"Left": 0.22409997880458832,
"Top": 0.5441341400146484
},
"Confidence": 90.4227066040039
},
{
"BoundingBox": {
"Width": 0.02848697081208229,
"Height": 0.19150497019290924,
"Left": 0.0,
"Top": 0.5107086896896362
},
"Confidence": 86.65286254882812
},
{
"BoundingBox": {
"Width": 0.04067881405353546,
"Height": 0.03428703173995018,
"Left": 0.316415935754776,
"Top": 0.5566273927688599
},
"Confidence": 85.36471557617188
},
{
"BoundingBox": {
"Width": 0.043411049991846085,
"Height": 0.0893595889210701,
"Left": 0.18293385207653046,
"Top": 0.5394920110702515
},
"Confidence": 82.21705627441406
},
{
"BoundingBox": {
"Width": 0.031183116137981415,
"Height": 0.03989990055561066,
"Left": 0.2853088080883026,
"Top": 0.5579366683959961
},
"Confidence": 81.0157470703125
},
{
"BoundingBox": {
"Width": 0.031113790348172188,
"Height": 0.056484755128622055,
"Left": 0.2580395042896271,
"Top": 0.5504819750785828
},
"Confidence": 56.13441467285156
},
{
"BoundingBox": {
"Width": 0.08586374670267105,
"Height": 0.08550430089235306,
"Left": 0.5128012895584106,
"Top": 0.5438792705535889
},
"Confidence": 52.37760925292969
}
],
"Parents": [
{
"Name": "Vehicle"
},
{
"Name": "Transportation"
}
]
},
{
"Name": "Human",
"Confidence": 98.9914321899414,
"Instances": [],
"Parents": []
},
{
"Name": "Person",
"Confidence": 98.9914321899414,
"Instances": [
{
"BoundingBox": {
"Width": 0.19360728561878204,
"Height": 0.2742200493812561,
"Left": 0.43734854459762573,
"Top": 0.35072067379951477
},
"Confidence": 98.9914321899414
},
{
"BoundingBox": {
"Width": 0.03801717236638069,
"Height": 0.06597328186035156,
"Left": 0.9155802130699158,
"Top": 0.5010883808135986
},
"Confidence": 85.02790832519531
}
],
"Parents": []
}
],
"LabelModelVersion": "2.0"
}
}