设置项目和开发环境

本指南介绍如何设置 Google Cloud 项目以开始使用 Vertex AI Vision。

设置项目

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI Vision API:

    gcloud services enable visionai.googleapis.com
  8. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/visionai.editor IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/visionai.editor

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
    3. Generate the key file:

      gcloud iam service-accounts keys create FILE_NAME.json --iam-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

      Replace the following:

      • FILE_NAME: a name for the key file
      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  9. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  10. Install the Google Cloud CLI.

  11. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  12. To initialize the gcloud CLI, run the following command:

    gcloud init
  13. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  14. Make sure that billing is enabled for your Google Cloud project.

  15. Enable the Vertex AI Vision API:

    gcloud services enable visionai.googleapis.com
  16. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/visionai.editor IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/visionai.editor

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
    3. Generate the key file:

      gcloud iam service-accounts keys create FILE_NAME.json --iam-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

      Replace the following:

      • FILE_NAME: a name for the key file
      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  17. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.


  18. 某些任务要求您使用 Vertex AI Vision 之外的其他Google Cloud 产品。您可能需要执行额外的设置任务才能使用其他 Google Cloud 产品。

    Vertex AI Vision SDK

    Vertex AI Vision 软件开发套件 (SDK) 包含各种工具和库,可帮助您使用 Vertex AI Vision 开发程序和自定义工作流。

    这些工具是指一组二进制源文件,可帮助您在使用或开发基于 Vertex AI Vision 的解决方案时提高工作效率。您还可以将这些二进制源文件添加到脚本中,以帮助管理大规模部署。命令行界面 (CLI) vaictl 是此类别的示例。

    库是指一组编程 API,您可以使用这些 API 以编程方式管理、控制 Vertex AI Vision 并执行不同形式的 I/O。C++ 编程 API 是此类别中的一个示例。

    这两种工具和库的代码都是开源的,我们欢迎开发者直接构建它们。对于工具,我们还提供特定平台上的预构建二进制文件,您可以下载这些文件以直接使用。我们还提供 Docker 映像,以应对平台不受直接支持的情况。

    前提条件

    在使用 Vertex AI Vision SDK 之前,请考虑以下前提条件。

    支持的平台

    我们目前仅直接支持运行基于 Debian 的 Linux 发行版的 x86 机器。如果您使用其他平台,我们还提供已构建并安装了工具的 Docker 映像。

    其他软件要求

    安装预构建的二进制文件时,系统会自动管理 Vertex AI Vision SDK 所需的大部分第三方软件依赖项。不过,如需使用 SDK 功能和工作流的某些方面,您必须安装其他依赖项。本部分列出了这些例外情况,并介绍了如何下载和安装它们。

    Python SDK

    Python pip 软件包会自动安装其所有依赖项。 不过,您的基本底层 Python 安装必须满足以下条件:

    • Python >= 3.8。

    从源代码构建依赖项

    大多数用户都可以使用预构建的 SDK 二进制文件来完成工作流程。 如果您想从源代码开发和构建 SDK,还必须确保您的系统满足以下要求。

    • 安装 Bazel。如需了解安装说明,请参阅 Bazel 文档
    • 在 Ubuntu 20.04 上,您还需要一些系统依赖项。您可以使用以下命令安装它们:

      apt-get install -y --no-install-recommends \
          autoconf \
          automake \
          build-essential \
          ca-certificates \
          flex \
          bison \
          python3 \
          nasm \
          libjpeg-dev
      

    获取预构建的二进制文件

    vaictl 工具是现成的二进制工具,可用于控制 Vertex AI Vision,以及发送和接收其处理的数据。

    本部分介绍了下载和安装此工具的方法。

    安装 Debian 软件包

    您可以使用 vaictl 命令行工具,该工具需要处理 I/O(流数据),您有以下两种选择:

    • 在本地安装命令行工具(操作系统:Debian GNU/Linux,CPU 架构:x86_64)
    • 在安装了所有依赖项的 Docker 映像中运行命令。

    请按以下步骤获取 vaictl 命令行工具:

    下载软件包

    对于直接安装,我们目前仅支持 Debian/Ubuntu 发行版。

    您还可以从 Vertex AI Vision SDK GitHub 版本页面下载此 Debian 软件包。

    1. (可选)移除旧版 vaictl

      您必须先在机器上删除 vaictl 命令行工具的所有先前版本,然后才能安装该工具:

      sudo apt-get remove visionai
    2. 下载必需的软件包。您可以从 GitHub 发布页面下载软件包,也可以使用以下命令:
      wget https://github.com/google/visionai/releases/download/v0.0.5/visionai_0.0-5_amd64.deb
    3. 下载软件包后,在下载文件的目录中运行以下命令:
      sudo apt install ./visionai_0.0-5_amd64.deb
    4. 验证安装:
      vaictl --help

    获取 Docker

    您可以获取已预安装 Vertex AI Vision SDK 及其所有依赖项的 Docker 映像。此 Docker 映像可在 gcr.io/visionai-public-images/vaictl 中找到。

    1. 从 Container Registry 下载映像:
      docker pull gcr.io/visionai-public-images/vaictl
    2. 运行交互式容器终端。
      docker run -it --rm gcr.io/visionai-public-images/vaictl /bin/bash
    3. 验证功能:
      vaictl --help

    获取源代码

    Vertex AI Vision SDK 是开源的,可在 GitHub 上公开获取。

    虽然 SDK 依赖于服务 API 定义,但 Bazel 已自动管理此依赖项,您无需明确获取它。不过,如果您需要直接访问服务 API,可以在 googleapis GitHub 代码库中获取它们。

    Python 编程 SDK

    Vertex AI Vision 还支持 Python SDK。如需使用此 SDK 进行编程,只需确保在安装 Python SDK 之前满足基本的 Python SDK 依赖项即可。

    如需了解 SDK 参考信息,请参阅 Python SDK 参考文档

    如需查看使用 Python SDK 的示例代码,请参阅使用 Python SDK 进行人脸模糊处理的教程,或查看源分发中的一些示例(位于 visionai/python/examples/ 目录中)。

    获取 Python SDK 软件包

    Vertex AI Vision SDK 还包含一个 Python 库。按照以下说明下载并安装 Python SDK 的预构建版本。

    1. 下载软件包。

      您可以从 Vertex AI Vision SDK GitHub 发布页面下载 Python SDK 软件包,也可以运行以下命令:

      wget https://github.com/google/visionai/releases/download/v0.0.5/visionai-0.0.5-py3-none-any.whl
      
    2. 可选。创建并激活新的虚拟环境:

      python3 -m venv vaivenv
      source vaivenv/bin/activate
      
    3. 安装软件包:

      pip3 install visionai-0.0.5-py3-none-any.whl
      
    4. 可选。确认安装成功:

      python3
      
      import visionai
      
    5. 可选。停用虚拟环境:

      deactivate
      

    C++ 编程 SDK

    C++ 是我们支持的第一个编程 SDK。C++ 公共 SDK 位于 visionai/public/streams.h 中。如需了解参考信息,请参阅参考文档

    后续步骤