使用 Cloud Vision API 偵測圖片中的標籤

本快速入門導覽課程會說明完成下列作業的方法:

  • 建立 Cloud Storage bucket。
  • 將圖片上傳至 Cloud Storage,並將檔案設為公開存取。
  • 使用該圖片向 Vision API 提出要求。

完成這些步驟大約需要五分鐘。您最多可免費在 Cloud Storage 中儲存 5 GB 的資料,並每月免費向 Vision API 提出最多 1000 項功能要求

事前準備

如果尚未設定專案並建立 Cloud Storage bucket,請按照下列操作說明進行。

設定專案

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Vision API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Vision API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

必要的角色

  1. 請確認您在專案中擁有下列角色: 儲存空間管理員 (roles/storage.admin)

    檢查角色

    1. 前往 Google Cloud 控制台的「IAM」頁面。

      前往「IAM」頁面
    2. 選取專案。
    3. 在「主體」欄中,找出所有識別您或您所屬群組的資料列。如要瞭解自己所屬的群組,請與管理員聯絡。

    4. 針對指定或包含您的所有列,請檢查「角色」欄,確認角色清單是否包含必要角色。

    授予角色

    1. 前往 Google Cloud 控制台的「IAM」頁面。

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「Grant access」(授予存取權)
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。

    5. 按一下「選取角色」,然後搜尋角色。
    6. 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
    7. 按一下「Save」(儲存)

建立 Cloud Storage 值區

  1. 前往 Google Cloud 控制台的 Cloud Storage「Buckets」(值區) 頁面。

    前往「Buckets」(值區) 頁面

  2. 點選 「Create」(建立)
  3. 在「建立 bucket」頁面中,輸入 bucket 資訊。如要前往下一個步驟,請按「繼續」
    1. 在「Name your bucket」(為 bucket 命名) 欄位中,輸入 bucket 的專屬名稱。請勿在 bucket 名稱中加入任何機密資訊,因為 bucket 命名空間全域通用並會公開顯示。
    2. 在「Choose where to store your data」(選擇資料的儲存位置) 專區中,執行下列操作:
      1. 選取「位置類型」
      2. 從「位置類型」下拉式選單中,選擇要永久儲存 bucket 資料的位置。
        • 如果您選取「雙區域」位置類型,也可以使用相關核取方塊啟用強化型複製
      3. 如要設定跨值區複製,請選取「透過 Storage 移轉服務新增跨值區複製作業」,然後按照下列步驟操作:

        設定跨 bucket 複製作業

        1. 在「Bucket」選單中選取 bucket。
        2. 在「複製設定」部分,按一下「設定」,設定複製作業的設定。

          系統隨即會顯示「設定跨 bucket 複製作業」窗格。

          • 如要依物件名稱前置字串篩選要複製的物件,請輸入要納入或排除物件的前置字串,然後按一下「新增前置字串」
          • 如要為複製的物件設定儲存空間級別,請從「儲存空間級別」選單中選取儲存空間級別。如果略過這個步驟,複製的物件預設會使用目標值區的儲存空間級別。
          • 按一下 [完成]
    3. 在「選擇資料儲存方式」部分,執行下列操作:
      1. 在「設定預設類別」部分,選取「Standard」
      2. 如要啟用階層命名空間,請在「為資料密集型工作負載提供最理想的儲存空間」部分,選取「為這個值區啟用階層命名空間」
    4. 在「選取如何控制物件的存取權」部分,選取 bucket 是否要強制執行禁止公開存取,並為 bucket 的物件選取存取權控管方法
    5. 在「選擇保護物件資料的方式」部分,執行下列操作:
      • 在「資料保護」下方,選取要為 bucket 設定的選項。
        • 如要啟用虛刪除,請按一下「虛刪除政策 (用於資料復原)」核取方塊,並指定要保留物件的天數 (刪除後)。
        • 如要設定「物件版本管理」,請按一下「物件版本管理 (用於版本管控)」核取方塊,並指定每個物件的版本數量上限,以及非現行版本失效的天數。
        • 如要為物件和 bucket 啟用保留政策,請勾選「保留 (符合法規)」核取方塊,然後執行下列操作: