呼叫 Amazon Rekognition 自訂標籤操作 - Rekognition

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

呼叫 Amazon Rekognition 自訂標籤操作

執行以下程式碼,以確認您可以呼叫 Amazon Rekognition 自訂標籤 API。此代碼會在目前 AWS 區域中列出您 AWS 帳戶中的專案。如果您之前尚未建立專案,則回應為空白,但確認您可以呼叫該 DescribeProjects操作。

一般而言,呼叫範例函數需要 AWS SDK Rekognition 用戶端和任何其他必要的參數。AWS SDK 用戶端在主函數中宣告。

如果程式碼失敗,請檢查您使用的使用者是否擁有正確的權限。另請檢查您使用的 AWS 區域,因為並非所有區域都提供 Amazon Rekognition 自訂標籤。 AWS

若要呼叫 Amazon Rekognition 自訂標籤操作
  1. 如果您尚未這樣做,請安裝並設定 AWS CLI 和 AWS SDK。如需詳細資訊,請參閱 步驟 4:設定 AWS CLI 和開 AWS 發套件

  2. 使用以下的範例程式碼來檢視您的專案。

    CLI

    使用 describe-projects 指令列出您帳戶中的專案。

    aws rekognition describe-projects \ --profile custom-labels-access
    Python
    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ This example shows how to describe your Amazon Rekognition Custom Labels projects. If you haven't previously created a project in the current AWS Region, the response is an empty list, but does confirm that you can call an Amazon Rekognition Custom Labels operation. """ from botocore.exceptions import ClientError import boto3 def describe_projects(rekognition_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. : param rekognition_client: A Boto3 Rekognition client. """ try: response = rekognition_client.describe_projects() for project in response["ProjectDescriptions"]: print("Status: " + project["Status"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError as err: print(f"Couldn't describe projects. \n{err}") raise def main(): """ Entrypoint for script. """ session = boto3.Session(profile_name='custom-labels-access') rekognition_client = session.client("rekognition") describe_projects(rekognition_client) if __name__ == "__main__": main()
    Java V2
    /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.rekognition; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; 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.DatasetMetadata; import software.amazon.awssdk.services.rekognition.model.DescribeProjectsRequest; import software.amazon.awssdk.services.rekognition.model.DescribeProjectsResponse; import software.amazon.awssdk.services.rekognition.model.ProjectDescription; import software.amazon.awssdk.services.rekognition.model.RekognitionException; public class Hello { public static final Logger logger = Logger.getLogger(Hello.class.getName()); public static void describeMyProjects(RekognitionClient rekClient) { DescribeProjectsRequest descProjects = null; // If a single project name is supplied, build projectNames argument List<String> projectNames = new ArrayList<String>(); descProjects = DescribeProjectsRequest.builder().build(); // Display useful information for each project. DescribeProjectsResponse resp = rekClient.describeProjects(descProjects); for (ProjectDescription projectDescription : resp.projectDescriptions()) { System.out.println("ARN: " + projectDescription.projectArn()); System.out.println("Status: " + projectDescription.statusAsString()); if (projectDescription.hasDatasets()) { for (DatasetMetadata datasetDescription : projectDescription.datasets()) { System.out.println("\tdataset Type: " + datasetDescription.datasetTypeAsString()); System.out.println("\tdataset ARN: " + datasetDescription.datasetArn()); System.out.println("\tdataset Status: " + datasetDescription.statusAsString()); } } System.out.println(); } } public static void main(String[] args) { try { // Get the Rekognition client RekognitionClient rekClient = RekognitionClient.builder() .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access")) .region(Region.US_WEST_2) .build(); // Describe projects describeMyProjects(rekClient); rekClient.close(); } catch (RekognitionException rekError) { logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage()); System.exit(1); } } }