建立集合 - Amazon Rekognition

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

建立集合

您可以使用該CreateCollection操作來創建集合。

如需詳細資訊,請參閱管理集合

若要建立集合 (SDK)
  1. 如果您尚未執行:

    1. 建立或更新具有 AmazonRekognitionFullAccess 許可的使用者。如需詳細資訊,請參閱步驟 1:設定AWS帳戶並建立使用者

    2. 安裝並設定 AWS CLI 和 AWS SDKs. 如需詳細資訊,請參閱步驟 2:設定 AWS CLI 以及 AWS SDKs

  2. 使用下列範例來呼叫 CreateCollection 操作。

    Java

    下列範例會建立集合並顯示其 Amazon 資源名稱 (ARN)。

    collectionId 的值變更為您要建立集合的名稱。

    //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 aws.example.rekognition.image; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.CreateCollectionRequest; import com.amazonaws.services.rekognition.model.CreateCollectionResult; public class CreateCollection { public static void main(String[] args) throws Exception { AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); String collectionId = "MyCollection"; System.out.println("Creating collection: " + collectionId ); CreateCollectionRequest request = new CreateCollectionRequest() .withCollectionId(collectionId); CreateCollectionResult createCollectionResult = rekognitionClient.createCollection(request); System.out.println("CollectionArn : " + createCollectionResult.getCollectionArn()); System.out.println("Status code : " + createCollectionResult.getStatusCode().toString()); } }
    Java V2

    此代碼取自 AWS 文檔SDK示例 GitHub 存儲庫。請參閱此處的完整範例。

    將建立 Rekognition 工作階段的行中 profile_name 值取代為您開發人員設定檔的名稱。

    //snippet-start:[rekognition.java2.create_collection.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.CreateCollectionResponse; import software.amazon.awssdk.services.rekognition.model.CreateCollectionRequest; import software.amazon.awssdk.services.rekognition.model.RekognitionException; //snippet-end:[rekognition.java2.create_collection.import] /** * 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 = "\n" + "Usage: " + " <collectionName> \n\n" + "Where:\n" + " collectionName - The name of the collection. \n\n"; 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) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); System.out.println("Creating collection: " +collectionId); createMyCollection(rekClient, collectionId ); rekClient.close(); } // snippet-start:[rekognition.java2.create_collection.main] 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); } } // snippet-end:[rekognition.java2.create_collection.main]
    AWS CLI

    此 AWS CLI 指令會顯示create-collectionCLI作業的JSON輸出。

    collection-id 的值取代為您要建立集合的名稱。

    使用您開發人員設定檔的名稱取代 profile_name 的值。

    aws rekognition create-collection --profile profile-name --collection-id "collection-name"
    Python

    下列範例會建立集合並顯示其 Amazon 資源名稱 (ARN)。

    collection_id 的值變更為您要建立集合的名稱。將建立 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_collection(collection_id): session = boto3.Session(profile_name='profile-name') client = session.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 = "collection-id" create_collection(collection_id) if __name__ == "__main__": main()
    .NET

    下列範例會建立集合並顯示其 Amazon 資源名稱 (ARN)。

    collectionId 的值變更為您要建立集合的名稱。

    //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 CreateCollection { public static void Example() { AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); String collectionId = "MyCollection"; Console.WriteLine("Creating collection: " + collectionId); CreateCollectionRequest createCollectionRequest = new CreateCollectionRequest() { CollectionId = collectionId }; CreateCollectionResponse createCollectionResponse = rekognitionClient.CreateCollection(createCollectionRequest); Console.WriteLine("CollectionArn : " + createCollectionResponse.CollectionArn); Console.WriteLine("Status code : " + createCollectionResponse.StatusCode); } }
    Node.JS

    在下列範例中,將 region 的值取代為與您帳戶相關聯的區域名稱,並將 collectionName 的值替代為您集合的所需名稱。

    將建立 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 { CreateCollectionCommand} from "@aws-sdk/client-rekognition"; import { RekognitionClient } from "@aws-sdk/client-rekognition"; import {fromIni} from '@aws-sdk/credential-providers'; // Set the AWS Region. const REGION = "region-name"; //e.g. "us-east-1" // Set the profile name const profileName = "profile-name" // Name the collection const collectionName = "collection-name" const rekogClient = new RekognitionClient({region: REGION, credentials: fromIni({profile: profileName,}), }); const createCollection = async (collectionName) => { try { console.log(`Creating collection: ${collectionName}`) const data = await rekogClient.send(new CreateCollectionCommand({CollectionId: collectionName})); console.log("Collection ARN:") console.log(data.CollectionARN) console.log("Status Code:") console.log(String(data.StatusCode)) console.log("Success.", data); return data; } catch (err) { console.log("Error", err.stack); } }; createCollection(collectionName)

CreateCollection 操作請求

CreationCollection 的輸入是您要建立的集合的名稱。

{ "CollectionId": "MyCollection" }

CreateCollection 作業回應

Amazon Rekognition 創建集合並返回新創建的集合的 Amazon 資源名稱(ARN)。

{ "CollectionArn": "aws:rekognition:us-east-1:acct-id:collection/examplecollection", "StatusCode": 200 }