本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在映像中偵測標籤
您可以使用 DetectLabel 操作來偵測映像中的標籤 (物件和概念),並擷取有關映像屬性的資訊。映像屬性包括前景和背景顏色等屬性,以及映像的銳利度、亮度和對比度。您可以只檢索映像中的標籤,只檢索映像的屬性,或兩者兼而有之。如需範例,請參閱「分析存放在 Amazon S3 儲存貯體中的映像」。
下列範例使用各種 AWS SDKs和 AWS CLI 來呼叫 DetectLabels
。如需有關 DetectLabels
操作回應的資訊,請參閱 DetectLabels 回應。
偵測映像中的標籤
-
如果您尚未執行:
建立或更新具有 AmazonRekognitionFullAccess
和 AmazonS3ReadOnlyAccess
許可的使用者。如需詳細資訊,請參閱步驟 1:設定AWS帳戶並建立使用者。
安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 以及 AWS SDKs。
-
將包含一個或多個物件的映像 (例如樹、房子和船) 上傳至您的 S3 儲存貯體。映像的格式必須是 .jpg 或 .png 格式。
如需指示說明,請參閱《Amazon Simple Storage Service 使用者指南》中的上傳物件至 Amazon S3。
-
使用下列範例來呼叫 DetectLabels
操作。
- Java
-
此範例顯示一份在輸入映像中偵測到的標籤清單。將 bucket
與 photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。
package com.amazonaws.samples;
import java.util.List;
import com.amazonaws.services.rekognition.model.BoundingBox;
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.Instance;
import com.amazonaws.services.rekognition.model.Label;
import com.amazonaws.services.rekognition.model.Parent;
import com.amazonaws.services.rekognition.model.S3Object;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.AmazonRekognitionException;
public class DetectLabels {
public static void main(String[] args) throws Exception {
String photo = "photo";
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 + "\n");
for (Label label : labels) {
System.out.println("Label: " + label.getName());
System.out.println("Confidence: " + label.getConfidence().toString() + "\n");
List<Instance> instances = label.getInstances();
System.out.println("Instances of " + label.getName());
if (instances.isEmpty()) {
System.out.println(" " + "None");
} else {
for (Instance instance : instances) {
System.out.println(" Confidence: " + instance.getConfidence().toString());
System.out.println(" Bounding box: " + instance.getBoundingBox().toString());
}
}
System.out.println("Parent labels for " + label.getName() + ":");
List<Parent> parents = label.getParents();
if (parents.isEmpty()) {
System.out.println(" None");
} else {
for (Parent parent : parents) {
System.out.println(" " + parent.getName());
}
}
System.out.println("--------------------");
System.out.println();
}
} catch (AmazonRekognitionException e) {
e.printStackTrace();
}
}
}
- AWS CLI
-
此範例顯示 detect-labels
CLI 操作的 JSON 輸出。將 bucket
與 photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。使用您開發人員設定檔的名稱取代 profile-name
的值。
aws rekognition detect-labels --image '{ "S3Object": { "Bucket": "bucket-name", "Name": "file-name" } }' \
--features GENERAL_LABELS IMAGE_PROPERTIES \
--settings '{"ImageProperties": {"MaxDominantColors":1}, {"GeneralLabels":{"LabelInclusionFilters":["Cat"]}}}' \
--profile profile-name \
--region us-east-1
如果您在 Windows 裝置上存取 CLI,請使用雙引號而非單引號,並以反斜線 (即\) 替代內部雙引號,以解決您可能遇到的任何剖析器錯誤。例如,請參閱下列內容:
aws rekognition detect-labels --image "{\"S3Object\":{\"Bucket\":\"bucket-name\",\"Name\":\"file-name\"}}" --features GENERAL_LABELS IMAGE_PROPERTIES \
--settings "{\"GeneralLabels\":{\"LabelInclusionFilters\":[\"Car\"]}}" --profile profile-name --region us-east-1
- Python
此範例顯示在輸入映像中偵測到的標籤。在函數 main
中,將 bucket
與 photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。將建立 Rekognition 工作階段的行中 profile_name
值取代為您開發人員設定檔的名稱。
#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.)
import boto3
def detect_labels(photo, bucket):
session = boto3.Session(profile_name='profile-name')
client = session.client('rekognition')
response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
MaxLabels=10,
# Uncomment to use image properties and filtration settings
#Features=["GENERAL_LABELS", "IMAGE_PROPERTIES"],
#Settings={"GeneralLabels": {"LabelInclusionFilters":["Cat"]},
# "ImageProperties": {"MaxDominantColors":10}}
)
print('Detected labels for ' + photo)
print()
for label in response['Labels']:
print("Label: " + label['Name'])
print("Confidence: " + str(label['Confidence']))
print("Instances:")
for instance in label['Instances']:
print(" Bounding box")
print(" Top: " + str(instance['BoundingBox']['Top']))
print(" Left: " + str(instance['BoundingBox']['Left']))
print(" Width: " + str(instance['BoundingBox']['Width']))
print(" Height: " + str(instance['BoundingBox']['Height']))
print(" Confidence: " + str(instance['Confidence']))
print()
print("Parents:")
for parent in label['Parents']:
print(" " + parent['Name'])
print("Aliases:")
for alias in label['Aliases']:
print(" " + alias['Name'])
print("Categories:")
for category in label['Categories']:
print(" " + category['Name'])
print("----------")
print()
if "ImageProperties" in str(response):
print("Background:")
print(response["ImageProperties"]["Background"])
print()
print("Foreground:")
print(response["ImageProperties"]["Foreground"])
print()
print("Quality:")
print(response["ImageProperties"]["Quality"])
print()
return len(response['Labels'])
def main():
photo = 'photo-name'
bucket = 'amzn-s3-demo-bucket'
label_count = detect_labels(photo, bucket)
print("Labels detected: " + str(label_count))
if __name__ == "__main__":
main()
- .NET
-
此範例顯示一份在輸入映像中偵測到的標籤清單。將 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.)
using System;
using Amazon.Rekognition;
using Amazon.Rekognition.Model;
public class DetectLabels
{
public static void Example()
{
String photo = "input.jpg";
String bucket = "amzn-s3-demo-bucket";
AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient();
DetectLabelsRequest detectlabelsRequest = new DetectLabelsRequest()
{
Image = new Image()
{
S3Object = new S3Object()
{
Name = photo,
Bucket = bucket
},
},
MaxLabels = 10,
MinConfidence = 75F
};
try
{
DetectLabelsResponse detectLabelsResponse = rekognitionClient.DetectLabels(detectlabelsRequest);
Console.WriteLine("Detected labels for " + photo);
foreach (Label label in detectLabelsResponse.Labels)
Console.WriteLine("{0}: {1}", label.Name, label.Confidence);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
- Ruby
-
此範例顯示一份在輸入映像中偵測到的標籤清單。將 bucket
與 photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。
# Add to your Gemfile
# gem 'aws-sdk-rekognition'
require 'aws-sdk-rekognition'
credentials = Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'],
ENV['AWS_SECRET_ACCESS_KEY']
)
bucket = 'bucket' # the bucket name without s3://
photo = 'photo' # the name of file
client = Aws::Rekognition::Client.new credentials: credentials
attrs = {
image: {
s3_object: {
bucket: bucket,
name: photo
},
},
max_labels: 10
}
response = client.detect_labels attrs
puts "Detected labels for: #{photo}"
response.labels.each do |label|
puts "Label: #{label.name}"
puts "Confidence: #{label.confidence}"
puts "Instances:"
label['instances'].each do |instance|
box = instance['bounding_box']
puts " Bounding box:"
puts " Top: #{box.top}"
puts " Left: #{box.left}"
puts " Width: #{box.width}"
puts " Height: #{box.height}"
puts " Confidence: #{instance.confidence}"
end
puts "Parents:"
label.parents.each do |parent|
puts " #{parent.name}"
end
puts "------------"
puts ""
end
- Node.js
-
此範例顯示一份在輸入映像中偵測到的標籤清單。將 bucket
與 photo
的數值取代為您在步驟 2 中所使用的 Amazon S3 儲存貯體名稱與映像名稱。將建立 Rekognition 工作階段的行中 profile_name
值取代為您開發人員設定檔的名稱。
如果您使用 TypeScript 定義,可能需要使用 import AWS from 'aws-sdk'
而不是 const
AWS = require('aws-sdk')
,以便使用 Node.js 執行該程序。您可以查閱適用於 Javascript 的AWS
SDK,以獲取更多詳細資訊。視您設定組態的方式而定,您可能還需要使用 AWS.config.update({region:region
});
來指定您所在的區域。
// Load the SDK
var AWS = require('aws-sdk');
const bucket = 'bucket-name' // the bucketname without s3://
const photo = 'image-name' // the name of file
var credentials = new AWS.SharedIniFileCredentials({profile: 'profile-name'});
AWS.config.credentials = credentials;
AWS.config.update({region:'region-name'});
const client = new AWS.Rekognition();
const params = {
Image: {
S3Object: {
Bucket: bucket,
Name: photo
},
},
MaxLabels: 10
}
client.detectLabels(params, function(err, response) {
if (err) {
console.log(err, err.stack); // if an error occurred
} else {
console.log(`Detected labels for: ${photo}`)
response.Labels.forEach(label => {
console.log(`Label: ${label.Name}`)
console.log(`Confidence: ${label.Confidence}`)
console.log("Instances:")
label.Instances.forEach(instance => {
let box = instance.BoundingBox
console.log(" Bounding box:")
console.log(` Top: ${box.Top}`)
console.log(` Left: ${box.Left}`)
console.log(` Width: ${box.Width}`)
console.log(` Height: ${box.Height}`)
console.log(` Confidence: ${instance.Confidence}`)
})
console.log("Parents:")
label.Parents.forEach(parent => {
console.log(` ${parent.Name}`)
})
console.log("------------")
console.log("")
}) // for response.labels
} // if
});
- Java V2
-
此程式碼取自 AWS 文件開發套件範例 GitHub 儲存庫。請參閱此處的完整範例。
//snippet-start:[rekognition.java2.detect_labels.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.Image;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsRequest;
import software.amazon.awssdk.services.rekognition.model.DetectLabelsResponse;
import software.amazon.awssdk.services.rekognition.model.Label;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
import software.amazon.awssdk.services.rekognition.model.S3Object;
import java.util.List;
/**
* Before running this Java V2 code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class DetectLabels {
public static void main(String[] args) {
final String usage = "\n" +
"Usage: " +
" <bucket> <image>\n\n" +
"Where:\n" +
" bucket - The name of the Amazon S3 bucket that contains the image (for example, ,ImageBucket)." +
" image - The name of the image located in the Amazon S3 bucket (for example, Lake.png). \n\n";
if (args.length != 2) {
System.out.println(usage);
System.exit(1);
}
String bucket = args[0];
String image = args[1];
Region region = Region.US_WEST_2;
RekognitionClient rekClient = RekognitionClient.builder()
.region(region)
.credentialsProvider(ProfileCredentialsProvider.create("profile-name"))
.build();
getLabelsfromImage(rekClient, bucket, image);
rekClient.close();
}
// snippet-start:[rekognition.java2.detect_labels_s3.main]
public static void getLabelsfromImage(RekognitionClient rekClient, String bucket, String image) {
try {
S3Object s3Object = S3Object.builder()
.bucket(bucket)
.name(image)
.build() ;
Image myImage = Image.builder()
.s3Object(s3Object)
.build();
DetectLabelsRequest detectLabelsRequest = DetectLabelsRequest.builder()
.image(myImage)
.maxLabels(10)
.build();
DetectLabelsResponse labelsResponse = rekClient.detectLabels(detectLabelsRequest);
List<Label> labels = labelsResponse.labels();
System.out.println("Detected labels for the given photo");
for (Label label: labels) {
System.out.println(label.name() + ": " + label.confidence().toString());
}
} catch (RekognitionException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
// snippet-end:[rekognition.java2.detect_labels.main]
}
DetectLabels 操作要求
DetectLabel
的輸入是映像。在此範例 JSON 輸入中,來源映像是從 Amazon S3 儲存貯體載入的。MaxLabels
是回應中傳回的最大標籤數量。MinConfidence
是 Amazon Rekognition Image 在偵測到的標籤的準確性中必須具有的最低可信度,以便在回應中傳回。
此功能可讓您指定要傳回的映像的一或多個特徵,以便選取 GENERAL_LABELS
和 IMAGE_PROPERTIES
。其包括的 GENERAL_LABELS
將傳回輸入映像中偵測到的標籤,IMAGE_PROPERTIES
將允許您存取映像的顏色和質量。
設定可讓您篩選 GENERAL_LABELS
和 IMAGE_PROPERTIES
特徵的傳回專案。對於標籤,您可以使用包容性和獨家篩選器。您還可以按特定標籤,單個標籤或按標籤類別進行篩選:
-
LabelInclusionFilter:可讓您指定要包含在回應中的標籤。
-
LabelExclusionFilter:可讓您指定要從回應中排除哪些標籤。
-
LabelCategoryInclusionFilter:可讓您指定要包含在回應中的標籤類別。
-
LabelCategoryExclusionFilter:可讓您指定要從回應中排除的標籤類別。
您還可以根據需要組合包含性和排斥性篩選,但不包括某些標籤或類別以及包括其他標籤或類別。
IMAGE_PROPERTIES
指映像的主要色彩和品質屬性,例如銳利度、亮度和對比度。偵測 IMAGE_PROPERTIES
時,您可以使用 MaxDominantColors
參數指定要傳回的最大主色數 (預設值為 10)。
{
"Image": {
"S3Object": {
"Bucket": "bucket",
"Name": "input.jpg"
}
},
"MaxLabels": 10,
"MinConfidence": 75,
"Features": [ "GENERAL_LABELS", "IMAGE_PROPERTIES" ],
"Settings": {
"GeneralLabels": {
"LabelInclusionFilters": [<Label(s)>],
"LabelExclusionFilters": [<Label(s)>],
"LabelCategoryInclusionFilters": [<Category Name(s)>],
"LabelCategoryExclusionFilters": [<Category Name(s)>]
},
"ImageProperties": {
"MaxDominantColors":10
}
}
}
DetectLabels 回應
DetectLabels
的回應是映像中偵測到的一系列標籤,以及偵測所依據的可信度層級。
以下是 DetectLabels
的回應範例。以下範例回應包含針對 GENERAL_LABELS 傳回的各種屬性,包括:
-
名稱:偵測到的標籤的名稱。在此範例中,操作偵測到帶有標籤移動電話的物件。
-
可信度:每個標籤都有一個相關的可信度等級。在此範例中,標籤的可信度為 99.36%。
-
父項:偵測到之標籤的祖系標籤。在此範例中,標籤行動電話有一個名為電話的父標籤。
-
別名:標籤可能有別名的相關資訊。在此範例中,行動電話標籤有可能的行動電話別名。
-
類別:偵測到的標籤所屬的標籤類別。在這個例子中,其是技術和計算。
常見物件標籤的回應包含輸入映像上標籤位置的週框方塊資訊。例如,人員標籤具有一個實例陣列,其中包含兩個週框方塊。這些是在映像中偵測到的兩個人員位置。
回應還包括有關 IMAGE_PROPERTIES 的屬性。IMAGE_PROPERTIES 特徵所呈現的屬性如下:
-
質量:有關輸入映像的銳利度,亮度和對比度的資訊,得分在 0 到 100 之間。質量會針對整個映像以及映像的背景和前景報告品質 (如果有的話)。不過,只會報告整個映像的對比度,而銳利度和亮度也會用於報告背景和前景。
-
主色:映像中主色的陣列。每種主要顏色都使用簡化的顏色名稱、CSS 調色盤、RGB 值和十六進位程式碼來描述。
-
前景:有關輸入映像前景的主要顏色、銳利度和亮度的資訊。
-
背景:有關輸入映像背景的主要顏色、銳利度和亮度的資訊。
當 GENERAL_LABELS 和 IMAGE_PROPERTIES 一起使用做為輸入參數時,Amazon Rekognition Image 也會傳回具有邊界方塊之物件的主要顏色。
欄位 LabelModelVersion
包含 DetectLabels
所使用之偵測模型的版本編號。
{
"Labels": [
{
"Name": "Mobile Phone",
"Parents": [
{
"Name": "Phone"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
],
"Categories": [
{
"Name": "Technology and Computing"
}
],
"Confidence": 99.9364013671875,
"Instances": [
{
"BoundingBox": {
"Width": 0.26779675483703613,
"Height": 0.8562285900115967,
"Left": 0.3604024350643158,
"Top": 0.09245597571134567,
}
"Confidence": 99.9364013671875,
"DominantColors": [
{
"Red": 120,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"SimplifiedColor": "red",
"CssColor": "fuscia",
"PixelPercentage": 40.10
}
],
}
]
}
],
"ImageProperties": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
"Contrast": 24,
},
"DominantColors": [
{
"Red": 120,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"SimplifiedColor": "red",
"CssColor": "fuscia",
"PixelPercentage": 40.10
}
],
"Foreground": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
},
"DominantColors": [
{
"Red": 200,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"CSSColor": "",
"SimplifiedColor": "red",
"PixelPercentage": 30.70
}
],
}
"Background": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
},
"DominantColors": [
{
"Red": 200,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"CSSColor": "",
"SimplifiedColor": "Red",
"PixelPercentage": 10.20
}
],
},
},
"LabelModelVersion": "3.0"
}
使用 DetectLabels API 時,您可能需要回應結構來模擬較舊的 API 回應結構,其中主要標籤和別名都包含在同一個清單中。
以下是目前來自 DetectLabels 的 API 回應範例:
"Labels": [
{
"Name": "Mobile Phone",
"Confidence": 99.99717712402344,
"Instances": [],
"Parents": [
{
"Name": "Phone"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
]
}
]
下列範例顯示先前來自 DetectLabels 的 API 回應:
"Labels": [
{
"Name": "Mobile Phone",
"Confidence": 99.99717712402344,
"Instances": [],
"Parents": [
{
"Name": "Phone"
}
]
},
{
"Name": "Cell Phone",
"Confidence": 99.99717712402344,
"Instances": [],
"Parents": [
{
"Name": "Phone"
}
]
},
]
如果需要,您可以將當前回應轉換為遵循舊回應的格式。您可以使用下列範例程式碼,將最新的 API 回應轉換為先前的 API 回應結構:
- Python
-
下列程式碼範例示範如何從 DetectLabels API 轉換為目前的回應。在下面的程式碼範例中,您可以將 EXAMPLE_INFERENCE_OUTPUT
的值取代為您已執行的 DetectLabels 作業的輸出。
from copy import deepcopy
LABEL_KEY = "Labels"
ALIASES_KEY = "Aliases"
INSTANCE_KEY = "Instances"
NAME_KEY = "Name"
#Latest API response sample
EXAMPLE_INFERENCE_OUTPUT = {
"Labels": [
{
"Name": "Mobile Phone",
"Confidence": 97.530106,
"Categories": [
{
"Name": "Technology and Computing"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
],
"Instances":[
{
"BoundingBox":{
"Height":0.1549897,
"Width":0.07747964,
"Top":0.50858885,
"Left":0.00018205095
},
"Confidence":98.401276
}
]
},
{
"Name": "Urban",
"Confidence": 99.99982,
"Categories": [
"Colors and Visual Composition"
]
}
]
}
def expand_aliases(inferenceOutputsWithAliases):
if LABEL_KEY in inferenceOutputsWithAliases:
expandInferenceOutputs = []
for primaryLabelDict in inferenceOutputsWithAliases[LABEL_KEY]:
if ALIASES_KEY in primaryLabelDict:
for alias in primaryLabelDict[ALIASES_KEY]:
aliasLabelDict = deepcopy(primaryLabelDict)
aliasLabelDict[NAME_KEY] = alias[NAME_KEY]
del aliasLabelDict[ALIASES_KEY]
if INSTANCE_KEY in aliasLabelDict:
del aliasLabelDict[INSTANCE_KEY]
expandInferenceOutputs.append(aliasLabelDict)
inferenceOutputsWithAliases[LABEL_KEY].extend(expandInferenceOutputs)
return inferenceOutputsWithAliases
if __name__ == "__main__":
outputWithExpandAliases = expand_aliases(EXAMPLE_INFERENCE_OUTPUT)
print(outputWithExpandAliases)
以下是轉換回應的範例:
#Output example after the transformation
{
"Labels": [
{
"Name": "Mobile Phone",
"Confidence": 97.530106,
"Categories": [
{
"Name": "Technology and Computing"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
],
"Instances":[
{
"BoundingBox":{
"Height":0.1549897,
"Width":0.07747964,
"Top":0.50858885,
"Left":0.00018205095
},
"Confidence":98.401276
}
]
},
{
"Name": "Cell Phone",
"Confidence": 97.530106,
"Categories": [
{
"Name": "Technology and Computing"
}
],
"Instances":[]
},
{
"Name": "Urban",
"Confidence": 99.99982,
"Categories": [
"Colors and Visual Composition"
]
}
]
}