本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
检测图像中的标签
您可以使用该DetectLabels操作来检测图像中的标签(对象和概念),并检索有关图像属性的信息。图像属性包括诸如前景和背景的颜色以及图像的锐度、亮度和对比度之类的属性。您可以只检索图像中的标签,也可以仅检索图像的属性,或者两者兼而有之。有关示例,请参阅分析存储在 Amazon S3 存储桶中的图像。
以下示例使用各种 AWS SDKs 和 to AWS CLI call DetectLabels
。有关 DetectLabels
操作响应的信息,请参阅DetectLabels 响应。
检测图像中的标签
-
如果您尚未执行以下操作,请:
使用 AmazonRekognitionFullAccess
和 AmazonS3ReadOnlyAccess
权限创建或更新用户。有关更多信息,请参阅 步骤 1:设置 AWS 账户并创建用户。
安装并配置 AWS CLI 和 AWS SDKs。有关更多信息,请参阅 步骤 2:设置 AWS CLI 和 AWS SDK。
-
将其中包含一个或多个对象(如树木、房屋和船)的图像上传到您的 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 文档 SDK 示例 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
功能的返回项目。对于标签,您可以使用纳入和排除筛选器。您也可以按特定标签、单个标签或标签类别进行筛选:
-
LabelInclusionFilters -允许您指定要在响应中包含哪些标签。
-
LabelExclusionFilters -允许您指定要从响应中排除哪些标签。
-
LabelCategoryInclusionFilters -允许您指定要在响应中包含哪些标签类别。
-
LabelCategoryExclusionFilters -允许您指定要从响应中排除哪些标签类别。
您还可以根据需要组合纳入和排除筛选器,排除某些标签或类别,而纳入其他标签或类别。
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 返回的各种属性,包括:
-
名称 – 检测到的标签的名称。在此示例中,操作检测到一个标有“Mobile Phone”标签的对象。
-
置信度 – 每个标签均有一个关联的置信度级别。在此示例中,标签的置信度为 99.36%。
-
父级 – 检测到的标签的原级标签。在此示例中,“Mobile Phone”标签有一个名为“Phone”的父标签。
-
别名 – 有关标签可能的别名的信息。在此示例中,“Mobile Phone”标签的别名可能是“Cell Phone”。
-
类别 – 检测到的标签所属的标签类别。在此示例中,它是“技术和计算”。
常见对象标签的响应包括边界框信息,针对输入图像上标签的位置。例如,“人”标签有包含两个边界框的实例数组。这两个边界框是在图像中检测到的两个人的位置。
响应还包括与 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 响应结构,其中主标签和别名都包含在同一个列表中。
以下是来自的当前 API 响应的示例 DetectLabels:
"Labels": [
{
"Name": "Mobile Phone",
"Confidence": 99.99717712402344,
"Instances": [],
"Parents": [
{
"Name": "Phone"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
]
}
]
以下示例显示了 DetectLabelsAPI 之前的响应:
"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"
]
}
]
}