目標
在本範例中,您將使用 Google Vision API 偵測圖片中的臉孔。為證明系統正確偵測到臉部,您接著會使用該資料在每個臉部周圍繪製方塊。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- Cloud Vision
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. 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.enablepermission. 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. 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.enablepermission. 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 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。