列出集合 - Amazon Rekognition

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

列出集合

您可以使用此ListCollections作業列出所使用區域中的集合。

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

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

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

    2. 安裝和設定 AWS CLI AWS 軟體開發套件。如需詳細資訊,請參閱 步驟 2:設定 AWS CLI 和開 AWS 發套件

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

    Java

    以下範例列出目前區域中的集合。

    //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 java.util.List; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.ListCollectionsRequest; import com.amazonaws.services.rekognition.model.ListCollectionsResult; public class ListCollections { public static void main(String[] args) throws Exception { AmazonRekognition amazonRekognition = AmazonRekognitionClientBuilder.defaultClient(); System.out.println("Listing collections"); int limit = 10; ListCollectionsResult listCollectionsResult = null; String paginationToken = null; do { if (listCollectionsResult != null) { paginationToken = listCollectionsResult.getNextToken(); } ListCollectionsRequest listCollectionsRequest = new ListCollectionsRequest() .withMaxResults(limit) .withNextToken(paginationToken); listCollectionsResult=amazonRekognition.listCollections(listCollectionsRequest); List < String > collectionIds = listCollectionsResult.getCollectionIds(); for (String resultId: collectionIds) { System.out.println(resultId); } } while (listCollectionsResult != null && listCollectionsResult.getNextToken() != null); } }
    Java V2

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

    //snippet-start:[rekognition.java2.list_collections.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.ListCollectionsRequest; import software.amazon.awssdk.services.rekognition.model.ListCollectionsResponse; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import java.util.List; //snippet-end:[rekognition.java2.list_collections.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 ListCollections { public static void main(String[] args) { Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); System.out.println("Listing collections"); listAllCollections(rekClient); rekClient.close(); } // snippet-start:[rekognition.java2.list_collections.main] public static void listAllCollections(RekognitionClient rekClient) { try { ListCollectionsRequest listCollectionsRequest = ListCollectionsRequest.builder() .maxResults(10) .build(); ListCollectionsResponse response = rekClient.listCollections(listCollectionsRequest); List<String> collectionIds = response.collectionIds(); for (String resultId : collectionIds) { System.out.println(resultId); } } catch (RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } // snippet-end:[rekognition.java2.list_collections.main] }
    AWS CLI

    此 AWS CLI 命令會顯示 list-collections CLI 作業的 JSON 輸出。使用您開發人員設定檔的名稱取代 profile_name 的值。

    aws rekognition list-collections --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 list_collections(): max_results=2 client=boto3.client('rekognition') #Display all the collections print('Displaying collections...') response=client.list_collections(MaxResults=max_results) collection_count=0 done=False while done==False: collections=response['CollectionIds'] for collection in collections: print (collection) collection_count+=1 if 'NextToken' in response: nextToken=response['NextToken'] response=client.list_collections(NextToken=nextToken,MaxResults=max_results) else: done=True return collection_count def main(): collection_count=list_collections() print("collections: " + str(collection_count)) if __name__ == "__main__": main()
    .NET

    以下範例列出目前區域中的集合。

    //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 ListCollections { public static void Example() { AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); Console.WriteLine("Listing collections"); int limit = 10; ListCollectionsResponse listCollectionsResponse = null; String paginationToken = null; do { if (listCollectionsResponse != null) paginationToken = listCollectionsResponse.NextToken; ListCollectionsRequest listCollectionsRequest = new ListCollectionsRequest() { MaxResults = limit, NextToken = paginationToken }; listCollectionsResponse = rekognitionClient.ListCollections(listCollectionsRequest); foreach (String resultId in listCollectionsResponse.CollectionIds) Console.WriteLine(resultId); } while (listCollectionsResponse != null && listCollectionsResponse.NextToken != null); } }
    Node.js

    將建立 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 { ListCollectionsCommand } 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 rekogClient = new RekognitionClient({region: REGION, credentials: fromIni({profile: profileName,}), }); const listCollection = async () => { var max_results = 10 console.log("Displaying collections:") var response = await rekogClient.send(new ListCollectionsCommand({MaxResults: max_results})) var collection_count = 0 var done = false while (done == false){ var collections = response.CollectionIds collections.forEach(collection => { console.log(collection) collection_count += 1 }); return collection_count } } var collect_list = await listCollection() console.log(collect_list)

ListCollections 操作請求

ListCollections 的輸入是要傳回的最大集合數。

{ "MaxResults": 2 }

如果回應的集合多於 MaxResults 請求,則會傳回一個字符,您可以在後續的 ListCollections 呼叫中使用該字符獲取下一組結果。例如:

{ "NextToken": "MGYZLAHX1T5a....", "MaxResults": 2 }

ListCollections 作業回應

Amazon Rekognition 傳回集合陣列 (CollectionIds)。個別陣列 (FaceModelVersions) 提供用於分析每個集合中人臉的人臉模型的版本。例如,在下列 JSON 回應中,集合 MyCollection 使用臉部模型的 2.0 版分析臉部。集合 AnotherCollection 使用臉部模型的 3.0 版本。如需詳細資訊,請參閱 模型版本控制

NextToken 是在後續的 ListCollections 呼叫中用於取得下一組結果的字符。

{ "CollectionIds": [ "MyCollection", "AnotherCollection" ], "FaceModelVersions": [ "2.0", "3.0" ], "NextToken": "MGYZLAHX1T5a...." }