設定專案和開發環境

本指南說明如何設定 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,可用於以程式輔助方式管理、控制及執行 Vertex AI Vision 的各種形式 I/O。C++ 程式設計 API 就是這類 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。如需參考資訊,請參閱參考說明文件

    後續步驟