AWS SDK を使用して Amazon Rekognition コレクションを作成します - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して Amazon Rekognition コレクションを作成します

次のコード例は、Amazon Rekognition コレクションを作成する方法を示しています。

詳細については、「コレクションを作成する」を参照してください。

.NET
AWS SDK for .NET
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses Amazon Rekognition to create a collection to which you can add /// faces using the IndexFaces operation. /// </summary> public class CreateCollection { public static async Task Main() { var rekognitionClient = new AmazonRekognitionClient(); string collectionId = "MyCollection"; Console.WriteLine("Creating collection: " + collectionId); var createCollectionRequest = new CreateCollectionRequest { CollectionId = collectionId, }; CreateCollectionResponse createCollectionResponse = await rekognitionClient.CreateCollectionAsync(createCollectionRequest); Console.WriteLine($"CollectionArn : {createCollectionResponse.CollectionArn}"); Console.WriteLine($"Status code : {createCollectionResponse.StatusCode}"); } }
  • API の詳細については、「 API リファレンスCreateCollection」の「」を参照してください。 AWS SDK for .NET

CLI
AWS CLI

コレクションを作成するには

次の create-collection コマンドは、指定された名前のコレクションを作成します。

aws rekognition create-collection \ --collection-id "MyCollection"

出力:

{ "CollectionArn": "aws:rekognition:us-west-2:123456789012:collection/MyCollection", "FaceModelVersion": "4.0", "StatusCode": 200 }

詳細については、「Amazon Rekognition 開発者ガイド」の「コレクションの作成」を参照してください。

  • API の詳細については、「 コマンドリファレンスCreateCollection」の「」を参照してください。 AWS CLI

Java
SDK for Java 2.x
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.CreateCollectionResponse; import software.amazon.awssdk.services.rekognition.model.CreateCollectionRequest; import software.amazon.awssdk.services.rekognition.model.RekognitionException; /** * 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 CreateCollection { public static void main(String[] args) { final String usage = """ Usage: <collectionName>\s Where: collectionName - The name of the collection.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String collectionId = args[0]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); System.out.println("Creating collection: " + collectionId); createMyCollection(rekClient, collectionId); rekClient.close(); } public static void createMyCollection(RekognitionClient rekClient, String collectionId) { try { CreateCollectionRequest collectionRequest = CreateCollectionRequest.builder() .collectionId(collectionId) .build(); CreateCollectionResponse collectionResponse = rekClient.createCollection(collectionRequest); System.out.println("CollectionArn: " + collectionResponse.collectionArn()); System.out.println("Status code: " + collectionResponse.statusCode().toString()); } catch (RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } }
  • API の詳細については、「 API リファレンスCreateCollection」の「」を参照してください。 AWS SDK for Java 2.x

Kotlin
SDK for Kotlin
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

suspend fun createMyCollection(collectionIdVal: String) { val request = CreateCollectionRequest { collectionId = collectionIdVal } RekognitionClient { region = "us-east-1" }.use { rekClient -> val response = rekClient.createCollection(request) println("Collection ARN is ${response.collectionArn}") println("Status code is ${response.statusCode}") } }
  • API の詳細については、CreateCollectionAWS「 SDK for Kotlin API リファレンス」の「」を参照してください。

Python
SDK for Python (Boto3)
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

class RekognitionCollectionManager: """ Encapsulates Amazon Rekognition collection management functions. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, rekognition_client): """ Initializes the collection manager object. :param rekognition_client: A Boto3 Rekognition client. """ self.rekognition_client = rekognition_client def create_collection(self, collection_id): """ Creates an empty collection. :param collection_id: Text that identifies the collection. :return: The newly created collection. """ try: response = self.rekognition_client.create_collection( CollectionId=collection_id ) response["CollectionId"] = collection_id collection = RekognitionCollection(response, self.rekognition_client) logger.info("Created collection %s.", collection_id) except ClientError: logger.exception("Couldn't create collection %s.", collection_id) raise else: return collection
  • API の詳細については、CreateCollectionAWS「 SDK for Python (Boto3) API リファレンス」の「」を参照してください。