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

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

  • 建立 Cloud Storage bucket。
  • 將圖片上傳至 Cloud Storage,並公開提供使用。
  • 使用該圖片向 Vision API 提出要求。

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

事前準備

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

設定專案

  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. 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 Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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 Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 必要的角色

    1. Make sure that you have the following role or roles on the project: Storage Admin (roles/storage.admin)

      Check for the roles

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

      4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

      Grant the roles

      1. In the Google Cloud console, go to the IAM page.

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

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

      建立 Cloud Storage 值區

      1. In the Google Cloud console, go to the Cloud Storage Buckets page.

        Go to Buckets

      2. Click Create.
      3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
        1. For Name your bucket, enter a unique bucket name. Don't include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
        2. In the Choose where to store your data section, do the following:
          1. Select a Location type.
          2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
          3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

            Set up cross-bucket replication

            1. In the Bucket menu, select a bucket.
            2. In the Replication settings section, click Configure to configure settings for the replication job.

              The Configure cross-bucket replication pane appears.

              • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
              • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
              • Click Done.
        3. In the Choose how to store your data section, do the following:
          1. In the Set a default class section, select the following: Standard.
          2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
        4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
        5. In the Choose how to protect object data section, do the following:
          • Select any of the options under Data protection that you want to set for your bucket.
            • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
            • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
            • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
              • To enable Object Retention Lock, click the Enable object retention checkbox.
              • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
          • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
      4. Click Create.

      向 Cloud Vision 提出要求

      1. 儲存下列範例圖片檔案的本機版本 (demo-img.jpg)。

        兩名女子在雅加達騎單車。
        圖片來源Unsplash 的 Rohiim Ariful
      2. 在 Google Cloud 控制台,前往「Cloud Storage bucket」頁面。

        前往「Buckets」(值區) 頁面

      3. 選取您在前一節建立的值區。

      4. 按一下「Upload Files」(上傳檔案),然後選取要上傳到 Cloud Storage bucket 的 demo-img.jpg 檔案本機版本。

      5. 檔案上傳至 Cloud Storage bucket 並列出後,請公開分享圖片

      6. 在「Try this method」(試用這個方法) 部分,完成互動式 API Explorer 範本,方法是將 image.source.imageUri 欄位中的 cloud-samples-data/vision 替換為您上傳 demo-img.jpg 檔案的 Cloud Storage bucket 名稱。要求主體應如下所示:

        {
          "requests": [
            {
              "features": [
                {
                  "type": "LABEL_DETECTION"
                }
              ],
              "image": {
                "source": {
                  "imageUri": "gs://cloud-samples-data/vision/demo-img.jpg"
                }
              }
            }
          ]
        }

      7. 按一下「執行」,將要求傳送至服務。JSON 回應會顯示在要求主體之後。

      恭喜!您已向 Cloud Vision 發出第一個 images.annotate 要求。

      清除所用資源

      如要避免產生不必要的 Google Cloud 費用,請前往 Google Cloud 控制台刪除不需要的 Cloud Storage bucket (和專案)。

      後續步驟