本教學課程示範如何使用 Cloud Run functions、Cloud Vision API 與 ImageMagick 偵測並模糊處理上傳至 Cloud Storage bucket 的令人反感圖片。
目標
- 部署 Cloud Storage 觸發的背景 Cloud Run 函式。
- 使用 Vision API 偵測暴力或成人內容。
- 使用 ImageMagick 模糊處理令人反感的圖片。
- 上傳食人殭屍的圖片來測試函式。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- Cloud Run functions
- Cloud Storage
- 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 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 Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
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. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
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 Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
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. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init - 準備開發環境。
- 將圖片上傳至 Cloud Storage bucket。
- 函式會使用 Vision API 分析圖片。
- 如果偵測到暴力或成人內容,函式會使用 ImageMagick 模糊處理圖片。
- 模糊處理後的圖片會上傳至另一個 Cloud Storage bucket,以供使用。
建立 Cloud Storage bucket 以上傳圖片,其中
YOUR_INPUT_BUCKET_NAME是全域不重複的 bucket 名稱:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
建立 Cloud Storage bucket,接收模糊處理的圖片,其中
YOUR_OUTPUT_BUCKET_NAME是全域不重複的 bucket 名稱:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
將應用程式存放區範例複製到本機電腦中:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
您也可以下載 zip 格式的範例檔案,然後將檔案解壓縮。
變更為包含 Cloud Run functions 程式碼範例的目錄:
Node.js
cd nodejs-docs-samples/functions/imagemagick/
Python
cd python-docs-samples/functions/imagemagick/
Go
cd golang-samples/functions/imagemagick/
Java
cd java-docs-samples/functions/imagemagick/
Ruby
cd ruby-docs-samples/functions/imagemagick/
RUNTIME:以 Ubuntu 18.04 為基礎的執行階段 (後續執行階段則不支援 ImageMagick)。YOUR_INPUT_BUCKET_NAME:用於上傳圖片的 Cloud Storage bucket 名稱。YOUR_OUTPUT_BUCKET_NAME:模糊處理後的圖片應儲存至的 bucket 名稱。上傳令人反感的圖片,例如這張食人殭屍圖片:
gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
其中
YOUR_INPUT_BUCKET_NAME是您先前建立用於上傳圖片的 Cloud Storage bucket。觀察記錄以確定執行作業已完成:
gcloud functions logs read --limit 100
您可以在先前建立的
YOUR_OUTPUT_BUCKET_NAMECloud Storage bucket 中查看模糊處理的圖片。- 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.
如已安裝 gcloud CLI,請執行下列指令進行更新:
gcloud components update
以圖表呈現資料流程
ImageMagick 教學課程應用程式中的資料流動過程涉及數個步驟:
準備應用程式
瞭解程式碼
匯入依附元件
應用程式必須匯入數個依附元件,才能與Google Cloud 服務、ImageMagick 和檔案系統互動:
Node.js
Python
Go
Java
Ruby
分析圖片
系統會在圖片上傳至您建立用於儲存圖片的 Cloud Storage bucket 時,叫用下列函式。該函式會使用 Vision API 偵測上傳圖片中的暴力或成人內容。
Node.js
Python
Go
Java
Ruby
模糊處理圖片
在上傳的圖片中偵測到暴力或成人內容時,會呼叫下列函式。函式會下載令人反感的圖片,使用 ImageMagick 模糊處理圖片,然後上傳模糊處理後的圖片覆蓋原始圖片。
Node.js
Python
Go
Java
Ruby
部署函式
如要使用儲存空間觸發條件部署函式,請在包含程式碼範例的目錄中執行下列指令 (如果是 Java,則是在 pom.xml 檔案中執行):
Node.js
gcloud functions deploy blurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Python
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Go
gcloud functions deploy BlurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Java
gcloud functions deploy java-blur-function \ --no-gen2 \ --entry-point=functions.ImageMagick \ --runtime=RUNTIME \ --memory 512MB \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
C#
gcloud functions deploy csharp-blur-function \ --no-gen2 \ --entry-point=ImageMagick.Function \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Ruby
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
更改下列內容:
在這個特定範例中,請勿在 deploy 指令中加入 gs:// 做為 bucket 名稱的一部分。
上傳圖片
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
刪除專案的方法如下:
刪除函式
刪除 Cloud Run functions 不會移除儲存在 Cloud Storage 中的任何資源。
如要刪除在本教學課程中部署的函式,請執行下列指令:
Node.js
gcloud functions delete blurOffensiveImages
Python
gcloud functions delete blur_offensive_images
Go
gcloud functions delete BlurOffensiveImages
Java
gcloud functions delete java-blur-function
Ruby
gcloud functions delete blur_offensive_images
您也可以從Google Cloud 控制台刪除 Cloud Run functions。