使用用戶端程式庫偵測圖片中的標籤
本頁說明如何以您慣用的程式語言開始使用 Vision API。
如要直接在 Cloud Shell 編輯器中按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 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
安裝用戶端程式庫
Go
go get cloud.google.com/go/vision/apiv1
Java
如要進一步瞭解如何設定 Java 開發環境,請參閱 Java 開發環境設定指南。
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Node.js
如要進一步瞭解如何設定 Node.js 開發環境,請參閱 Node.js 開發環境設定指南。
npm install @google-cloud/vision
Python
如要進一步瞭解如何設定 Python 開發環境,請參閱 Python 開發環境設定指南。
pip install --upgrade google-cloud-vision
標籤偵測範例
現在您可以使用 Vision API 要求取得圖片的資訊,例如標籤偵測。執行下列程式碼,即可進行您的第一個圖片標籤偵測要求。
Go
在試用這個範例之前,請先按照Go「使用用戶端程式庫的 Vision 快速入門導覽課程」中的設定說明操作。詳情請參閱 Vision Go API 參考說明文件。
如要向 Vision 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java「使用用戶端程式庫的 Vision 快速入門導覽課程」中的設定說明操作。詳情請參閱 Vision Java API 參考說明文件。
如要向 Vision 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Node.js
在試用這個範例之前,請先按照Node.js「使用用戶端程式庫的 Vision 快速入門導覽課程」中的設定說明操作。詳情請參閱 Vision Node.js API 參考說明文件。
如要向 Vision 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Python
在試用這個範例之前,請先按照Python「使用用戶端程式庫的 Vision 快速入門導覽課程」中的設定說明操作。詳情請參閱 Vision Python API 參考說明文件。
如要向 Vision 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行範例
如要執行先前的範例,請使用下列指令。如果您複製了特定範例語言的 GitHub 存放區,請取消註解第一行:
Go
# cd golang-samples/vision/vision_quickstart/
go run .
Java
# cd java-docs-samples/vision/snippets/src/main/java/com/example/vision/quickstart/
javac QuickstartSample.java
java QuickstartSample.java
Node.js
# cd nodejs-docs-samples/vision/
node quickstart.js
Python
# cd python-docs-samples/vision/snippets/quickstart
python -c 'import quickstart; quickstart.run_quickstart()'
還順利嗎?
清除所用資源
如要避免系統向您的 Google 帳戶收取本快速入門導覽課程所用資源的費用:
- 使用 Google Cloud console 刪除您不需要的專案。
後續步驟
進一步瞭解 Vision API 用戶端程式庫。