使用指令列偵測圖片中的標籤
這個頁面說明如何使用 REST 介面和 curl
指令,將三個特徵偵測和註解要求傳送至 Vision API。
Vision API 可將 Google 影像辨識技術輕鬆整合至開發人員應用程式,您可以將圖片資料和所需功能類型傳送至 Vision API,然後根據您感興趣的圖片屬性,取得相應的回覆。如要進一步瞭解提供的功能類型,請參閱「所有 Vision API 功能清單」。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vision API:
gcloud services enable vision.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/storage.objectViewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vision API:
gcloud services enable vision.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/storage.objectViewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- 三項
labelAnnotations
結果 - 一項
textAnnotations
結果 (為求清楚起見,已縮短) - 一項
localizedObjectAnnotations
結果 - description: "人物", score: 0.950
- description: "街道", score: 0.891
- description: "交通工具", score: 0.890
- text: 牛牛面馆\n
- vertices: (x: 159, y: 212), (x: 947, y: 212), (x: 947, y: 354), (x: 159, y: 354 )
- name: "人", score: 0.944
- normalized vertices: (x: 0.260, y: 0.468), (x: 0.407, y: 0.468), (x: 0.407, y: 0.895), (x: 0.260, y: 0.895)
發出圖片註解要求
完成「事前準備」步驟後,即可使用 Vision API 為圖片檔加上註解。
在本範例中,您會使用 curl 傳送要求至 Vision API,並使用下列圖片:
Cloud Storage URI:
gs://cloud-samples-data/vision/using_curl/shanghai.jpeg
HTTPS 網址:
https://console.cloud.google.com/storage/browser/cloud-samples-data/vision/using_curl/shanghai.jpeg

建立要求 JSON
下列 request.json
檔案示範如何要求三項 images:annotate
功能,並限制回應中的結果。
建立具有下列文字的 JSON 要求檔案,並將檔案儲存為工作目錄中的 request.json
純文字檔案:
request.json
{ "requests": [ { "image": { "source": { "imageUri": "gs://cloud-samples-data/vision/using_curl/shanghai.jpeg" } }, "features": [ { "type": "LABEL_DETECTION", "maxResults": 3 }, { "type": "OBJECT_LOCALIZATION", "maxResults": 1 }, { "type": "TEXT_DETECTION", "maxResults": 1, "model": "builtin/latest" } ] } ] }
傳送要求
您可以使用 curl 和 request.json
中的主體內容,將要求傳送至 Vision API。在指令列中輸入下列內容:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "x-goog-user-project: PROJECT_ID" \ -H "Content-Type: application/json; charset=utf-8" \ https://vision.googleapis.com/v1/images:annotate -d @request.json
解讀回應
您應該會看到類似下方的 JSON 回應。
要求 JSON 主體為各個註解類型指定 maxResults
。
因此,您會在回應 JSON 中看到以下內容:
標籤偵測結果

文字偵測結果

物件偵測結果

恭喜!您已將第一個要求傳送至 Vision API。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke