翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Amazon Rekognition Custom Labels オペレーションを呼び出す
次のコードを実行して、Amazon Rekognition Custom Labels API を呼び出せることを確認します。このコードは、現在の AWS リージョンの AWS アカウント内のプロジェクトを一覧表示します。まだプロジェクトを作成していない場合、レスポンスは空ですが、DescribeProjects
オペレーションを呼び出せることを確認できます。
一般的に、サンプル関数を呼び出すには、AWS SDK Rekognition クライアントとそのほかの必要なパラメータが必要です。AWS SDK クライアントはメイン関数で宣言されます。
コードが失敗する場合は、使用するユーザーに正しいアクセス許可があるかどうかを確認します。また、Amazon Rekognition Custom Labels として使用している AWS リージョンが、一部の AWS リージョンで使用できないことも確認してください。
Amazon Rekognition Custom Labels オペレーションを呼び出すには
-
まだインストールしていない場合は、 と AWS SDKs をインストール AWS CLI して設定します。詳細については、「ステップ 4: AWS CLI と AWS SDKsを設定する」を参照してください。
-
次のサンプルコードを使用して、プロジェクトを表示します。
- 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);
}
}
}