View a markdown version of this page

标记集合 - Amazon Rekognition

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

标记集合

您可以使用标签识别、整理、搜索和筛选 Amazon Rekognition 集合。每个标签都是由用户定义的键和值组成的标签。

您还可以使用 Identity and Access Management (IAM) 使用标签控制集合的访问权限。有关更多信息,请参阅使用 AWS 资源标签控制对资源的访问权限

向新集合添加标签

您可以使用 CreateCollection 操作在创建集合时向其添加标签。请在 Tags 数组输入参数中指定一个或多个标签。

AWS CLI

profile_name的值替换为您的开发人员资料的名称。

aws rekognition create-collection --collection-id "collection-name" --tags "{"key1":"value1","key2":"value2"}" --profile profile-name

对于 Windows 设备:

aws rekognition create-collection --collection-id "collection-name" --tags "{\"key1\":\"value1\",\"key2\":\"value2\"}" --profile profile-name
Python

将创建 Rekognition 会话的行中的profile_name值替换为您的开发人员资料的名称。

import boto3 def create_collection(collection_id): client = boto3.client('rekognition') # Create a collection print('Creating collection:' + collection_id) response = client.create_collection(CollectionId=collection_id) print('Collection ARN: ' + response['CollectionArn']) print('Status code: ' + str(response['StatusCode'])) print('Done...') def main(): collection_id = 'NewCollectionName' create_collection(collection_id) if __name__ == "__main__": main()

向现有集合添加标签

要将一个或多个标签添加到现有集合,请使用 TagResource 操作。指定集合的 Amazon 资源名称(ARN)(ResourceArn)和要添加的标签(Tags)。以下示例说明了如何添加两个标签。

AWS CLI

profile_name的值替换为您的开发人员资料的名称。

aws rekognition tag-resource --resource-arn collection-arn --tags "{"key1":"value1","key2":"value2"}" --profile profile-name

对于 Windows 设备:

aws rekognition tag-resource --resource-arn collection-arn --tags "{\"key1\":\"value1\",\"key2\":\"value2\"}" --profile profile-name
Python

将创建 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 create_tag(collection_id): session = boto3.Session(profile_name='default') client = session.client('rekognition') response = client.tag_resource(ResourceArn=collection_id, Tags={ "KeyName": "ValueName" }) print(response) if "'HTTPStatusCode': 200" in str(response): print("Success!!") def main(): collection_arn = "collection-arn" create_tag(collection_arn) if __name__ == "__main__": main()
注意

如果您不知道该集合的 Amazon 资源名称,则可以使用 DescribeCollection 操作。

列出集合中的标签

要列出附加到集合的标签,请使用 ListTagsForResource 操作并指定该集合的 ARN(ResourceArn)。该操作的响应是附加到指定集合的标签键和值的映射图。

AWS CLI

profile_name的值替换为您的开发人员资料的名称。

aws rekognition list-tags-for-resource --resource-arn resource-arn --profile profile-name
Python

将创建 Rekognition 会话的行中的profile_name值替换为您的开发人员资料的名称。

import boto3 def list_tags(): client = boto3.client('rekognition') response = client.list_tags_for_resource(ResourceArn="arn:aws:rekognition:region-name:5498347593847598:collection/NewCollectionName") print(response) def main(): list_tags() if __name__ == "__main__": main()

输出将显示附加到集合的标签列表:

{ "Tags": { "Dept": "Engineering", "Name": "Ana Silva Carolina", "Role": "Developer" } }

从集合中删除标签

要从集合中删除一个或多个标签,请使用 UntagResource 操作。指定模型的 ARN(ResourceArn)和要移除的标签键(Tag-Keys)。

AWS CLI

profile_name的值替换为您的开发人员资料的名称。

aws rekognition untag-resource --resource-arn resource-arn --profile profile-name --tag-keys "key1" "key2"

或者,您可以按以下格式指定标签键:

--tag-keys key1,key2
Python

将创建 Rekognition 会话的行中的profile_name值替换为您的开发人员资料的名称。

import boto3 def list_tags(): client = boto3.client('rekognition') response = client.untag_resource(ResourceArn="arn:aws:rekognition:region-name:5498347593847598:collection/NewCollectionName", TagKeys=['KeyName']) print(response) def main(): list_tags() if __name__ == "__main__": main()