呼叫 Amazon Lookout for Vision 操作 - Amazon Lookout for Vision

支援終止通知:在 2025 年 10 月 31 日, AWS 將停止對 Amazon Lookout for Vision 的支援。2025 年 10 月 31 日之後,您將無法再存取 Lookout for Vision 主控台或 Lookout for Vision 資源。如需詳細資訊,請造訪此部落格文章

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

呼叫 Amazon Lookout for Vision 操作

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

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

如果程式碼失敗,請檢查您使用的使用者是否擁有正確的權限。另請檢查 AWS 您用作 Amazon Lookout for Vision 的區域,並非所有 AWS 區域都可用。

呼叫 Amazon Lookout for Vision 操作
  1. 如果您尚未這麼做,請安裝並設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 和 AWS SDKs

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

    CLI

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

    aws lookoutvision list-projects \ --profile lookoutvision-access
    Python

    此程式碼取自 AWS 文件SDK範例 GitHub 儲存庫。請參閱此處的完整範例。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 from botocore.exceptions import ClientError import boto3 class GettingStarted: @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError: raise def main(): session = boto3.Session(profile_name='lookoutvision-access') lookoutvision_client = session.client("lookoutvision") GettingStarted.list_projects(lookoutvision_client) if __name__ == "__main__": main()
    Java V2

    此程式碼取自 AWS 文件SDK範例 GitHub 儲存庫。請參閱此處的完整範例。

    /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.lookoutvision; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.services.lookoutvision.LookoutVisionClient; import software.amazon.awssdk.services.lookoutvision.model.ProjectMetadata; import software.amazon.awssdk.services.lookoutvision.paginators.ListProjectsIterable; import software.amazon.awssdk.services.lookoutvision.model.ListProjectsRequest; import software.amazon.awssdk.services.lookoutvision.model.LookoutVisionException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class GettingStarted { public static final Logger logger = Logger.getLogger(GettingStarted.class.getName()); /** * Lists the Amazon Lookoutfor Vision projects in the current AWS account and * AWS Region. * * @param lfvClient An Amazon Lookout for Vision client. * @return List<ProjectMetadata> Metadata for each project. */ public static List<ProjectMetadata> listProjects(LookoutVisionClient lfvClient) throws LookoutVisionException { logger.log(Level.INFO, "Getting projects:"); ListProjectsRequest listProjectsRequest = ListProjectsRequest.builder() .maxResults(100) .build(); List<ProjectMetadata> projectMetadata = new ArrayList<>(); ListProjectsIterable projects = lfvClient.listProjectsPaginator(listProjectsRequest); projects.stream().flatMap(r -> r.projects().stream()) .forEach(project -> { projectMetadata.add(project); logger.log(Level.INFO, project.projectName()); }); logger.log(Level.INFO, "Finished getting projects."); return projectMetadata; } public static void main(String[] args) throws Exception { try { // Get the Lookout for Vision client. LookoutVisionClient lfvClient = LookoutVisionClient.builder() .credentialsProvider(ProfileCredentialsProvider.create("lookoutvision-access")) .build(); List<ProjectMetadata> projects = Projects.listProjects(lfvClient); System.out.printf("Projects%n--------%n"); for (ProjectMetadata project : projects) { System.out.printf("Name: %s%n", project.projectName()); System.out.printf("ARN: %s%n", project.projectArn()); System.out.printf("Date: %s%n%n", project.creationTimestamp().toString()); } } catch (LookoutVisionException lfvError) { logger.log(Level.SEVERE, "Could not list projects: {0}: {1}", new Object[] { lfvError.awsErrorDetails().errorCode(), lfvError.awsErrorDetails().errorMessage() }); System.out.println(String.format("Could not list projects: %s", lfvError.getMessage())); System.exit(1); } } }