使用 Cloud Vision API 偵測圖片中的標籤
本快速入門導覽課程會說明完成下列作業的方法:
- 建立 Cloud Storage bucket。
- 將圖片上傳至 Cloud Storage,並將檔案設為可公開存取。
- 使用該圖片向 Vision API 提出要求。
完成這些步驟大約需要五分鐘。您可以在 Cloud Storage 中免費儲存最多 5 GB 的資料,並免費向 Vision API 提出最多 1000 項功能要求。
事前準備
如果您尚未完成這項操作,請按照下列操作說明設定專案並建立 Cloud Storage 值區。
設定專案
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Vision API.
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
- 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.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
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.
-
In the Choose how to store your data section, do the following:
- In the Set a default class section, select the following: Standard.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- 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.
-
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 (Data encryption method. ), and select a
- Select any of the options under Data protection that you
want to set for your bucket.
- Click Create.
儲存本機版本的範例圖片檔案 (
demo-img.jpg
)。在 Google Cloud 控制台,前往「Cloud Storage bucket」頁面。
選取您在上一節建立的值區。
按一下「Upload Files」(上傳檔案),然後選取
demo-img.jpg
檔案的本機版本,上傳至 Cloud Storage 值區。圖片版權:Rohiim Ariful (Unsplash)。 檔案上傳完成並顯示在 Cloud Storage 值區後,請公開共用圖片。
在「Try this method」部分,將
image.source.imageUri
欄位中的cloud-samples-data/vision
替換為您上傳demo-img.jpg
檔案的 Cloud Storage 值區名稱,即可完成互動式 API Explorer 範本。要求主體應如下所示:{ "requests": [ { "features": [ { "type": "LABEL_DETECTION" } ], "image": { "source": { "imageUri": "gs://cloud-samples-data/vision/demo-img.jpg" } } } ] }
按一下「執行」,將要求傳送至服務。JSON 回應會顯示在要求主體之後。
建立 Cloud Storage 值區
向 Cloud Vision 提出要求
恭喜!您已向 Cloud Vision 提出第一個 images.annotate
要求。
清除所用資源
如要避免產生不必要的 Google Cloud 費用,請使用 Google Cloud 控制台刪除不需要的 Cloud Storage bucket (和專案)。