目標
在本範例中,您會使用 Google Vision API 偵測圖片中的臉孔。接著,您會使用該資料在每個臉部周圍繪製方框,證明臉部偵測結果正確無誤。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
- Cloud Vision
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- 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.
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Google Cloud Vision API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Google Cloud Vision API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. - 設定環境,以便使用 應用程式預設憑證。
-
設定語言專屬工作和工具:
C#
- 安裝 Google 用戶端程式庫
- 安裝 Visual Studio 2012/2015。
Java
- 安裝 Java。
- API 參考資料。
下載並安裝 Apache Maven 建構系統。Maven 會確保在建構專案時安裝 Google API 用戶端程式庫和 Vision API 用戶端程式庫,因為我們已將這些程式庫納入
pom.xml
。...
Node.js
- 安裝 Google 用戶端程式庫
- 安裝 node.js。
- API 參考資料。
安裝 npm 和 node-canvas。程式碼範例包含
package.json
,可使用npm install
指令安裝所有依附元件。請注意,node-canvas 有其他依附元件可能需要安裝,詳情請參閱 node-canvas 安裝文件。
PHP
- 安裝 Google 用戶端程式庫
- 安裝 PHP 和 Composer。
- API 參考資料。
- 安裝並啟用 PHP 適用的 GD。
Python
- 安裝 Google 用戶端程式庫
- 安裝 Python。
- API 參考資料。
- 安裝 pillow。
Ruby
- 安裝 Google 用戶端程式庫
- 安裝 Ruby 和 Bundler。
- API 參考資料。
- 安裝 ImageMagick。
建立服務物件
如要使用官方用戶端 SDK 存取 Google API,請根據 API 的探索文件建立服務物件,向 SDK 說明 API。您必須使用憑證,從 Vision API 的探索服務擷取該檔案:
Java
Node.js
Python
傳送臉部偵測要求
如要建構 Vision API 的要求,請先參閱 API 說明文件。在這種情況下,您會要求 images
資源annotate
您的圖片。對這個 API 提出的要求會以物件形式呈現,並包含 requests
清單。這份清單中的每個項目都包含兩項資訊:
- Base64 編碼的圖片資料
- 您希望系統為該圖片註解的特徵清單。
在本例中,您只會要求一張圖片的 FACE_DETECTION
註解,並傳回回應的相關部分:
Java
Node.js
Python
處理回應
恭喜!您已偵測到圖片中的臉孔。臉部註解要求的回應包含偵測到的臉部中繼資料,包括涵蓋臉部的多邊形座標。不過,此時這只是一串數字。讓我們使用這些標記,確認您確實已在圖片中找到臉孔。我們將使用 Vision API 傳回的座標,在圖片副本上繪製多邊形:
馬上開始全面整合吧!
Java
...
如要建構及執行範例,請從範例程式碼目錄執行下列指令:
mvn clean compile assembly:single java -cp target/vision-face-detection-1.0-SNAPSHOT-jar-with-dependencies.jar \ com.google.cloud.vision.samples.facedetect.FaceDetectApp \ data/face.jpg \ output.jpg
Node.js
如要執行範例,請從範例程式碼目錄執行下列指令:
node faceDetection resources/face.png
Python
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.